반응형
https://www.acmicpc.net/problem/10026
10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
[ 문제풀이 ]
각각의 문자에 따라 구역을 0으로 바꿔주는 함수를 만들어서 적록색약이 아닌 사람이 볼 수 있는 구역의 수를 구해줍니다. 그리고 R과 G를 한 구역으로 찾는 함수를 하나 더 만들어주어서 적록색약인 사람이 볼 수 있는 구역의 수를 출력해줍니다.
[ 소스 코드 ]
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include<iostream> #include<queue> using namespace std; int N; int dy[4] = { -1,1,0,0 }; int dx[4] = { 0,0,-1,1 }; struct node { int y; int x; }; void bfs(int y, int x, char ch, char temp[][101]) { queue<node> q; q.push({ y,x }); temp[y][x] = 0; while (!q.empty()) { int y = q.front().y; int x = q.front().x; q.pop(); for (int i = 0; i < 4; i++) { int yy = y + dy[i]; int xx = x + dx[i]; if (yy >= 0 && yy < N && xx >= 0 && xx < N) { if (temp[yy][xx] == ch) { temp[yy][xx] = 0; q.push({ yy,xx }); } } } } } void bfsRG(int y, int x, char temp[][101]) { queue<node> q; q.push({ y,x }); temp[y][x] = 0; while (!q.empty()) { int y = q.front().y; int x = q.front().x; q.pop(); for (int i = 0; i < 4; i++) { int yy = y + dy[i]; int xx = x + dx[i]; if (yy >= 0 && yy < N && xx >= 0 && xx < N) { if (temp[yy][xx] == 'R' || temp[yy][xx] == 'G') { temp[yy][xx] = 0; q.push({ yy,xx }); } } } } } int main() { scanf("%d", &N); char arr[101][101]; char temp[101][101]; for (int i = 0; i < N; i++) { scanf("%s", arr[i]); } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { temp[i][j] = arr[i][j]; } } int cnt = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (temp[i][j] == 'R') { cnt++; bfs(i, j, 'R', temp); } else if (temp[i][j] == 'G') { cnt++; bfs(i, j, 'G', temp); } else if (temp[i][j] == 'B') { cnt++; bfs(i, j, 'B', temp); } } } printf("%d ", cnt); cnt = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (arr[i][j] == 'R' || arr[i][j] == 'G') { cnt++; bfsRG(i, j, arr); } else if (arr[i][j] == 'B') { cnt++; bfs(i, j, 'B', arr); } } } printf("%d", cnt); } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 7578번 - 공장 (C++) (0) | 2022.08.07 |
---|---|
[ 백준 ] 16287번 - Parcel (C++) (0) | 2022.08.06 |
[ 백준 ] 11438번 - LCA 2 (C++) (0) | 2022.08.04 |
[ 백준 ] 1725번 - 히스토그램 (C++) (0) | 2022.08.03 |
[ 백준 ] 6549번 - 히스토그램에서 가장 큰 직사각형 (C++) (0) | 2022.08.02 |