CodeLabs

[백준 / C#] 9019 : DLSR 본문

백준/5000~

[백준 / C#] 9019 : DLSR

무오_ 2023. 6. 24. 18:26
 public class Pair
    {
        public int num;
        public string str;
        public Pair(int num, string str)
        {
            this.num = num;
            this.str = str;
        }
    }
    internal class Program
    {
        static int t;
        static int[] a, b;
        static string[] result;
        static void BFS(int flag)
        {
            var queue = new Queue<Pair>();
            queue.Enqueue(new Pair(a[flag], ""));
            bool[] visited = new bool[10000];
            visited[a[flag]] = true;

            while(queue.Count != 0)
            {
                int curNum = queue.Peek().num;
                string curStr = queue.Dequeue().str;

                if(curNum == b[flag])
                {
                    result[flag] = curStr;
                    return;
                }

                int D, S, L, R;

                D = (curNum * 2) % 10000;
                if (!visited[D])
                {
                    visited[D] = true;
                    queue.Enqueue(new Pair(D, curStr + "D"));
                }

                S = curNum - 1 < 0 ? 9999 : curNum - 1;
                if (!visited[S])
                {
                    visited[S] = true;
                    queue.Enqueue(new Pair(S, curStr + "S"));
                }

                L = (curNum %1000)*10 + (curNum /1000);
                if (!visited[L])
                {
                    visited[L] = true;
                    queue.Enqueue(new Pair(L, curStr + "L"));
                }

                R = curNum / 10 + (curNum % 10) * 1000;
                if (!visited[R])
                {
                    visited[R] = true;
                    queue.Enqueue(new Pair(R, curStr + "R"));
                }
            }
        }
        static void Solution()
        {
            for (int i = 0; i < t; i++)
            {
                BFS(i);
            }
        }
        static void Input()
        {
            t = int.Parse(Console.ReadLine());
            a = new int[t];
            b = new int[t];
            result = new string[t];
            for (int i = 0; i < t; i++)
            {
                var input = Console.ReadLine().Split();
                a[i] = int.Parse(input[0]);
                b[i] = int.Parse(input[1]);
            }
        }
        static void Output()
        {
            foreach (string str in result)
            {
                Console.WriteLine(str);
            }
        }
        static void Solve()
        {
            Input();
            Solution();
            Output();
        }
        static void Main(string[] args)
        {
            Solve();
        }
    }

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

[백준 / C#] 7576 : 토마토  (0) 2023.06.30
[백준 / C#] 7569 : 토마토  (0) 2023.06.28