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++) { for(c = t; c < 3; c++) 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; }
|