Skip to content

Commit 550dcca

Browse files
committed
[Silver II] Title: 사탕 게임, Time: 172 ms, Memory: 14420 KB -BaekjoonHub
1 parent bb1a5db commit 550dcca

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [Silver II] 사탕 게임 - 3085
2+
3+
[문제 링크](https://www.acmicpc.net/problem/3085)
4+
5+
### 성능 요약
6+
7+
메모리: 14420 KB, 시간: 172 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 구현
12+
13+
### 제출 일자
14+
15+
2024년 12월 31일 09:56:45
16+
17+
### 문제 설명
18+
19+
<p>상근이는 어렸을 적에 "봄보니 (Bomboni)" 게임을 즐겨했다.</p>
20+
21+
<p>가장 처음에 N×N크기에 사탕을 채워 놓는다. 사탕의 색은 모두 같지 않을 수도 있다. 상근이는 사탕의 색이 다른 인접한 두 칸을 고른다. 그 다음 고른 칸에 들어있는 사탕을 서로 교환한다. 이제, 모두 같은 색으로 이루어져 있는 가장 긴 연속 부분(행 또는 열)을 고른 다음 그 사탕을 모두 먹는다.</p>
22+
23+
<p>사탕이 채워진 상태가 주어졌을 때, 상근이가 먹을 수 있는 사탕의 최대 개수를 구하는 프로그램을 작성하시오.</p>
24+
25+
### 입력
26+
27+
<p>첫째 줄에 보드의 크기 N이 주어진다. (3 ≤ N ≤ 50)</p>
28+
29+
<p>다음 N개 줄에는 보드에 채워져 있는 사탕의 색상이 주어진다. 빨간색은 C, 파란색은 P, 초록색은 Z, 노란색은 Y로 주어진다.</p>
30+
31+
<p>사탕의 색이 다른 인접한 두 칸이 존재하는 입력만 주어진다.</p>
32+
33+
### 출력
34+
35+
<p>첫째 줄에 상근이가 먹을 수 있는 사탕의 최대 개수를 출력한다.</p>
36+
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static char[][] arr;
6+
static int N,answer;
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
N = Integer.parseInt(br.readLine());
11+
12+
arr = new char[N][N];
13+
answer = 0;
14+
for(int i =0; i<N; i++) {
15+
String input = br.readLine();
16+
for(int j =0; j<N; j++) {
17+
arr[i][j] = input.charAt(j);
18+
}
19+
}
20+
21+
for(int i =0; i<N; i++) {
22+
for(int j =0; j<N; j++) {
23+
if(j+1 <N) {
24+
swap(i, j, i, j+1);
25+
check();
26+
swap(i, j, i, j+1);
27+
}
28+
29+
if(i+1<N) {
30+
swap(i, j, i+1, j);
31+
check();
32+
swap(i, j, i+1, j);
33+
}
34+
35+
}
36+
}
37+
38+
System.out.println(answer);
39+
40+
}
41+
42+
static void swap(int startX, int startY, int endX, int endY) {
43+
char temp = arr[startX][startY];
44+
arr[startX][startY] = arr[endX][endY];
45+
arr[endX][endY] = temp;
46+
}
47+
static void check() {
48+
49+
for(int i =0; i<N; i++) {
50+
int count = 1;
51+
52+
for(int j =0; j<N-1;j++) {
53+
if(arr[i][j] == arr[i][j+1]) {
54+
count+=1;
55+
answer = Math.max(answer,count);
56+
}
57+
else {
58+
count=1;
59+
}
60+
}
61+
}
62+
63+
for(int i =0; i<N; i++) {
64+
int count =1;
65+
66+
for(int j =0; j<N-1; j++) {
67+
if(arr[j][i] == arr[j+1][i]) {
68+
count+=1;
69+
answer= Math.max(answer,count);
70+
}
71+
else {
72+
count=1;
73+
}
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)