728x90
SMALL

C#

 

 

 

 

using System;

using System.Collections.Generic;

public class Solution {
   public int solution(int x, int y, int n)
        {
            int answer = 0;
            int value = 0;
            int tempValue = 0;
            Queue<int> currentValuesQu = new Queue<int>();
            bool[] visited = new bool[y + 1];

            if (x == y)
                return 0;

            currentValuesQu.Enqueue(x);
       
            while (currentValuesQu.Count != 0)
            {
                int size = currentValuesQu.Count;
                for (int iPos = 0; iPos < size; iPos++)
                {
                    value = currentValuesQu.Dequeue();

                    tempValue = value * 3;
                    if (tempValue <= y && visited[tempValue] == false)
                    {
                        currentValuesQu.Enqueue(tempValue);
                        visited[tempValue] = true;
                    }

                    tempValue = value * 2;
                    if (tempValue <= y && visited[tempValue] == false)
                    {
                        currentValuesQu.Enqueue(tempValue);
                        visited[tempValue] = true;
                    }

                    tempValue = value + n;
                    if (tempValue <= y && visited[tempValue] == false)
                    {
                        currentValuesQu.Enqueue(tempValue);
                        visited[tempValue] = true;
                    }
                }

                answer++;
                if (currentValuesQu.Contains(y))
                    return answer;
            }
            return -1;
        }
}

728x90

설정

트랙백

댓글