728x90
BFS로 풀었다.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n;
int main() {
cin >> n;
vector<vector<char>> v(n);
vector<vector<bool>> visited(n);
for (int i = 0; i < n; i++) {
v[i] = vector<char>(n);
visited[i] = vector<bool>(n);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> v[i][j];
}
}
int normalCount = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j]) {
normalCount++;
if (v[i][j] == 'R') {
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (!visited[cur.first][cur.second]) {
visited[cur.first][cur.second] = true;
if (cur.first + 1 < n && !visited[cur.first + 1][cur.second] && v[cur.first + 1][cur.second] == 'R') {
q.push(make_pair(cur.first + 1, cur.second));
}
if (cur.first - 1 >= 0 && !visited[cur.first - 1][cur.second] && v[cur.first - 1][cur.second] == 'R') {
q.push(make_pair(cur.first - 1, cur.second));
}
if (cur.second + 1 < n && !visited[cur.first][cur.second + 1] && v[cur.first][cur.second + 1] == 'R') {
q.push(make_pair(cur.first, cur.second + 1));
}
if (cur.second - 1 >= 0 && !visited[cur.first][cur.second - 1] && v[cur.first][cur.second - 1] == 'R') {
q.push(make_pair(cur.first, cur.second - 1));
}
}
}
}
else if (v[i][j] == 'G') {
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (!visited[cur.first][cur.second]) {
visited[cur.first][cur.second] = true;
if (cur.first + 1 < n && !visited[cur.first + 1][cur.second] && v[cur.first + 1][cur.second] == 'G') {
q.push(make_pair(cur.first + 1, cur.second));
}
if (cur.first - 1 >= 0 && !visited[cur.first - 1][cur.second] && v[cur.first - 1][cur.second] == 'G') {
q.push(make_pair(cur.first - 1, cur.second));
}
if (cur.second + 1 < n && !visited[cur.first][cur.second + 1] && v[cur.first][cur.second + 1] == 'G') {
q.push(make_pair(cur.first, cur.second + 1));
}
if (cur.second - 1 >= 0 && !visited[cur.first][cur.second - 1] && v[cur.first][cur.second - 1] == 'G') {
q.push(make_pair(cur.first, cur.second - 1));
}
}
}
}
else if (v[i][j] == 'B') {
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (!visited[cur.first][cur.second]) {
visited[cur.first][cur.second] = true;
if (cur.first + 1 < n && !visited[cur.first + 1][cur.second] && v[cur.first + 1][cur.second] == 'B') {
q.push(make_pair(cur.first + 1, cur.second));
}
if (cur.first - 1 >= 0 && !visited[cur.first - 1][cur.second] && v[cur.first - 1][cur.second] == 'B') {
q.push(make_pair(cur.first - 1, cur.second));
}
if (cur.second + 1 < n && !visited[cur.first][cur.second + 1] && v[cur.first][cur.second + 1] == 'B') {
q.push(make_pair(cur.first, cur.second + 1));
}
if (cur.second - 1 >= 0 && !visited[cur.first][cur.second - 1] && v[cur.first][cur.second - 1] == 'B') {
q.push(make_pair(cur.first, cur.second - 1));
}
}
}
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
visited[i][j] = false;
if (v[i][j] == 'G')
v[i][j] = 'R';
}
}
int abnormalCount = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (!visited[i][j]) {
abnormalCount++;
if (v[i][j] == 'R') {
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (!visited[cur.first][cur.second]) {
visited[cur.first][cur.second] = true;
if (cur.first + 1 < n && !visited[cur.first + 1][cur.second] && v[cur.first + 1][cur.second] == 'R') {
q.push(make_pair(cur.first + 1, cur.second));
}
if (cur.first - 1 >= 0 && !visited[cur.first - 1][cur.second] && v[cur.first - 1][cur.second] == 'R') {
q.push(make_pair(cur.first - 1, cur.second));
}
if (cur.second + 1 < n && !visited[cur.first][cur.second + 1] && v[cur.first][cur.second + 1] == 'R') {
q.push(make_pair(cur.first, cur.second + 1));
}
if (cur.second - 1 >= 0 && !visited[cur.first][cur.second - 1] && v[cur.first][cur.second - 1] == 'R') {
q.push(make_pair(cur.first, cur.second - 1));
}
}
}
}
else if (v[i][j] == 'B') {
queue<pair<int, int>> q;
q.push(make_pair(i, j));
while (!q.empty()) {
pair<int, int> cur = q.front();
q.pop();
if (!visited[cur.first][cur.second]) {
visited[cur.first][cur.second] = true;
if (cur.first + 1 < n && !visited[cur.first + 1][cur.second] && v[cur.first + 1][cur.second] == 'B') {
q.push(make_pair(cur.first + 1, cur.second));
}
if (cur.first - 1 >= 0 && !visited[cur.first - 1][cur.second] && v[cur.first - 1][cur.second] == 'B') {
q.push(make_pair(cur.first - 1, cur.second));
}
if (cur.second + 1 < n && !visited[cur.first][cur.second + 1] && v[cur.first][cur.second + 1] == 'B') {
q.push(make_pair(cur.first, cur.second + 1));
}
if (cur.second - 1 >= 0 && !visited[cur.first][cur.second - 1] && v[cur.first][cur.second - 1] == 'B') {
q.push(make_pair(cur.first, cur.second - 1));
}
}
}
}
}
}
}
cout << normalCount << endl;
cout << abnormalCount << endl;
return 0;
}
728x90
'알고리즘 문제' 카테고리의 다른 글
[백준] 2583번 영역 구하기 (0) | 2020.02.06 |
---|---|
[백준] 2468번 안전영역 (0) | 2020.02.06 |
[백준] 4153번 직각삼각형 (0) | 2020.02.05 |
[백준] 3009번 네 번째 점 (0) | 2020.02.05 |
[백준] 10845번 큐 (0) | 2020.02.05 |