Algorithm/BOJ
[BOJ]10026 - 적록색약(dfs)
Ellery
2021. 7. 3. 11:49
https://www.acmicpc.net/problem/10026
10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
- 쉬운 문제라 정리 안하려다가 타 블로그 해설을 보면 너무 어렵게 푸는 거 같아서 올려둠
- 모든 칸에서 dfs를 하면서 구간을 카운트하면 되고 적록색약인 케이스는 입력받은 맵을 수정해서(R -> G) 다시 dfs를 돌면 너무나도 쉽게 풀린다
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
int n, ans = 0;
string a[101];
bool check[101][101];
void dfs(int x, int y) {
check[x][y] = true;
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 > nx || 0 > ny || nx >= n || ny >= n) continue;
if (a[x][y] != a[nx][ny] || check[nx][ny]) continue;
dfs(nx, ny);
}
}
void dfsLoop() {
memset(check, false, sizeof(check));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (!check[i][j]) {
dfs(i, j);
ans++;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
dfsLoop();
cout << ans << ' ';
ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'R') a[i][j] = 'G';
}
}
dfsLoop();
cout << ans;
return 0;
}