Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • BFS
  • boj
  • DFS
  • django
  • 백트래킹
  • docker
  • Network
  • 다이나믹 프로그래밍
  • Kubernetes
  • 프로그래머스

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

Algorithm/BOJ

[백준 10026] 적록색약 C++

2021. 5. 8. 23:26
 

10026번: 적록색약

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

www.acmicpc.net

BFS 연습 #1

 

전형적인 BFS 탐색 문제이다

색약인 경우에 'R' 과 'G' 를 같다 생각하고 탐색하게 해주면 문제없이 풀 수 있다 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <queue>
#include <utility>
 
#define pii pair<int, int>
 
using namespace std;
 
int N;
char Map[101][101];
bool visited[101][101][2];
int dr[4] = {0, 0, 1, -1}, dc[4] = {1, -1, 0, 0};
 
void input(){
    string in;
    cin >> N;
    for(int i = 0; i < N; i++){
        cin >> in;
        for(int j = 0; j < N; j++){
            Map[i][j] = in[j];
        }
    }
}
 
void BFS(int r, int c, int d, char color){
    queue<pii> que;
    que.push({r, c});
    visited[r][c][d] = true;
    
    while(!que.empty()){
        pii now = que.front();
        que.pop();
        
        for(int i = 0; i < 4; i++){
            int nr = now.first + dr[i];
            int nc = now.second + dc[i];
            if(nr < 0 || nr >= N || nc < 0 || nc >= N || visited[nr][nc][d]) continue;
            if(d == 0){     //색약이 아닌경우
                if(Map[nr][nc] == color){
                    visited[nr][nc][d] = true;
                    que.push({nr, nc});
                }
            }
            else{   // 색약인 경우
                if(color == Map[nr][nc]){
                    visited[nr][nc][d] = true;
                    que.push({nr, nc});
                }
                else if((color == 'R' && Map[nr][nc] == 'G') || (color == 'G' && Map[nr][nc] == 'R')){
                    visited[nr][nc][d] = true;
                    que.push({nr, nc});
                }
            }
        }
    }
}
 
void solve(){
    int ncount = 0, cwcount = 0;
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            char nowColor = Map[i][j];
            if(!visited[i][j][0]){
                BFS(i, j, 0, nowColor);
                ncount++;
            }
            if(!visited[i][j][1]){
                BFS(i, j, 1, nowColor);
                cwcount++;
            }
        }
    }
    cout << ncount << " " << cwcount;
}
 
int main(){
    input();
    solve();
    
    return 0;
}
 
Colored by Color Scripter
cs

BFS

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

[백준 1700] 멀티탭 스케줄링 C++  (0) 2021.05.16
[백준 1799] 비숍 C++  (0) 2021.05.12
[백준 14499] 주사위 굴리기 C++  (0) 2021.05.08
[백준 2533] 사회망 서비스(SNS) C++  (0) 2021.05.06
[백준 1949] 우수 마을 C++  (2) 2021.05.06
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 1700] 멀티탭 스케줄링 C++
    • [백준 1799] 비숍 C++
    • [백준 14499] 주사위 굴리기 C++
    • [백준 2533] 사회망 서비스(SNS) C++

    티스토리툴바