|
| 1 | +# 문제 |
| 2 | +적록색약 |
| 3 | +## 문제 원본 |
| 4 | +문제의 원본은 [여기서](https://www.acmicpc.net/problem/10026) 확인하세요. |
| 5 | + |
| 6 | +## 분류 |
| 7 | +* DFS 구현 |
| 8 | + |
| 9 | +# 풀이 |
| 10 | + |
| 11 | +단순 DFS를 이용하여 영역의 수를 구한다. |
| 12 | +하지만 이문제에서는 적록색약의 시선에서 적색과 녹색의 구분이 없기 떄문에 정상인 경우와 적록색약인 경우의 시선에서 같은 색 인지 아닌지의 여부를 판별하는 함수를 이용한다. |
| 13 | + |
| 14 | + |
| 15 | +``` c++ |
| 16 | +#include <iostream> |
| 17 | +#include <cstring> |
| 18 | + |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +enum class Color { |
| 22 | + R = 'R', G = 'G', B = 'B' |
| 23 | +}; |
| 24 | + |
| 25 | +enum class Condition { |
| 26 | + NORMAL, WEAKNESS |
| 27 | +}; |
| 28 | + |
| 29 | +Color map[100][100]; |
| 30 | +bool visited[100][100]; |
| 31 | + |
| 32 | +bool isSameColor(Color a, Color b, Condition condition) { |
| 33 | + if (condition == Condition::NORMAL) { |
| 34 | + return a == b; |
| 35 | + } |
| 36 | + else if (condition == Condition::WEAKNESS) { |
| 37 | + if ((a == b) || (a == Color::R && b == Color::G) || (a == Color::G && b == Color::R)) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + else { |
| 41 | + return false; |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void dfs(int i, int j, int n, Condition condition) { |
| 47 | + visited[i][j] = true; |
| 48 | + |
| 49 | + if ((j + 1) < n && !visited[i][j + 1] && isSameColor(map[i][j], map[i][j + 1], condition)) { |
| 50 | + dfs(i, j + 1, n, condition); |
| 51 | + } |
| 52 | + if ((j - 1) >= 0 && !visited[i][j - 1] && isSameColor(map[i][j], map[i][j - 1], condition)) { |
| 53 | + dfs(i, j - 1, n, condition); |
| 54 | + } |
| 55 | + if ((i + 1) < n && !visited[i + 1][j] && isSameColor(map[i][j], map[i + 1][j], condition)) { |
| 56 | + dfs(i + 1, j, n, condition); |
| 57 | + } |
| 58 | + if ((i - 1) >= 0 && !visited[i - 1][j] && isSameColor(map[i][j], map[i - 1][j], condition)) { |
| 59 | + dfs(i - 1, j, n, condition); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +int main(void) { |
| 64 | + ios::sync_with_stdio(false); |
| 65 | + cin.tie(0); cout.tie(0); |
| 66 | + |
| 67 | + int n; |
| 68 | + cin >> n; |
| 69 | + |
| 70 | + char c; |
| 71 | + Color col; |
| 72 | + for (int i = 0; i < n; i++) { |
| 73 | + for (int j = 0; j < n; j++) { |
| 74 | + cin >> c; |
| 75 | + map[i][j] = (Color)c; |
| 76 | + } |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + int normalCount = 0; |
| 81 | + int weakCount = 0; |
| 82 | + |
| 83 | + |
| 84 | + for (int i = 0; i < n; i++) { |
| 85 | + for (int j = 0; j < n; j++) { |
| 86 | + if (!visited[i][j]) { |
| 87 | + dfs(i, j, n, Condition::NORMAL); |
| 88 | + normalCount++; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + memset(visited, false, sizeof(visited)); |
| 94 | + |
| 95 | + for (int i = 0; i < n; i++) { |
| 96 | + for (int j = 0; j < n; j++) { |
| 97 | + if (!visited[i][j]) { |
| 98 | + dfs(i, j, n, Condition::WEAKNESS); |
| 99 | + weakCount++; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + cout << normalCount << " " << weakCount; |
| 105 | + return 0; |
| 106 | +} |
| 107 | +``` |
0 commit comments