📃 coding test/◽ 백준

(백준/c++) 10026 - 적록색약 / BFS, 그래프

핑크코냥 2024. 8. 20. 16:12
728x90

"(백준/c++) 10026 - 적록색약 / BFS, 그래프"


🏆 solved.ac 난이도: 골드5 

 

#include<iostream>
#include<queue>
using namespace std;
char grid[101][101];
bool visited[101][101] = {false,};
bool visitesd[101][101] = { false, };
int direction[4][2] = { {-1, 0} , {0, 1}, {1, 0}, {0 ,-1} };
int mTC;
int normalCount = 0;
int blindnessCount = 0;
queue<pair<int, int>> normalPerson; 
queue<char> colorblindness;

void normalFunc(int i, int j);
void bilndnessFunc(int i, int j);

int main(void)
{
	cin >> mTC;

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

	

	for (int i = 0; i < mTC; ++i)
	{
		for (int j = 0; j < mTC; ++j)
		{
			normalFunc(i, j);
			bilndnessFunc(i, j);
		}
	}

	cout << normalCount << " " << blindnessCount;
	return 0;
}

void normalFunc(int i, int j)
{
	if (visited[i][j] == false)
		normalPerson.push(make_pair(i, j));
	else return;

	normalCount++;

	while (!normalPerson.empty())
	{
		auto tmep = normalPerson.front();
		visited[tmep.first][tmep.second] = true;
		normalPerson.pop();

		for (int k = 0; k < 4; ++k)
		{
			int x = tmep.first + direction[k][0];
			int y = tmep.second + direction[k][1];

			if (grid[x][y] == grid[tmep.first][tmep.second]
				&& visited[x][y] == false
				&& x >= 0 && x < mTC && y >= 0 && y < mTC)
			{
				normalPerson.push(make_pair(x, y));
				visited[x][y] = true;
			}
		}
	}
}

void bilndnessFunc(int i, int j)
{
	if (visitesd[i][j] == false)
		normalPerson.push(make_pair(i, j));
	else return;

	blindnessCount++;

	while (!normalPerson.empty())
	{
		auto tmep = normalPerson.front();

		visitesd[tmep.first][tmep.second] = true;
		normalPerson.pop();

		for (int k = 0; k < 4; ++k)
		{
			int x = tmep.first + direction[k][0];
			int y = tmep.second + direction[k][1];

			if ((grid[x][y] == grid[tmep.first][tmep.second]
				|| (grid[x][y] == 'R' && grid[tmep.first][tmep.second] == 'G')
				|| (grid[x][y] == 'G' && grid[tmep.first][tmep.second] == 'R'))
				&& visitesd[x][y] == false
				&& x >= 0 && x < mTC && y >= 0 && y < mTC)
			{
				normalPerson.push(make_pair(x, y));
				visitesd[x][y] = true;
			}
		}
	}
}
728x90