문제를 잘읽자...
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 | #include <cstdio> #include <cstring> #include <algorithm> #define MAX 101 using namespace std; bool is_visited[101][101]; char color[101][101] = { 0 }; char blind_color[101][101] = { 0 }; int dx[] = { 0, 0, -1, 1 }; int dy[] = { 1, -1, 0, 0 }; void dfs(char c, int x, int y, char(*arr)[MAX]) { is_visited[x][y] = true; for (int i = 0; i < 4; i++) { int to_x = x + dx[i]; int to_y = y + dy[i]; if (arr[to_x][to_y] == c) { if (!is_visited[to_x][to_y]) { dfs(c, to_x, to_y, arr); } } } } int main() { int n; int res = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf(" %c", &color[i][j]); blind_color[i][j] = color[i][j]; if (blind_color[i][j] == 'G') blind_color[i][j] = 'R'; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (!is_visited[i][j]) { dfs(color[i][j], i, j, color); res++; } } } printf("%d ", res); res = 0; memset(is_visited, false, sizeof(is_visited)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (!is_visited[i][j]) { dfs(blind_color[i][j], i, j, blind_color); res++; } } } printf("%d\n", res); } | cs |