using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "Input5"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("5"); } else if (InputPattern == "Input2") { WillReturn.Add("11"); } else if (InputPattern == "Input3") { WillReturn.Add("4"); } else if (InputPattern == "Input4") { WillReturn.Add("10000"); } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static void Main() { List InputList = GetInputList(); //すごろくのマスの数 int MasuCnt = int.Parse(InputList[0]); //各マスの1のビット数の配列 int[] BitCntArr = new int[MasuCnt]; for (int I = 1; I <= BitCntArr.GetUpperBound(0); I++) { int wkBitCnt = Convert.ToString(I, 2).ToCharArray().Count(X => X == '1'); BitCntArr[I] = wkBitCnt; } //移動回数と訪問済マスのDict var VisitedArrDict = new Dictionary(); for (int MaxMoveCnt = 1; MaxMoveCnt <= MasuCnt; MaxMoveCnt++) { bool[] VisitedArr = new bool[MasuCnt + 1]; VisitedArr[1] = true; for (int CurrMoveCnt = 2; CurrMoveCnt <= MaxMoveCnt; CurrMoveCnt++) { var VisitedIndSet = new HashSet(); for (int I = 1; I <= VisitedArr.GetUpperBound(0); I++) { if (VisitedArr[I] == false) continue; int wkLeftInd = I - BitCntArr[I]; int wkRightInd = I + BitCntArr[I]; if (wkLeftInd >= 1) VisitedIndSet.Add(wkLeftInd); if (wkRightInd <= MasuCnt) VisitedIndSet.Add(wkRightInd); } foreach (int EachVisitedInd in VisitedIndSet) { VisitedArr[EachVisitedInd] = true; } } if (VisitedArr[MasuCnt]) { Console.WriteLine(MaxMoveCnt); return; } //移動回数が1少ない場合と、訪問済マスが同じなら、解なし VisitedArrDict.Add(MaxMoveCnt, VisitedArr); if (MaxMoveCnt >= 2) { if (VisitedArrDict[MaxMoveCnt - 1].SequenceEqual(VisitedArrDict[MaxMoveCnt])) { Console.WriteLine(-1); } } } } }