반응형
R,G,B각각에 대해 DFS를 시행한다. R:1, G:2, B:3
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 | #include <iostream> using namespace std; char k; int h, w, a[101][101], b[101][101], acheck[101][101], bcheck[101][101], n, acnt,bcnt, dh[] = { 0,0,-1,1 }, dw[] = { -1,1,0,0 };//a는 정상인 b는 색약 void aDFS(int h, int w, int color,int acnt) { acheck[h][w] = color; for (int i = 0; i < 4; i++) { int nh = h + dh[i]; int nw = w + dw[i]; if (0 <= nh && nh < n && 0 <= nw && nw <= n) { if (a[nh][nw] == color && acheck[nh][nw] == 0) aDFS(nh, nw, color,acnt); } } } void bDFS(int h, int w, int color, int bcnt) { bcheck[h][w] = color; for (int i = 0; i < 4; i++) { int nh = h + dh[i]; int nw = w + dw[i]; if (0 <= nh && nh < n && 0 <= nw && nw <= n) { if (b[nh][nw] == color && bcheck[nh][nw] == 0) bDFS(nh, nw, color, bcnt); } } } int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> k; switch (k) { case 'R': a[i][j] = 1; b[i][j] = 1; break; case 'G': a[i][j] = 2; b[i][j] = 1; break; case 'B':a[i][j] = 3; b[i][j] = 2; break; } } cin.ignore(); } /*for (int i = 0; i<n; i++,puts("")) for (int j = 0; j < n; j++) { cout << a[i][j] << ' '; } cout << '\n'; for (int i = 0; i<n; i++, puts("")) for (int j = 0; j < n; j++) { cout << b[i][j] << ' '; }*/ for(int i = 0; i<n; i++) for (int j = 0; j < n; j++) { if (a[i][j] == 1 && acheck[i][j] == 0 &&(acheck[i][j] !=2 || acheck[i][j] != 3)) { aDFS(i, j, 1, ++acnt); } if (a[i][j] == 2 && acheck[i][j] == 0 && (acheck[i][j] != 1 || acheck[i][j] != 3)) { aDFS(i, j, 2, ++acnt); } if (a[i][j] == 3 && acheck[i][j] == 0 && (acheck[i][j] != 1 || acheck[i][j] != 2)) { aDFS(i, j, 3, ++acnt); } if (b[i][j] == 1 && bcheck[i][j] == 0 && (bcheck[i][j] != 2 || bcheck[i][j] != 3)) { bDFS(i, j, 1, ++bcnt); } if (b[i][j] == 2 && bcheck[i][j] == 0 && (bcheck[i][j] != 1 || bcheck[i][j] != 3)) { bDFS(i, j, 2, ++bcnt); } } cout << acnt << ' ' << bcnt; } | cs |
'Algorithm' 카테고리의 다른 글
(C++) - 백준(BOJ) 11723 : 집합 (0) | 2017.02.14 |
---|---|
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1967번:트리의 지름 답 (0) | 2017.02.11 |
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 1850번:최대공약수 답 (0) | 2017.02.10 |
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 3036번:링 답 (0) | 2017.02.10 |
C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 9012번:괄호 답 (0) | 2017.02.10 |