728x90
반응형
https://www.acmicpc.net/problem/10026
10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
int n;
char arr[100][100];
bool visited[100][100];
int dx[4] = { -1, 0, 0, 1 };
int dy[4] = { 0, -1, 1, 0 };
void bfs(int a, int b){
queue<pair<int, int>> q;
q.push({a, b});
visited[a][b] = true;
while (!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if (!(nx >= 0 && ny >= 0 && nx < n && ny < n)) continue;
if (visited[nx][ny] == false && arr[nx][ny] == arr[x][y]){
visited[nx][ny] = true;
q.push({nx, ny});
}
}
}
}
void bfs2(int a, int b){
queue<pair<int, int>> q;
q.push({a, b});
visited[a][b] = true;
while (!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if (!(nx >= 0 && ny >= 0 && nx < n && ny < n)) continue;
if(arr[x][y] == 'R' || arr[x][y] == 'G') {
if((arr[nx][ny] == 'R' || arr[nx][ny] == 'G') && !visited[nx][ny]){
visited[nx][ny] = true;
q.push({nx, ny});
}
}
if(arr[x][y] == 'B'){
if(!visited[nx][ny] && arr[nx][ny] == 'B'){
visited[nx][ny] = true;
q.push({nx, ny});
}
}
}
}
}
int main() {
cin >> n;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> arr[i][j];
}
}
int cnt1 = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!visited[i][j]) {
bfs(i, j);
cnt1++;
}
}
}
memset(visited, false, sizeof(visited));
int cnt2 = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(!visited[i][j]) {
bfs2(i, j);
cnt2++;
}
}
}
cout << cnt1 << " " << cnt2 << endl;
return 0;
}
728x90
반응형
'Algorithm > BAEKJOON' 카테고리의 다른 글
[BOJ] 1717번 집합의 표현 (C++) (0) | 2022.05.23 |
---|---|
[BOJ] 21940번 가운데에서 만나기 (C++) (0) | 2022.05.22 |
[BOJ] 22945번 팀 빌딩 (C++) (0) | 2022.05.18 |
[BOJ] 2467번 용액 (C++) (0) | 2022.05.17 |
[BOJ] 11055번 가장 큰 증가 부분 수열 (C++) (0) | 2022.05.16 |