(백준/c++) 10026 - 적록색약 / BFS, 그래프📃 coding test/◽ 백준2024. 8. 20. 16:12
Table of Contents
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
'📃 coding test > ◽ 백준' 카테고리의 다른 글
(백준/c++) 1987 - 알파벳/ DFS, 백트래킹 (0) | 2024.08.27 |
---|---|
(백준/c++) 1238 - 파티 / 최단경로그래프, 플로이드 워샬 (0) | 2024.08.22 |
(백준/c++) 11403 - 경로찾기 / BFS, 그래프 (0) | 2024.08.16 |
(백준/c++) 11724 - 연결 요소의 개수 / BFS, 그래프 (0) | 2024.08.16 |
(백준/c++) 14940 - 쉬운 최단 거리 / DFS, 그래프 (0) | 2024.08.16 |
@핑크코냥 :: 핑크코냥
안 하는 것 보다 낫겠지
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!