본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 10026 적록색약

by 왹져박사 2023. 10. 14.
728x90

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

 

10026번: 적록색약

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

www.acmicpc.net

이번에도 BFS 문제이다!

같은 BFS문제여도 매번 색다른 조건이 주어져 항상 재밌다..!

이 문제는 접근하는 방법이 여러 가지 있을 듯하다. 

나는 BFS와 BFSRG 메서드를 만들어 하나의 맵과 방문리스트로 풀어나갔다. 

using System;
using System.IO;
using System.Text;

namespace B10026
{
    class Program
    {
        static int N;
        static char[,] map = new char[101, 101];
        static bool[,] visited = new bool[101, 101];
        static Queue<(int, int)> q = new Queue<(int, int)>();
        static int[] dx = { -1, 1, 0, 0 };
        static int[] dy = { 0, 0, -1, 1 };
        static int count = 0;
        static int countRG = 0;
        //일반
        static void BFS(int x, int y)
        {
            count++;
            q.Enqueue((x, y));
            visited[x, y] = true;
            char c = map[x, y];
            while (q.Count > 0)
            {
                var deq = q.Dequeue();
                for (int i = 0; i < 4; i++)
                {
                    int deqx = deq.Item1 + dx[i];
                    int deqy = deq.Item2 + dy[i];
                    if (deqx < 0 || deqy < 0 || deqx >= N || deqy >= N) continue;
                    if (map[deqx, deqy] == c && visited[deqx, deqy] == false) 
                    {
                        q.Enqueue((deqx, deqy));
                        visited[deqx, deqy] = true;
                    }
                }
            }
        }
        //적록색약
        static void BFSRG(int x, int y)
        {
            countRG++;
            q.Enqueue((x, y));
            char c = map[x, y];
            visited[x, y] = true;
            while (q.Count > 0)
            {
                var deq = q.Dequeue();
                for (int i = 0; i < 4; i++)
                {
                    int deqx = deq.Item1 + dx[i];
                    int deqy = deq.Item2 + dy[i];
                    if (deqx < 0 || deqy < 0 || deqx >= N || deqy >= N) continue;
                    if (c == 'B')
                    {
                        if (map[deqx, deqy] == 'B' && visited[deqx, deqy] == false)
                        {
                            q.Enqueue((deqx, deqy));
                            visited[deqx, deqy] = true;
                        }
                    }
                    else
                    {
                        if ((map[deqx, deqy] == 'G' || map[deqx, deqy] == 'R') && visited[deqx, deqy] == false) 
                        {
                            q.Enqueue((deqx, deqy));
                            visited[deqx, deqy] = true;
                        }
                    }
                }
            }
        }
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            //입력
            N = int.Parse(sr.ReadLine());
            for (int i = 0; i < N; i++)
            {
                string str = sr.ReadLine();
                for (int j = 0; j < N; j++)
                {
                    map[i, j] = str[j];
                }
            }
            //BFS
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (visited[i, j] == false) BFS(i, j);
                }
            }
            Array.Clear(visited);
            //BFS적록색약
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    if (visited[i, j] == false) BFSRG(i, j);
                }
            }
            sw.Write(count + " " + countRG);
            sr.Close();
            sw.Flush();
            sw.Close();
        }

    }
}

C#으로 많이 푸는 사람이 없어 랭킹 들기가 쉬운 것 같다..ㅎㅎ

 

728x90