CodeLabs

[백준 / C#] 2178 : 미로탐색 본문

백준/1000 ~

[백준 / C#] 2178 : 미로탐색

무오_ 2023. 7. 28. 22:23
using System;
using System.Collections.Generic;
using System.Linq;

namespace _2178
{
    class Pair
    {
        public Pair(int first_, int second_)
        {
            first = first_;
            second = second_;
        }
        public int second { get; set; }
        public int first { get; set; }
    }
    class Program
    {
        static int n, m;
        static int[] dx = { 0, 1, 0, -1 }, dy = { -1, 0, 1, 0 };
        static int[,] check = new int[100, 100];
        static string[] input = new string[100];
        static bool[,] vi = new bool[100,100];
        static void BFS(int i, int j)
        {
            vi[i, j] = true;
            Pair pair = new Pair(i, j);
            Queue<Pair> q = new Queue<Pair>();
            q.Enqueue(pair);
            while (!q.Count.Equals(0))
            {
                int x = q.Peek().second;
                int y = q.Peek().first;
                q.Dequeue();
                for (int k = 0; k < 4; k++)
                {
                    int newY = y + dy[k];
                    int newX = x + dx[k];
                    if (0 <= newY && newY < n && 0 <= newX && newX < m)
                    {
                        if ((input[newY])[newX] == '1' && !vi[newY, newX]&& check[newY,newX] ==0)
                        {
                            check[newY, newX] = check[y, x] + 1;
                            vi[newY, newX] = true;
                            q.Enqueue(new Pair(newY,newX));
                        }
                    }
                }
            }
        }
        static void Input()
        {
            int[] y_x = Console.ReadLine().Split().Select(int.Parse).ToArray();
            n = y_x[0]; m = y_x[1];
            for (int i = 0; i < n; i++) input[i] = Console.ReadLine();
            BFS(0, 0);
            Console.WriteLine(check[n - 1, m - 1] + 1);
        }
        static void Solve()
        {
            Input();
        }
        static void Main(string[] args)
        {
            Solve();
        }
    }
}

'백준 > 1000 ~' 카테고리의 다른 글

[백준 / C#] 2606 : 바이러스  (0) 2023.08.02
[백준 / C#] 2579 : 계단 오르기  (0) 2023.07.30
[백준 / C#] 1931 : 회의실 배정  (0) 2023.07.26
[백준 / C#] 1927 : 최소 힙  (0) 2023.07.24
[백준 / C++] 1764 : 듣보잡  (0) 2023.07.21