-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaekJoon10026.cpp
More file actions
80 lines (74 loc) · 1.44 KB
/
BaekJoon10026.cpp
File metadata and controls
80 lines (74 loc) · 1.44 KB
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
75
76
77
78
79
80
//BaekJoon 10026 적록색약
#include <iostream>
#include <string>
#include <queue>
#include <utility>
using namespace std;
int n = 0;
int arr[100][100] = { 0, };
int arr2[100][100] = { 0, };
queue <pair<int, int>> qu;
int res = 0;
bool visited[100][100] = { false, };
pair<int, int> cur;
void isNear(int x, int y, int rgb[100][100]) {
if (x < 0 || y < 0 || x >= n || y >= n) return;
if (visited[x][y] == false && rgb[x][y] == rgb[cur.first][cur.second]) {
qu.push(make_pair(x, y));
visited[x][y] = true;
}
}
void bfs(int rgb[100][100]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = false;
}
}
res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
if (visited[i][j]) continue;
qu.push(make_pair(i, j));
visited[i][j] = true;
res++;
while (!qu.empty()) {
cur = qu.front();
qu.pop();
int a = cur.first;
int b = cur.second;
isNear(a + 1, b, rgb);
isNear(a, b - 1, rgb);
isNear(a, b + 1, rgb);
isNear(a - 1, b, rgb);
}
}
}
cout << res << " ";
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
char tmp;
cin >> tmp;
if (tmp == 'R') {
arr[i][j] = 0;
arr2[i][j] = 0;
}
else if (tmp == 'G') {
arr[i][j] = 1;
arr2[i][j] = 0;
}
else if (tmp == 'B') {
arr[i][j] = 2;
arr2[i][j] = 2;
}
}
}
bfs(arr);
bfs(arr2);
return 0;
}