CodeLabs

[백준 / C#] 1697 : 숨바꼭질 본문

백준/1000 ~

[백준 / C#] 1697 : 숨바꼭질

무오_ 2023. 7. 18. 18:42
using System;
using System.Collections.Generic;
using System.Linq;

struct Locate
{
    public int location, time;
    public Locate(int location,int time)
    {
        this.location = location;
        this.time = time;
    }
}

class Program
{
    static int[] N_K = Console.ReadLine().Split().Select(int.Parse).ToArray();
    static int n = N_K[0], k = N_K[1];
    static bool[] visited = new bool[100001];
    static Queue<Locate> lot = new Queue<Locate>();

    static bool Confirm(int n)
    {
        if (n < 0 || n > 100000 || visited[n])
            return false;
        return true;
    }
    static void BFS()
    {
        visited[n] = true;
        lot.Enqueue(new Locate(n,0));
        while (lot.Count != 0)
        {
            int location = lot.Peek().location;
            int time = lot.Peek().time;
            lot.Dequeue();
            if (location == k)
            {
                Console.WriteLine(time);
                break;
            }
            if (Confirm(location - 1))
            {
                visited[location - 1] = true;
                lot.Enqueue(new Locate(location - 1, time + 1));
            }
            if(Confirm(location +1))
            {
                visited[location + 1] = true;
                lot.Enqueue(new Locate(location + 1, time + 1));
            }
            if(Confirm(location*2))
            {
                visited[location * 2] = true;
                lot.Enqueue(new Locate(location * 2, time + 1));
            }
        }
    }
    static void Main(string[] args)
    {
        BFS();
    }
}