[백준] 10026번: 적록색

프로타쿠·2024년 8월 2일

백준

목록 보기
6/24

Solved.ac 골드5
https://www.acmicpc.net/problem/10026

![문제https://velog.velcdn.com/images/protaku/post/45d182a9-7d4d-463d-96ca-ce1ec677a1da/image.png)

설명

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다.
크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록), B(파랑) 중 하나를 색칠한 그림이 있다.
그림은 몇 개의 구역으로 나뉘어져 있는데, 구역은 같은 색으로 이루어져 있다. 또, 같은 색상이 상하좌우로 인접해 있는 경우에 두 글자는 같은 구역에 속한다. (색상의 차이를 거의 느끼지 못하는 경우도 같은 색상이라 한다) 예를 들어, 그림이 아래와 같은 경우에

RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

적록색약이 아닌 사람이 봤을 때 구역의 수는 총 4개이다. (빨강 2, 파랑 1, 초록 1)
하지만, 적록색약인 사람은 구역을 3개 볼 수 있다. (빨강-초록 2, 파랑 1)
그림이 입력으로 주어졌을 때, 적록색약인 사람이 봤을 때와 아닌 사람이 봤을 때 구역의 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N이 주어진다. (1 ≤ N ≤ 100). 둘째 줄부터 N개 줄에는 그림이 주어진다.

출력

적록색약이 아닌 사람이 봤을 때의 구역의 개수와 적록색약인 사람이 봤을 때의 구역의 수를 공백으로 구분해 출력한다.

해결 Tip

알고리즘 분류

  • 그래프 이론
  • 그래프 탐색
  • 너비 우선 탐색
  • 깊이 우선 탐색

그래프 탐색을 할 때 적록색약인 경우는 R과 G를 똑같이 취급해서 방문하면 된다

코드

#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

struct Point {
    int x, y;
};

int N, ans1, ans2;
char color;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int image[102][102];
bool check[102][102];
map<char,int> match = {
    {'R', 1}, {'G', 2}, {'B', 3}
};

void solve1(int C, Point p) {
    queue<Point> q;
    q.push(p);
    check[p.y][p.x] = true;

    while (!q.empty()) {
        Point now = q.front();
        q.pop();

        for (int i=0 ; i < 4 ; i++) {
            Point next = {now.x+dx[i], now.y+dy[i]};
            if (image[next.y][next.x] == C && !check[next.y][next.x]) {
                check[next.y][next.x] = true;
                q.push(next);
            }
        }
    }
}

void solve2(int C, Point p) {
    queue<Point> q;
    q.push(p);
    check[p.y][p.x] = true;

    if (C == match['B']) {
        while (!q.empty()) {
            Point now = q.front();
            q.pop();

            for (int i=0 ; i < 4 ; i++) {
                Point next = {now.x+dx[i], now.y+dy[i]};
                if (image[next.y][next.x] == match['B'] && !check[next.y][next.x]) {
                    check[next.y][next.x] = true;
                    q.push(next);
                }
            }
        }
    }
    else {
        while (!q.empty()) {
            Point now = q.front();
            q.pop();

            for (int i=0 ; i < 4 ; i++) {
                Point next = {now.x+dx[i], now.y+dy[i]};
                if ((image[next.y][next.x] == match['G']
                    || image[next.y][next.x] == match['R'])
                    && !check[next.y][next.x]) {
                        check[next.y][next.x] = true;
                        q.push(next);
                }
            }
        }
    }
}

int main() {
    fastio;
    cin >> N;
    for (int i=1 ; i <= N ; i++) {
        for (int j=1 ; j <= N ; j++) {
            cin >> color;
            image[i][j] = match[color];
        }
    }

    for (int i=1 ; i <= N ; i++) {
        for (int j=1 ; j <= N ; j++) {
            if (!check[i][j]) {
                solve1(image[i][j], {j,i});
                ans1++;
            }
        }
    }

    for (int i=1 ; i <= N ; i++) {
        for (int j=1 ; j <= N ; j++)
            check[i][j] = false;
    }

    for (int i=1 ; i <= N ; i++) {
        for (int j=1 ; j <= N ; j++) {
            if (!check[i][j]) {
                solve2(image[i][j], {j,i});
                ans2++;
            }
        }
    }

    cout << ans1 << ' ' << ans2;

    return 0;
}
profile
안녕하세요! 프로타쿠입니다

0개의 댓글