[백준] 적록색약 10026

Soohyeon B·2022년 11월 23일
0

알고리즘 문제 풀이

목록 보기
53/70

문제

  • N*N 칸에 R,G,B가 칠해져있다.
  • 구역은 같은 색으로 이루어져있다.
  • 적록색약은 빨간색과 초록색을 구분하지 못한다.
  • 그림이 주어졌을 때 적록색약과 비 적록색약인 사람이 보는 구역의 수를 출력하시오.

풀이

풀이 1

예제 입력
5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

예제 출력
4 3

그림이랑 비슷한 문제

#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second

char board[102][102]; 
int dist[102][102];
int dist2[102][102];

int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    
    int cb=0, non_cb=0;
    
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin >> board[i][j];
        }
    }

   
    
    //색맹 아닌 사람
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(dist[i][j]!=0) continue;
            cb++;
            
             //시작하는 칸 큐에 넣기
            queue<pair<int, int>> Q;
            Q.push({i,j});
            dist[i][j]=1;
            
            while(!Q.empty()){
                auto cur = Q.front();
                Q.pop();
                
                for(int dir=0; dir<4; dir++){
                    int nx = cur.X+dx[dir];
                    int ny = cur.Y+dy[dir];
                    
                    if(nx >=n||nx<0||ny>=n||ny<0) continue; //범위를 벗어난 경우
                    //방문했거나 현재 색과 다음 색이 다를 경우 pass
                    if(dist[nx][ny] || board[cur.X][cur.Y]!= board[nx][ny]) continue; 
                    
                    dist[nx][ny]= 1;
                    Q.push({nx, ny}); 
                }
            }
        }
    }
 
    //G를 R로 바꾸기 
     for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(board[i][j]=='G')
                board[i][j]= 'R';
        }
     }
    for(int i = 0; i < n; i++)
        fill(dist[i], dist[i]+n, 0);
        
    //색맹인사람
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if(dist[i][j]!=0) continue;
            non_cb++;
            
            //시작하는 칸 큐에 넣기
            queue<pair<int, int>> Q;
            Q.push({i,j});
            dist[i][j]=1;
            
            while(!Q.empty()){
                auto cur = Q.front();
                Q.pop();
                
                for(int dir=0; dir<4; dir++){
                    int nx = cur.X+dx[dir];
                    int ny = cur.Y+dy[dir];
                    
                    if(nx >=n||nx<0||ny>=n||ny<0) continue; //범위를 벗어난 경우
                    //방문했거나 현재 색과 다음 색이 다를 경우 pass
                    if(dist[nx][ny] || board[cur.X][cur.Y]!= board[nx][ny]) continue; 
                    
                    dist[nx][ny]= 1;
                    Q.push({nx, ny}); 
                }
            }
        }
    }
    
            
    cout << cb<<' '<<non_cb;
    
    
    return 0;
}
profile
하루하루 성장하는 BE 개발자

0개의 댓글