사탕게임

[백준] 3085번 - 사탕게임


문제 링크

인접한 두 위치의 사탕을 바꾸어 최대한 긴 연속된 같은 종류의 사탕을 만드는 문제이다.
가장 쉬운 방법은 가능한 모든 경우를 다 해보는 것이다. 인접한 좌우 사탕의 위치를 바꾸는 경우의 수는 n(n-1) 이고, 상하 또한 마찬가지다. 즉, 완전 탐색을 사용할 시 시간복잡도는 $O(n^2)$이다.
n이 최대 50이므로 $O(n^2)$이지만 시간초과는 발생하지 않는다.

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <bits/stdc++.h>
using namespace std;

int n,ans;
vector<vector<char>> candies;

int main(void) {
    scanf("%d", &n);

    candies.resize(n);
    for(int i=0; i<n; ++i)
        candies[i].resize(n);

    for(int i=0; i<n; ++i) {
        for(int j=0; j<n; ++j)
            scanf(" %c", &candies[i][j]);
    }

    // 행 검사
    for(int i=0; i<n; ++i) {
        int temp=0;
        for(int j=0; j<n-1; ++j) {
            if(candies[i][j] == candies[i][j+1]) {
                if(temp == 0)   
                    temp=2;
                else
                    ++temp;
            } else {
                ans = ans < temp ? temp : ans;
                temp=0;
            }
        }
        ans = ans < temp ? temp : ans;
    }

    // 열 검사
    for(int j=0; j<n; ++j) {
        int temp=0;
        for(int i=0; i<n-1; ++i) {
            if(candies[i][j] == candies[i+1][j]) {
                if(temp == 0)   
                    temp=2;
                else
                    ++temp;
            } else {
                ans = ans < temp ? temp : ans;
                temp=0;
            }
        }
        ans = ans < temp ? temp : ans;
    }

    // 같은 행 캔디 바꾸기
    for(int i=0; i<n; ++i) {
        for(int j=0; j<n-1; ++j) {
            if(candies[i][j] == candies[i][j+1])
                continue;

            candies[i][j] ^= candies[i][j+1];
            candies[i][j+1] ^= candies[i][j];
            candies[i][j] ^= candies[i][j+1];

            int temp=0;
            for(int k=0; k<n-1; ++k) {
                if(candies[i][k] == candies[i][k+1]) {
                    if(temp == 0)
                        temp = 2;
                    else
                        ++temp;
                }
                else {
                    ans = ans < temp ? temp : ans;
                    temp=0;
                }
            }
            ans = ans < temp ? temp : ans;

            temp=0;
            for(int k=0; k<n-1; ++k) {
                if(candies[k][j] == candies[k+1][j]) {
                    if(temp == 0)
                        temp = 2;
                    else
                        ++temp;
                }
                else {
                    ans = ans < temp ? temp : ans;
                    temp=0;
                }
            }
            ans = ans < temp ? temp : ans;

            temp=0;
            for(int k=0; k<n-1; ++k) {
                if(candies[k][j+1] == candies[k+1][j+1]) {
                    if(temp == 0)
                        temp = 2;
                    else
                        ++temp;
                }
                else {
                    ans = ans < temp ? temp : ans;
                    temp=0;
                }
            }
            ans = ans < temp ? temp : ans;

            candies[i][j] ^= candies[i][j+1];
            candies[i][j+1] ^= candies[i][j];
            candies[i][j] ^= candies[i][j+1];
        }   
    }

    // 같은 열 캔디 바꾸기
    for(int j=0; j<n; ++j) {
        for(int i=0; i<n-1; ++i) {
            if(candies[i][j] == candies[i+1][j])
                continue;

            candies[i][j] ^= candies[i+1][j];
            candies[i+1][j] ^= candies[i][j];
            candies[i][j] ^= candies[i+1][j];

            int temp=0;
            for(int k=0; k<n-1; ++k) {
                if(candies[i][k] == candies[i][k+1]) {
                    if(temp == 0)
                        temp = 2;
                    else
                        ++temp;
                }
                else {
                    ans = ans < temp ? temp : ans;
                    temp=0;
                }
            }
            ans = ans < temp ? temp : ans;

            temp=0;
            for(int k=0; k<n-1; ++k) {
                if(candies[i+1][k] == candies[i+1][k+1]) {
                    if(temp == 0)
                        temp = 2;
                    else
                        ++temp;
                }
                else {
                    ans = ans < temp ? temp : ans;
                    temp=0;
                }
            }
            ans = ans < temp ? temp : ans;

            temp=0;
            for(int k=0; k<n-1; ++k) {
                if(candies[k][j] == candies[k+1][j]) {
                    if(temp == 0)
                        temp = 2;
                    else
                        ++temp;
                }
                else {
                    ans = ans < temp ? temp : ans;
                    temp=0;
                }
            }
            ans = ans < temp ? temp : ans;

            candies[i][j] ^= candies[i+1][j];
            candies[i+1][j] ^= candies[i][j];
            candies[i][j] ^= candies[i+1][j];
        }   
    }

    printf("%d", ans);
}
0%