using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace YukiCoder_Cs { class Program { public static void Main() { var N = int.Parse(Console.ReadLine().Trim()); var root = CreateRoot(N); var Sugoroku = new Sugoroku(root); Console.WriteLine($"{Sugoroku.BFSSearch()}"); } private static int[] CreateRoot(int length) { int[] root = new int[length]; for (int i = 0; i < length; ++i) { root[i] = i + 1; } return root; } } public class Sugoroku { private int[] visitedArray; // 訪問済み配列 private int startPos; // スタートポジション private int goalPos; // ゴールポジション enum move { Right, Left } // コンストラクタ public Sugoroku(int[] Sugoroku) { this.visitedArray = new int[Sugoroku.Length + 1]; this.startPos = 1; // スタートポジションは1固定 this.goalPos = Sugoroku.Length; } public int BFSSearch() { this.InitVisitedArray(); visitedArray[startPos] = startPos; // スタート位置をQueueに追加 var queue = new Queue(); queue.Enqueue(new Location(startPos,1)); // ***1の場合の特別処理*** if (startPos == goalPos) { return 1; } // Queueが0になるまでループ while (queue.Any()) { var location = queue.Dequeue(); // 移動先を計算 int moveCnt = Convert.ToString(location.num, 2).ToCharArray().Where(c => c == '1').Count(); int nextPlace = 0; foreach (move item in Enum.GetValues(typeof(move))) { switch (item) { case move.Right: nextPlace = location.num + moveCnt; break; case move.Left: nextPlace = location.num - moveCnt; break; } // 移動先が有効か、ゴールか等を判定 if (nextPlace < 1 || nextPlace > goalPos) { continue; } if (visitedArray[nextPlace] > 0) { continue; } // ゴールならキューに追加、ループを抜ける if (nextPlace == goalPos) { return location.count + 1; } // 移動先が有効ならキューに追加 visitedArray[nextPlace] = location.num; queue.Enqueue(new Location(nextPlace, location.count + 1)); } } return -1; } private void InitVisitedArray() { this.visitedArray = Enumerable.Repeat(-1, this.visitedArray.Length).ToArray(); } } public class Location { public int __location; public int num { get; set; } public int count { get; set; } public Location(int num, int count) { this.num = num; this.count = count; } } }