[백준 10026] 적록색약

rhkr9080·2023년 7월 7일
0

BOJ(백준)

목록 보기
11/19

문제링크 : https://www.acmicpc.net/problem/10026

💻 문제 풀이 : C++

#include <iostream>
#include <queue>
#include <cstring>

#define MAP_SIZE 105

using namespace std;

struct Coord {
	int row;
	int col;
};

int N;
char MAP[MAP_SIZE][MAP_SIZE];
int visited[MAP_SIZE][MAP_SIZE];

int dr[] = { 0, 0, -1, 1 };
int dc[] = { -1, 1, 0, 0 };

void bfs(Coord start, int flag)
{
	queue<Coord> nowQ;
	nowQ.push(start);
	while (!nowQ.empty())
	{
		Coord now = nowQ.front();
		nowQ.pop();
		for (int i = 0; i < 4; i++)
		{
			Coord next = { now.row + dr[i], now.col + dc[i] };
			if (next.row < 0 || next.col < 0 || next.row >= N || next.col >= N) continue;
			if (flag == 0)
			{
				if (MAP[next.row][next.col] != MAP[start.row][start.col]) continue;
			}
			else if (flag == 1)
			{
				if (MAP[start.row][start.col] == 'B' && MAP[next.row][next.col] != 'B') continue;
				if (MAP[start.row][start.col] != 'B' && MAP[next.row][next.col] == 'B') continue;
			}
			if (visited[next.row][next.col] != 0) continue;
			visited[next.row][next.col] = 1;
			nowQ.push(next);
		}
	}
}

int main()
{
	cin >> N;
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			cin >> MAP[i][j];
	
	int cnt1 = 0, cnt2 = 0;

	// 비 적록색약 탐색
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (visited[i][j] != 1)
			{
				cnt1 += 1;
				bfs({ i, j }, 0);
			}
		}
	}

	memset(visited, 0, sizeof(visited));

	// 적록색약 탐색
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < N; j++)
		{
			if (visited[i][j] != 1)
			{
				cnt2 += 1;
				bfs({ i, j }, 1); 
			}
		}
	}

	cout << cnt1 << " " << cnt2 << endl;

	return 0;
}

📌 memo 😊


ref)

profile
공부방

0개의 댓글