알고리즘/DFS & BFS
[백준] 10026 적록색약
HJ39
2023. 5. 7. 22:06
약간 새로운 패턴의 문제였다.
그냥 약간의 노가다(?) 문제
일반인의 덩어리를 세는 부분은 R,G,B를 모두 나누어 개수를 세면 되지만
적록색약의 덩어리를 세는 부분은 (R,G), B를 나누어 개수를 세면 끝이다
쉽죠?
□ 소스코드
#include <bits/stdc++.h>
using namespace std;
vector<vector<char>> input10026(101,vector<char>(101,0));
vector<vector<bool>> check10026(101,vector<bool>(101,false));
vector<vector<bool>> checkNotRG10026(101,vector<bool>(101,false));
bool dfsBlue10026(int N, int x, int y){
if(x<1 || y<1 || x>N || y>N)
return false;
if(input10026[x][y] == 'B' && !check10026[x][y] ){
check10026[x][y] = true;
dfsBlue10026(N, x+1, y);
dfsBlue10026(N, x-1, y);
dfsBlue10026(N, x, y-1);
dfsBlue10026(N, x, y+1);
return true;
}
return false;
}
bool dfsGreenRed10026(int N, int x, int y){
if(x<1 || y<1 || x>N || y>N)
return false;
if((input10026[x][y] == 'G' || input10026[x][y] == 'R') && !check10026[x][y]){
check10026[x][y] = true;
dfsGreenRed10026(N, x+1, y);
dfsGreenRed10026(N, x-1, y);
dfsGreenRed10026(N, x, y-1);
dfsGreenRed10026(N, x, y+1);
return true;
}
return false;
}
bool dfsGreen10026(int N, int x, int y){
if(x<1 || y<1 || x>N || y>N)
return false;
if(input10026[x][y] == 'G' && !checkNotRG10026[x][y] ){
checkNotRG10026[x][y] = true;
dfsGreen10026(N, x+1, y);
dfsGreen10026(N, x-1, y);
dfsGreen10026(N, x, y-1);
dfsGreen10026(N, x, y+1);
return true;
}
return false;
}
bool dfsRed10026(int N, int x, int y){
if(x<1 || y<1 || x>N || y>N)
return false;
if(input10026[x][y] == 'R' && !checkNotRG10026[x][y] ){
checkNotRG10026[x][y] = true;
dfsRed10026(N, x+1, y);
dfsRed10026(N, x-1, y);
dfsRed10026(N, x, y-1);
dfsRed10026(N, x, y+1);
return true;
}
return false;
}
void green10026(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N; cin>>N;
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
char a; cin>>a;
input10026[i][j] = a;
}
}
int notRGMedicine = 0;
int rgMedicine = 0;
int blueColor = 0;
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
if(dfsBlue10026(N, i, j)){
rgMedicine++;
blueColor++;
}
if(dfsGreenRed10026(N, i, j))
rgMedicine++;
if(dfsRed10026(N, i, j))
notRGMedicine++;
if(dfsGreen10026(N, i, j))
notRGMedicine++;
}
}
cout<<notRGMedicine + blueColor <<" "<<rgMedicine<<"\n";
}