(๋ฐฑ์ค/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 |
@DoctorSunAhna :: ํํฌ์ฝ๋ฅ
์ ํ๋ ๊ฒ ๋ณด๋ค ๋ซ๊ฒ ์ง
ํฌ์คํ ์ด ์ข์๋ค๋ฉด "์ข์์โค๏ธ" ๋๋ "๊ตฌ๋ ๐๐ป" ํด์ฃผ์ธ์!