10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
풀이과정
적록색약은 R과 G의 차이를 느끼지 못하므로 하나의 색상으로 인식하여 cnt를 해야한다. R, G를 같이 세는 RGcnt로 변수를 하나 선언해주고 check를 true로 바꿔주며 num[i][j]가 R 또는 G일 때 RGcnt를 카운트 해주었다. 적록색약인 사람이 봤을 때는 Rcnt + Gcnt + Bcnt, 아닌 사람이 봤을 때는 RGcnt + Bcnt을 출력한다.
#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
int N, Rcnt, Gcnt, Bcnt, RGcnt;
char num[101][101];
bool c[101][101];
bool check = 0; // 적록색약 아닌 사람
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, -1, 0, 1 };
queue <pair <int, int>> q;
void bfs(int x, int y, char kind) {
q.push({ x, y });
c[x][y] = 1;
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 < 1 || nx > N || ny < 1 || ny > N)
continue;
// 적록색약 아닌 사람
if (!check && num[nx][ny] == kind && !c[nx][ny]) {
q.push({ nx, ny });
c[nx][ny] = 1;
}
// 적록색약인 사람
if (check && (num[nx][ny] == kind || num[nx][ny] == 'G') && !c[nx][ny]) {
q.push({ nx, ny });
c[nx][ny] = 1;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
cin >> num[i][j];
}
}
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (num[i][j] == 'R' && !c[i][j]) {
bfs(i, j, 'R');
Rcnt++;
}
else if (num[i][j] == 'G' && !c[i][j]) {
bfs(i, j, 'G');
Gcnt++;
}
else if (num[i][j] == 'B' && !c[i][j]) {
bfs(i, j, 'B');
Bcnt++;
}
}
}
memset(c, 0, sizeof(c));
check = 1; // 적록색약
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if ((num[i][j] == 'R' || num[i][j] == 'G') && !c[i][j]) {
bfs(i, j, 'R');
RGcnt++;
}
}
}
cout << Rcnt + Gcnt + Bcnt << " " << RGcnt + Bcnt;
}
'🍞 Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 1197 최소 스패닝 트리 c++ (0) | 2022.09.02 |
---|---|
[백준] 17396 백도어 c++ (0) | 2022.09.02 |
[백준] 10757 큰 수 A+B c++ (0) | 2022.09.02 |
[백준] 11657 타임머신 c++ (0) | 2022.09.02 |
[백준] 13565 침투 c++ (0) | 2022.09.02 |