-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10026.cpp
More file actions
96 lines (90 loc) · 2.14 KB
/
10026.cpp
File metadata and controls
96 lines (90 loc) · 2.14 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
91
92
93
94
95
96
#include <iostream>
#include <queue>
using namespace std;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int ograph[100][100];
int ngraph[100][100];
int visited[100][100];
int N;
void init(){
for(int i=0; i<N; i++)
fill_n(visited[i],N,0);
}
void NBFS(int sx,int sy){
queue<pair<int, int> > q;
q.push(pair<int,int>(sx,sy));
visited[sx][sy]=1;
while(!q.empty()){
int vx = q.front().first;
int vy = q.front().second;
q.pop();
for(int i=0; i<4; i++){
int xx = vx + dx[i];
int yy = vy + dy[i];
if(xx>=0&&xx<N&&yy>=0&&yy<N&&visited[xx][yy]==0&&ngraph[xx][yy]==ngraph[vx][vy]){
q.push(pair<int,int>(xx,yy));
visited[xx][yy]=1;
}
}
}
}
void OBFS(int sx,int sy){
queue<pair<int, int> > q;
q.push(pair<int,int>(sx,sy));
visited[sx][sy]=1;
while(!q.empty()){
int vx = q.front().first;
int vy = q.front().second;
q.pop();
for(int i=0; i<4; i++){
int xx = vx + dx[i];
int yy = vy + dy[i];
if(xx>=0&&xx<N&&yy>=0&&yy<N&&visited[xx][yy]==0&&ograph[xx][yy]==ograph[vx][vy]){
q.push(pair<int,int>(xx,yy));
visited[xx][yy]=1;
}
}
}
}
int main(){
cin >> N;
string a;
int cntn =0;
int cnto =0;
for(int i=0; i<N;i++){
cin >>a;
for(int j=0; j<N;j++){
if(a[j]=='B'){
ograph[i][j]=1;
ngraph[i][j]=1;
}
else if(a[j]=='R'){
ograph[i][j]=2;
ngraph[i][j]=3;
}
else{
ograph[i][j]=2;
ngraph[i][j]=2;
}
}
}
for(int i=0; i<N; i++){
for (int j=0; j<N; j++){
if(visited[i][j]==0){
NBFS(i,j);
cntn++;
}
}
}
init();
for(int i=0; i<N; i++){
for (int j=0; j<N; j++){
if(visited[i][j]==0){
OBFS(i,j);
cnto++;
}
}
}
cout << cntn << " " << cnto;
}