[백준 10026] 적록색약

woonchoi·2022년 1월 14일
0

백준_그래프 탐색

목록 보기
3/3

문제 : 적록색약

그냥 조건이 다른 DFS를 두번 돌리면 된다.

함수나 visited배열은 두개씩 쓸 필요는 없지만 귀찮아서 따로따로 만들었다..

#include <iostream>
#include <vector>

using namespace std;

int		n;
char	pic[101][101];
int		visited[100][100];
int		visited_rg[100][100];
int		count;
int		count_rg;
int		dx[4] = {1, -1, 0, 0};
int		dy[4] = {0, 0, 1, -1};

void	DFS(int	y, int x, char c)
{
	int		i;
	int		nx, ny;
	
	visited[y][x] = true;
	i = 0;
	while (i < 4)
	{
		nx = dx[i] + x;
		ny = dy[i] + y;
		if (0 <= nx && nx < n && 0 <= ny && ny < n)
		{
			if (pic[ny][nx] == c && !visited[ny][nx])
				DFS(ny, nx, c);
		}
		i++;
	}
}

void	DFS_rg(int	y, int x, char c)
{
	int		i;
	int		nx, ny;
	
	visited_rg[y][x] = true;
	i = 0;
	while (i < 4)
	{
		nx = dx[i] + x;
		ny = dy[i] + y;
		if (0 <= nx && nx < n && 0 <= ny && ny < n)
		{
			if (c == 'R' || c == 'G')
			{
				if ((pic[ny][nx] == 'R' || pic[ny][nx] == 'G') && !visited_rg[ny][nx])
					DFS_rg(ny, nx, c);
			}
			else
			{
				if (pic[ny][nx] == 'B' && !visited_rg[ny][nx])
					DFS_rg(ny, nx, c);
			}
		}
		i++;
	}
}

int		main(void)
{
	int		x, y;
	
	scanf("%d", &n);
	y = 0;
	while (y < n)
		scanf("%s", pic[y++]);
	y = 0;
	while (y < n)
	{
		x = 0;
		while (x < n)
		{
			if (!visited[y][x])
			{
				DFS(y, x, pic[y][x]);
				count++;
			}
			if (!visited_rg[y][x])
			{
				DFS_rg(y, x, pic[y][x]);
				count_rg++;
			}
			x++;
		}
		y++;
	}
	printf("%d %d\n", count, count_rg);
	return (0);
}

profile
개발공부

0개의 댓글