본문 바로가기
Algorithm/C++

[백준 10026] 적록색약

by imagineer_jinny 2022. 8. 30.

10026번: 적록색약 (acmicpc.net)

 

10026번: 적록색약

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

www.acmicpc.net

 

내 코드

#include <bits/stdc++.h>
using namespace std;

char board[101][101];
bool vis[101][101];
int n;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 }; 


void bfs(int i, int j) {
    queue<pair<int,int>> q; 
    q.push({i,j});
    vis[i][j]=1;
    
    while(!q.empty())
    {
        int x=q.front().first;
        int y=q.front().second;
        q.pop();
        for(int i=0;i<4;i++)
        {
            int nx=x+dx[i];
            int ny=y+dy[i];

            if(0<=nx&&nx<n&&0<=ny&&ny<n)
            {
                if((board[x][y]==board[nx][ny])&&vis[nx][ny]==0)
                {
                    vis[nx][ny]=1;
                    q.push({nx,ny});
                }
                
            }
        }
    }
   
}

int area(){
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(vis[i][j]==false)
            {
                cnt++;
                bfs(i,j);
                
            }
                
        }
    }
    return cnt;
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
 
   int not_g=area(); //정상인
    
    for(int i = 0; i < n; i++)
        fill(vis[i], vis[i]+n, false);
    
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
              if (board[i][j] == 'G')
                    board[i][j] = 'R';
            }
        }        
    
    int is_g=area(); //색약
    
      cout << not_g << " " << is_g;
      return 0;
    
}

 

정답 코드

// Authored by : seeys
// Co-authored by : BaaaaaaaaaaarkingDog
// http://boj.kr/99a676d859f54fa0944f81f94ade04a3
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
char board[101][101];
bool vis[101][101];
int n;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 }; 

void bfs(int i, int j) {
  queue<pair<int, int>> Q;
  Q.push({ i,j });
  vis[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 < 0 || nx >= n || ny < 0 || ny >= n) continue;
      if (vis[nx][ny] == 1 || board[i][j] != board[nx][ny]) continue;
      vis[nx][ny] = 1;
      Q.push({ nx,ny });
    }
  }
}

int area(){
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (!vis[i][j]) {
        cnt++;
        bfs(i, j);
      }
    }
  }
  return cnt;
}

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
  int not_g = area(); //적록색약이 아닌사람

  // 적록색약인 사람을 구하기위한 방문배열 초기화
  for(int i = 0; i < n; i++)
    fill(vis[i], vis[i]+n, false);
  
  // 적록색약은 초록과 빨강을 구분 못하므로 초록이면 빨강으로 바꿔줌
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (board[i][j] == 'G')
        board[i][j] = 'R';
    }
  }

  int is_g = area(); //적록색약인 사람
  cout << not_g << " " << is_g;
  return 0;
}

'Algorithm > C++' 카테고리의 다른 글

[백준 1874] 스택 수열  (0) 2022.08.31
[백준 7569] 토마토  (0) 2022.08.30
[백준 1012] 유기농 배추  (0) 2022.08.30
[백준 1679] 숨바꼭질  (0) 2022.08.29
[백준 4179] 불!  (0) 2022.08.29

댓글