관리 메뉴

풀이 보관함

[C++] 백준 10026번: 적록색약 본문

problem solving/백준

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

viin 2022. 8. 22. 16:51

문제

https://www.acmicpc.net/problem/10026

 

풀이

BFS로 풀었다.

비장애인과 적록색약의 탐색 함수를 2개 만들어서 풀었다.

 

다른 방법으로는 입력 배열을 2개 만드는 방법이 있다.

  • R or G 은 1로 저장
  • B는 0으로 저장

이렇게 R과 G를 같은 걸로 취급하면 함수를 1개만 짜도 된다는 장점이 있다. 

 

소스

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <queue>
#include <memory.h>
#define MAX 101
 
char map[MAX][MAX];
bool visited[MAX][MAX];
 
int dx[4= {1-100};
int dy[4= {00-11};
 
bool isValid(int x, int y)
{
    if(x >= MAX || y >= MAX || x < 0 || y < 0)
        return false;
    return true;
}
 
void BFS(int r, int c)
{
    char now = map[r][c];
    std::queue<std::pair<intint>> q;
    q.push(std::make_pair(r, c));
    
    while(!q.empty())
    {
        for(int i = 0; i < 4++i)
        {
            int x = q.front().first + dx[i];
            int y = q.front().second + dy[i];
            
            if(visited[x][y] || !isValid(x, y))
                continue;
            
            if(now == map[x][y])
            {
                q.push(std::make_pair(x, y));
                visited[x][y] = true;
            }
        }
        q.pop();
    }
}
 
void BFS2(int r, int c)
{
    char now = map[r][c];
    bool flag = false;
    
    if(now == 'R' || now == 'G')
        flag = true;
    
    std::queue<std::pair<intint>> q;
    q.push(std::make_pair(r, c));
    
    while(!q.empty())
    {
        for(int i = 0; i < 4++i)
        {
            int x = q.front().first + dx[i];
            int y = q.front().second + dy[i];
            
            if(visited[x][y]|| !isValid(x, y)) continue;
            
            if(flag)
            {
                if(map[x][y] == 'R' || map[x][y] == 'G')
                {
                    q.push(std::make_pair(x, y));
                    visited[x][y] = true;
                }
            }
            else if(now == map[x][y])
            {
                q.push(std::make_pair(x, y));
                visited[x][y] = true;
            }
        }
        q.pop();
    }
}
 
 
int main()
{
    //input
    int N = 0;
    std::cin >> N;
    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j){
            std::cin >> map[i][j];
        }
    }
    
    //solve
    int rgb = 0;
    int rg = 0;
    
    //normal
    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j){
            if(!visited[i][j])
            {
                BFS(i, j);
                ++rgb;
            }
        }
    }
 
    //2
    memset(visited, falsesizeof(visited));
    for(int i=0; i<N; ++i){
        for(int j=0; j<N; ++j){
            if(!visited[i][j])
            {
                BFS2(i, j);
                ++rg;
            }
        }
    }
    
    //output
    std::cout << rgb <<" "<< rg << std::endl;
    return  0;
}
 
cs