[백준 3085번] 사탕 게임

2017. 10. 12. 23:47알고리즘/백준

반응형




소스코드


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
#include <iostream>
using namespace std;
 
int n, Max=0;
char map[51][51];
 
void garo() {
    for (int i = 0; i < n; i++) {
        int cnt = 1;
        for (int j = 0; j < n; j++) { // chk 가로
            if (map[i][j] == map[i][j + 1]) { cnt++; }
            else{
                if (Max < cnt)
                    Max = cnt;
                cnt = 1;
            }
        }
    }
}
 
void sero() {
    for (int i = 0; i < n; i++) {
        int cnt = 1;
        for (int j = 0; j < n; j++) { // chk 세로
            if (map[j][i] == map[j + 1][i]) {cnt++; }
            else {
                if (Max < cnt)
                    Max = cnt;
                cnt = 1;
            }
        }
    }
}
 
int main() {
    cin >> n;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            cin >> map[i][j];
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1; j++) {
            char tmp = map[i][j];
            map[i][j] = map[i][j + 1];
            map[i][j + 1= tmp;
            garo();
            sero();
            tmp = map[i][j];
            map[i][j] = map[i][j + 1];
            map[i][j + 1= tmp;
        }
    }
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n-1; j++) {
            char tmp = map[j][i];
            map[j][i] = map[j+1][i];
            map[j+1][i] = tmp;
            garo();
            sero();
            tmp = map[j][i];
            map[j][i] = map[j + 1][i];
            map[j + 1][i] = tmp;
        }
    }
 
    cout << Max << '\n';
    return 0;
}
cs


반응형