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

2022. 8. 22. 16:51·problem solving/백준

문제

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, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
 
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<int, int>> 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<int, int>> 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, false, sizeof(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;
}
 
Colored by Color Scripter
cs

 

저작자표시 비영리 변경금지 (새창열림)
'problem solving/백준' 카테고리의 다른 글
  • [C++] 백준 7576번: 토마토
  • [C++] 백준 6593번: 상범 빌딩
  • [C++] 백준 5014번: 스타트링크
  • [C++] 백준 9466번: 텀 프로젝트
u1qns
u1qns
http://github.com/u1qns
  • u1qns
    개발 블로그
    u1qns
  • 전체
    오늘
    어제
    • 분류 전체보기 (173)
      • 회고 (1)
      • programming (17)
        • 개념 정리 (6)
        • CI CD (1)
        • 트러블 슈팅 (0)
        • 환경설정 및 팁 (7)
      • problem solving (155)
        • 개념 정리 (3)
        • 백준 (129)
        • SWEA (15)
        • 프로그래머스 (4)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

    • 깃허브
  • 공지사항

  • 인기 글

  • 태그

    cpp
    C++
    구현
    SSAFY수료식
    DFS
    SSAFY
    boj
    SWEA
    POW
    되추적
    투포인터
    HELLOSSAFY
    삼성청년SW아카데미
    cmath
    미해결
    DP
    그리디
    완전탐색
    BFS
    백준
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
u1qns
[C++] 백준 10026번: 적록색약
상단으로

티스토리툴바