-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (89 loc) · 2.45 KB
/
Copy pathmain.cpp
File metadata and controls
90 lines (89 loc) · 2.45 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
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <queue>
#define CB_NORMAL 1
#define CB_ABNORMAL 0
using namespace std;
int n;
char current_colour;
char map[105][105];
bool visited[101][101];
int dr[] = {0, 0, 1, -1};
int dc[] = {1, -1, 0, 0};
queue<pair<int,int>> q;
void bfs(int condition) {
while(!q.empty()) {
int r = q.front().first;
int c = q.front().second;
q.pop();
for(int i=0; i<4; i++) {
int nr = r + dr[i];
int nc = c + dc[i];
if(0 <= nr && nr < n && 0 <= nc && nc < n) {
if(visited[nr][nc]) continue;
if(condition == CB_NORMAL) {
if(map[nr][nc] == current_colour) {
visited[nr][nc] = true;
q.push(make_pair(nr,nc));
}
} else { // 비정상
if(current_colour == 'B') { // 현재 파란색일경우
if(map[nr][nc] == 'B') {
visited[nr][nc] = true;
q.push(make_pair(nr,nc));
}
} else {
if(map[nr][nc] == 'R' || map[nr][nc] == 'G') {
visited[nr][nc] = true;
q.push(make_pair(nr,nc));
}
}
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(); cout.tie();
cin >> n;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
cin >> map[i][j];
}
}
// 정상인
int area = 0;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(visited[i][j] == false) {
visited[i][j] = true;
q.push(make_pair(i,j));
current_colour = map[i][j];
bfs(CB_NORMAL);
area++;
}
}
}
cout << area << ' ';
// 초기화
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
visited[i][j] = false;
}
}
// 비정상
area = 0;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if(visited[i][j] == false) {
visited[i][j] = true;
q.push(make_pair(i,j));
current_colour = map[i][j];
bfs(CB_ABNORMAL);
area++;
}
}
}
cout << area << '\n';
return 0;
}