-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1303.cpp
More file actions
76 lines (66 loc) · 1.58 KB
/
BOJ_1303.cpp
File metadata and controls
76 lines (66 loc) · 1.58 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
#include <bits/stdc++.h>
using namespace std;
int N, M;
char arr[101][101];
bool visited[101][101];
long long myTeam = 0;
long long enemy = 0;
queue<pair<int, int>> q;
int dx[4] = { -1, 1, 0, 0 };
int dy[4] = { 0, 0, 1, -1 };
void BFS(int row, int col, char color) {
int counter = 1;
q.push({ row, col });
visited[row][col] = true;
while (!q.empty()) {
int prevRow = q.front().first;
int prevCol = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int newRow = prevRow + dx[i];
int newCol = prevCol + dy[i];
if (color == 'W') {
if (newRow >= 0 && newRow < N && newCol >= 0 && newCol < M && visited[newRow][newCol] == false && arr[newRow][newCol] == 'W') {
q.push({ newRow, newCol });
visited[newRow][newCol] = true;
counter++;
}
}
else if (color == 'B') {
if (newRow >= 0 && newRow < N && newCol >= 0 && newCol < M && visited[newRow][newCol] == false && arr[newRow][newCol] == 'B') {
q.push({ newRow, newCol });
visited[newRow][newCol] = true;
counter++;
}
}
}
}
if (color == 'W') {
myTeam += (int)pow(counter, 2);
}
else if (color == 'B') {
enemy += (int)pow(counter, 2);
}
}
int main() {
cin >> M >> N;
for (int i = 0; i < N; i++) {
string input;
cin >> input;
for (int j = 0; j < M; j++) {
arr[i][j] = input[j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (visited[i][j] == false && arr[i][j] == 'W') {
BFS(i, j, 'W');
}
else if (visited[i][j] == false && arr[i][j] == 'B') {
BFS(i, j, 'B');
}
}
}
cout << myTeam << " " << enemy;
return 0;
}