CodeLabs

[백준 / C#] 1012 : 유기농 배추 본문

백준/1000 ~

[백준 / C#] 1012 : 유기농 배추

무오_ 2023. 7. 6. 18:31
using System;
using System.Linq;

namespace _1012
{
    class Program
    {
        static int t, cnt;
        static int[] dy = { 0, 1, 0, -1 }, dx = { -1, 0, 1, 0 }, input;
        static bool[,] vi;
        static int[,] map;
        static void DFS(int i, int j)
        {
            vi[i, j] = true;
            for (int k = 0; k < 4; k++)
            {
                int newX = i + dx[k];
                int newY = j + dy[k];

                if (0 <= newX && newX < input[0] && 0 <= newY && newY < input[1])
                    if (map[newX,newY] == 1 && !vi[newX, newY])
                        DFS(newX, newY);
            }
        }
        static void Solution()
        {
            for (int i = 0; i < input[0]; i++)
            {
                for (int j = 0; j < input[1]; j++)
                {
                    if (map[i, j] == 1 && !vi[i,j])
                    {
                        DFS(i, j);
                        cnt++;
                    }
                }
            }
        }
        static void Input()
        {
            t = int.Parse(Console.ReadLine());
            while(t-->0)
            {
                input = Console.ReadLine().Split().Select(int.Parse).ToArray();
                map = new int[input[0], input[1]];
                vi = new bool[input[0], input[1]];
                for(int i=0; i<input[2]; i++)
                {
                    int[] location = Console.ReadLine().Split().Select(int.Parse).ToArray();
                    map[location[0], location[1]] = 1;
                }
                cnt = 0;
                Solution();
                Console.WriteLine(cnt);
            }
        }
        static void Solve()
        {
            Input();
        }
        static void Main(string[] args)
        {
            Solve();
        }
    }
}