-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1303.cpp
40 lines (40 loc) · 896 Bytes
/
1303.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
#include <bits/stdc++.h>
using namespace std;
int n, m, w, b;
char arr[100][100];
bool vis[100][100];
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};
int main() {
cin >> n >> m;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(vis[i][j]) continue;
char team = arr[i][j];
int cnt = 1;
vis[i][j] = 1;
queue<pair<int,int>> q;
q.push({i,j});
while(!q.empty()) {
auto cur = q.front(); q.pop();
for(int k = 0; k < 4; k++) {
int fx = cur.first + dx[k];
int fy = cur.second + dy[k];
if(fx < 0 || fy < 0 || fx >= m || fy >= n) continue;
if(vis[fx][fy] || arr[fx][fy] != team) continue;
cnt++;
vis[fx][fy] = 1;
q.push({fx,fy});
}
}
if(team == 'B') b += cnt * cnt;
else w += cnt * cnt;
}
}
cout << w << ' ' << b;
}