Cooper's devlog

사탕 게임:3085번 - JAVA 본문

Algorithm/Baekjoon

사탕 게임:3085번 - JAVA

cooper_dev 2020. 7. 21. 01:15

1. 문제 링크

https://www.acmicpc.net/problem/3085

 

3085번: 사탕 게임

문제 상근이는 어렸을 적에 "봄보니 (Bomboni)" 게임을 즐겨했다. 가장 처음에 N×N크기에 사탕을 채워 놓는다. 사탕의 색은 모두 같지 않을 수도 있다. 상근이는 사탕의 색이 다른 인접한 두 칸을 고

www.acmicpc.net


2. 문제 설명


3. 문제 접근

1. 배열 값을 좌우/상하 변경

2. check(가능한 캔디 길이를 확인)

3. 데이터 원상 복귀

 

check method 설명

1. 행 탐색 : 좌우 값이 일치 시 -> count++

2. 열 탐색 : 상하 값이 일치 시 -> count++

3. count < ans라면, ans = count


4. 문제 풀이

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package brute_force;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class candy_game {
    static char[][] data;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        
        data = new char[n][n];
        for (int i = 0; i < n; i++)
            data[i] = br.readLine().toCharArray();
        
        int ans = 0;
        for (int row = 0; row < n; row++) {
            for (int col = 0; col < n; col++) {
                if(col + 1 < n) {
                    char char_tmp = data[row][col];
                    data[row][col] = data[row][col+1];
                    data[row][col + 1= char_tmp;
                    
                    int ans_tmp = check(data);
                    if(ans < ans_tmp)
                        ans = ans_tmp;
                    char_tmp = data[row][col];
                    data[row][col] = data[row][col + 1];
                    data[row][col + 1= char_tmp;
                }
                
                if(row + 1 < n) {
                    char char_tmp = data[row][col];
                    data[row][col] = data[row+1][col];
                    data[row + 1][col] = char_tmp;
                    
                    int ans_tmp = check(data);
                    if(ans < ans_tmp)
                        ans = ans_tmp;
                    char_tmp = data[row][col];
                    data[row][col] = data[row + 1][col];
                    data[row + 1][col] = char_tmp;
                }
            }
        }
        
        System.out.println(ans);
    }
    
    static int check(char[][] data) {
        int n = data.length;
        int ans = 1;
        int count = 1;
        for (int row = 0; row < n; row++) {
            count = 1;
            for (int col = 1; col < n; col++) {
                if(data[row][col] == data[row][col-1]) {
                    count+= 1;
                }else {
                    count = 1;
                }
                if(ans < count)
                    ans = count;
            }
        }
        
        count = 1;
        for (int col = 0; col< n; col++) {
            count = 1;
            for (int row = 1; row < n; row++) {
                if(data[row][col] == data[row - 1][col]) {
                    count+= 1;
                }else {
                    count = 1;
                }
                if(ans < count)
                    ans = count;
            }
        }
        
        return ans;
    }
}
 
 

 

<정답 확인>

 


 

'Algorithm > Baekjoon' 카테고리의 다른 글

좋은 친구 : 3078번 - JAVA  (0) 2020.10.05
합이 0인 네 정수:7453 - JAVA  (0) 2020.10.03
일곱 난쟁이:2309번 - JAVA  (0) 2020.07.21
가장 큰 증가 부분 수열:11055번 - JAVA  (0) 2020.07.21
동물원: 1309번 - JAVA  (0) 2020.07.17
Comments