알고리즘/백준

백준 10026 - 적록색약 (Java)

D_Helloper 2024. 5. 14. 23:27

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

문제

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다.

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

예를 들어, 그림이 아래와 같은 경우에

RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

적록색약이 아닌 사람이 봤을 때 구역의 수는 총 4개이다. (빨강 2, 파랑 1, 초록 1) 하지만, 적록색약인 사람은 구역을 3개 볼 수 있다. (빨강-초록 2, 파랑 1)

그림이 입력으로 주어졌을 때, 적록색약인 사람이 봤을 때와 아닌 사람이 봤을 때 구역의 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N이 주어진다. (1 ≤ N ≤ 100)

둘째 줄부터 N개 줄에는 그림이 주어진다.

출력

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

예제 입력 1 복사

5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

예제 출력 1 복사

4 3

 

풀이

요즘 BFS 감이 떨어져서 푼 문제,

BFS를 두 번 돌리면 될 것이라 생각했다.

색약인 경우와 아닌 경우의 탈출조건만 다르게 주면 될 것이라 생각.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class BJ_10026_적록색약 {
    static int N;
    static boolean[][] visited;
    static char[][] map;
    static Queue<int[]> que;
    static int[][] deltas = {{-1,0},{0,1},{1,0},{0,-1}};
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        N = Integer.parseInt(br.readLine());
        map = new char[N][N];
        visited = new boolean[N][N];
        que = new ArrayDeque<>();
        for(int i=0; i<N; i++)
        {
            String str = br.readLine();
            for(int j=0; j<N; j++)
            {
                map[i][j] = str.charAt(j);
            }
        }
        int cnt=0;
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<N; j++)
            {
                if(!visited[i][j])
                {
                    que.add(new int[]{i,j});
                    visited[i][j] = true;
                    cnt++;
                    BFS();
                }
            }
        }
        visited = new boolean[N][N];
        int cnt2=0;
        for(int i=0; i<N; i++)
        {
            for(int j=0; j<N; j++)
            {
                if(!visited[i][j])
                {
                    que.add(new int[]{i,j});
                    visited[i][j] = true;
                    cnt2++;
                    BFS2();
                }
            }
        }
        System.out.println(cnt);
        System.out.println(cnt2);
    }
    public static void BFS()
    {
        while(!que.isEmpty())
        {
            int[] temp = que.poll();
            for(int dir=0; dir<4; dir++)
            {
                int dx = temp[0]+deltas[dir][0];
                int dy = temp[1]+deltas[dir][1];
                if(dx<0||dy<0||dx>=N||dy>=N||visited[dx][dy]||map[dx][dy]!=map[temp[0]][temp[1]]) continue;
                visited[dx][dy] = true;
                que.add(new int[]{dx,dy});
            }
        }
    }
    public static void BFS2()
    {
        while(!que.isEmpty())
        {
            int[] temp = que.poll();
            for(int dir=0; dir<4; dir++)
            {
                int dx = temp[0]+deltas[dir][0];
                int dy = temp[1]+deltas[dir][1];
                if(dx<0||dy<0||dx>=N||dy>=N||visited[dx][dy]) continue;
                if(map[temp[0]][temp[1]]=='R' || map[temp[0]][temp[1]]=='G'){
                    if(map[dx][dy]=='B') continue;
                }
                else {
                    if(map[temp[0]][temp[1]]!=map[dx][dy]) continue;
                }

                visited[dx][dy] = true;
                que.add(new int[]{dx,dy});
            }
        }
    }
}