백준 10026번 적록색약

빵어 ㅣ 2023. 3. 16. 20:01

#include <iostream>
#include <vector>

using namespace std;

int n;
char graph[101][101];
bool visited[101][101];
char lastColor;

void dfs(int x, int y) {
	if (x > n || x < 0 || y > n || y < 0
		|| graph[x][y] != lastColor
		|| visited[x][y])
		return;

	visited[x][y] = true;

	dfs(x - 1, y);
	dfs(x, y - 1);
	dfs(x + 1, y);
	dfs(x, y + 1);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> n;
	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j) {
			cin >> graph[i][j];
		}

	int result = 0, rgResult = 0;	// rgResult: 적록색약

	for (int i = 0; i < n; ++i){
		for (int j = 0; j < n; ++j) {
			if (!visited[i][j]) {
				lastColor = graph[i][j];
				dfs(i, j);
				++result;
			}
		}
	}

	for (int i = 0; i < n; ++i)
		for (int j = 0; j < n; ++j) {
			visited[i][j] = false;
			if (graph[i][j] == 'R')
				graph[i][j] = 'G';
		}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			if (!visited[i][j]) {
				lastColor = graph[i][j];
				dfs(i, j);
				++rgResult;
			}
		}
	}

	cout << result << " " << rgResult;

	return 0;
}

https://www.acmicpc.net/problem/10026

 

10026번: 적록색약

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)

www.acmicpc.net

'알고리즘 공부' 카테고리의 다른 글

백준 7576번 토마토  (0) 2023.03.16
백준 2178번 미로 탐색  (0) 2023.03.16
백준 11724번 연결 요소의 개수  (0) 2023.03.13
백준 1012번 유기농 배추  (0) 2023.03.10
백준 2606번 바이러스  (0) 2023.03.07