관리 메뉴

민우의 코딩노트

10026번 적록색약 - C++ 본문

Algorithm/BOJ

10026번 적록색약 - C++

미미누 2022. 7. 31. 22:26

✅️[문제]
https://www.acmicpc.net/problem/10026

10026번: 적록색약

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)

www.acmicpc.net


✅️[풀이]

BFS 문제
적록색약이 아닌 경우, 이중 for문을 돌려 방문횟수가 없는 경우 ans+=1을 해주고, 큐에 넣어 BFS를 진행한다.
적록색약인 경우, 앞서와 같이 이중 for문을 돌려 방문횟수가 없는 경우 ans+=1을 해주고, 큐에 넣는다. BFS를 진행할때 색이 R or G인 경우에 B가 나오면 continue를 시키면 된다!


✅️[코드]

#include <bits/stdc++.h>

using namespace std;

#define X first

#define Y second

string arr[105];

bool vis[105][105];

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

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

int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    

    int n;

    cin >> n;

    

    for(int i=0; i<n; i++){

        cin >> arr[i];

    }

 

    queue<pair<int,int>> q;

    int ans = 0;

    for(int i=0; i<n; i++){

        

    for(int j=0; j<n; j++){

        if(vis[i][j] == 0) {

        q.push({i,j});

        ans++;

        }

    while(!q.empty()){

        pair<int,int> q2 = q.front(); q.pop();

        for(int i=0; i<4; i++){

            int x = q2.X + dx[i];

            int y = q2.Y + dy[i];

            

            if(x < 0 || x>=n || y < 0 || y>=n) continue;

            if(vis[x][y] != 0 || arr[q2.X][q2.Y] != arr[x][y]) continue;

            

            vis[x][y] = 1;

            q.push({x,y});

        }

    }

    }

    }

    

    cout << ans << ' ';

    memset(vis, 0, sizeof(vis));

    ans = 0;

    for(int i=0; i<n; i++){

        for(int j=0; j<n; j++){

            if(vis[i][j] == 0){

                q.push({i,j});

                ans++;

            } 

            while(!q.empty()){

                pair<int,int> q2 = q.front(); q.pop();

                for(int i=0; i<4; i++){

                    int x = q2.X + dx[i];

                    int y = q2.Y + dy[i];

                    

                    if(x<0|| x>=n || y<0|| y>=n) continue;

                    if(arr[q2.X][q2.Y] == 'G' || arr[q2.X][q2.Y] == 'R'){

                        if(vis[x][y] != 0 || arr[x][y] == 'B') continue;

                    }

                    else{

                        if(vis[x][y] != 0 || arr[x][y] != 'B') continue;

                    }

                    vis[x][y] = 1;

                    q.push({x,y});

                }

            }

        }

    }

    cout << ans;

    

}

'Algorithm > BOJ' 카테고리의 다른 글

5427번 불 - C++  (0) 2022.08.02
1012번 유기농 배추 - C++  (0) 2022.07.31
1697번 숨바꼭질 - C++  (0) 2022.07.29
7576번 토마토 - C++  (0) 2022.07.29
4179번 불! - C++  (0) 2022.07.29