#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 101;
int n;
char color[MAX][MAX];
char visited[MAX][MAX];
int colornum = 0;
typedef struct {
int x, y;
}Move;
Move m[4] = { {1,0},{-1,0},{0,1},{0,-1} };
void dfs(int i,int j) {
visited[i][j] = 1;
for (int k = 0; k < 4; k++) {
int ma = i + m[k].x;
int mb = j + m[k].y;
if (ma >= 0 && ma < n && mb >= 0 && mb < n) {
if (color[ma][mb] == color[i][j] && visited[ma][mb] == 0)
dfs(ma, mb);
}
}
}
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> color[i];
}
memset(visited, 0, sizeof(visited));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visited[i][j] == 0) {
dfs(i, j);
colornum++;
}
}
}
cout << colornum << " ";
memset(visited, 0, sizeof(visited));
colornum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (color[i][j] == 'R')
color[i][j] = 'G';
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visited[i][j] == 0) {
dfs(i, j);
colornum++;
}
}
}
cout << colornum;
}
'백준 알고리즘 > BFS' 카테고리의 다른 글
백준 2178번 C++ (0) | 2019.02.19 |
---|---|
백준 2589번 C++ (0) | 2019.01.14 |
백준 1389번 C++ (0) | 2019.01.13 |
백준 2644번 C++ (0) | 2019.01.12 |
백준 14502번 C++ (0) | 2019.01.10 |