[C++]BOJ 10026 - 적록색약

[C++]BOJ 10026 - 적록색약

문자열 입력인 문제가 오랜만이라 한번 틀렸다ㅠ
입력 받을 때 주의할 것.
무작정 두가지 경우를 다 돌려도 되지만 더 간단하게 짜봤다.
핵심은 적록색약이 아닌 사람을 탐색할 때 R을 B로 바꿔버리는 것이다.
그러면 적록색약인 사람을 탐색할 때 R은 전부 B로 바뀌어 조건에 맞게 탐색하게 된다.
dfs로 풀어봤다.


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
#include <iostream>
#include <cstring>
using namespace std;
int n, ans1, ans2, c, xx, yy, dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0};
char arr[102][102], v[102][102], color[3] = {'R', 'G', 'B'};
void dfs(int x, int y) {
v[y][x] = 1;
if(arr[y][x] == 'R') arr[y][x] = 'G';
for(int i = 0; i < 4; i++) {
xx = x + dx[i]; yy = y + dy[i];
if(xx < n && yy < n && xx >= 0 && yy >= 0 && arr[yy][xx] == color[c] && !v[yy][xx]) dfs(xx, yy);
}
}
int main() {
cin >> n;
for(int i = 0; i < n; i++)
scanf("%s", &arr[i]);
for(int t = 0; t < 2; t++) { // 0 = 적록색약이 아닌사람, 1 = 적록색약인 사람
for(c = t; c < 3; c++) // color
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(arr[i][j] == color[c] && !v[i][j]) {
dfs(j, i);
if(t) ans2++;
else ans1++;
}
memset(v, 0, sizeof(v));
}
cout << ans1 << " " << ans2;
}