728x90

이 문제는 기본적인 DFS를 활용한 땅 갯수 구하기와 비슷한 문제이다. 문제에서 정상인이 보이는 색과 적록색약이 보이는 색이 다르다. 따라서 각각 정상인 경우와 적록색약인 경우로 나누어 각각 DFS를 돌리면 된다.
#include<iostream>
#include<queue>
#include<string>
#include<cstring>
using namespace std;
char rgb[102][102];
char rb[102][102];
bool visited[102][102];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
int n;
int rgbCnt = 0, rbCnt = 0;
void dfs(int x, int y, char arr[][102],char c)
{
if(visited[x][y])
{
return;
}
visited[x][y] = true;
for(int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx<1 || nx>n || ny<1 || ny>n || visited[nx][ny]|| arr[nx][ny] != c)
{
continue;
}
dfs(nx, ny, arr,c);
}
}
void solution()
{
memset(rgb, 'x', sizeof(rgb));
memset(rb, 'x', sizeof(rb));
for(int i = 1; i <= n; i++)
{
string s;
cin >> s;
for(int j = 0; j < s.size(); j++)
{
if(s[j] == 'G')
{
rgb[i][j+1] = 'G';
rb[i][j+1] = 'R';
}
else
{
rgb[i][j+1] = s[j];
rb[i][j+1] = s[j];
}
}
}
memset(visited, false, sizeof(visited));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(!visited[i][j])
{
dfs(i, j, rgb, rgb[i][j]);
rgbCnt++;
}
}
}
memset(visited, false, sizeof(visited));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(!visited[i][j])
{
dfs(i, j, rb, rb[i][j]);
rbCnt++;
}
}
}
cout << rgbCnt << " " << rbCnt;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
solution();
}
반응형
'알고리즘 > boj' 카테고리의 다른 글
BOJ 5525 IOIOI (0) | 2023.03.07 |
---|---|
BOJ 5430 AC (0) | 2023.03.07 |
BOJ 9019 DSLR (0) | 2023.02.26 |
BOJ 1992 쿼드트리 (0) | 2023.02.22 |
BOJ 1389 케빈 베이컨의 6단계 법칙 (0) | 2023.02.21 |