-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10026_bfs.cpp
100 lines (99 loc) · 2.08 KB
/
10026_bfs.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// 200819 #10026 Gold V
// bfs 또는 dfs 이용
// 그래 이게 골5지, 다른 건 골5 치고 어려워
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int dx[4] = { 0,0,-1,1 }; // 상하좌우
int dy[4] = { -1,1,0,0 };
bool visit[100][100];
char a[100][100]; // 정상
char b[100][100]; // R=G
int N, acnt = 0, bcnt = 0; // 정상, 적록색약
// R!=G
void bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({ x,y });
visit[y][x] = true;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < N && ny < N) {
if (!visit[ny][nx] && a[ny][nx] == a[y][x]) {
visit[ny][nx] = true;
q.push({ nx, ny });
}
}
}
}
}
// R=G
void bfs2(int x, int y) {
queue<pair<int, int>> q;
q.push({ x,y });
visit[y][x] = true;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && ny >= 0 && nx < N && ny < N) {
// 다른 건 여기 뿐
if (!visit[ny][nx]&&b[ny][nx]==b[y][x]) {
visit[ny][nx] = true;
q.push({ nx, ny });
}
}
}
}
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
}
// 적록색약은 G랑 R이랑 같으므로 G를 R로 바꿈
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (a[i][j] == 'G')
b[i][j] = 'R';
else
b[i][j] = a[i][j];
}
}
// 방문하지 않은 곳 있으면 마저 탐색
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visit[i][j]) {
acnt++;
bfs(j, i);
}
}
}
// 다시 visit배열 원래대로
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
visit[i][j] = false;
}
}
// 이제 적록색약 구역 탐색 가자
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!visit[i][j]) {
bcnt++;
bfs2(j, i);
}
}
}
cout << acnt << " " << bcnt;
}