Gidhub BE Developer

[BOJ] 10026. 적록색약

2018-10-01
goodGid

Problem

Problem URL : 적록색약


[1] Answer Code (18. 10. 01)

#include<iostream>
#include<cstring>
using namespace std;

char map[101][101];
int v[101][101];
int n,m;
int dx[] = {0,1,0,-1};
int dy[] = {1,0,-1,0};

void dfs(int x, int y){
    v[x][y] = 1;
    for(int i=0; i<4; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if(nx >= 0 && nx < n && ny >= 0 && ny < n)
            if(!v[nx][ny] && map[nx][ny] == map[x][y])
                dfs(nx,ny);
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    memset(map,0,sizeof(map));
    memset(v,0,sizeof(v));
    cin >> n;
    
    for(int i=0; i<n; i++){
        string s;
        cin >> s;
        for(int j=0; j<n; j++)
            map[i][j] = s[j];
    }
    
    int cnt = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if( !v[i][j] ){
                cnt ++;
                dfs(i,j);
            }
        }
    }
    cout << cnt << " ";
    
    memset(v,0,sizeof(v));
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if( map[i][j] == 'G')
                map[i][j] = 'R';
        }
    }
    
    cnt = 0;
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            if( !v[i][j] ){
                cnt ++;
                dfs(i,j);
            }
        }
    }
    
    cout << cnt << '\n';
    
    return 0;
}

Review

  • DFS.

Comments

Content