結果

問題 No.3 ビットすごろく
ユーザー aketijyuuzouaketijyuuzou
提出日時 2024-10-10 21:26:08
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 305 ms / 5,000 ms
コード長 3,298 bytes
コンパイル時間 974 ms
コンパイル使用メモリ 115,932 KB
実行使用メモリ 66,168 KB
最終ジャッジ日時 2024-10-10 21:26:17
合計ジャッジ時間 6,817 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
24,796 KB
testcase_01 AC 29 ms
25,388 KB
testcase_02 AC 29 ms
24,920 KB
testcase_03 AC 36 ms
33,476 KB
testcase_04 AC 30 ms
25,136 KB
testcase_05 AC 91 ms
54,392 KB
testcase_06 AC 39 ms
31,972 KB
testcase_07 AC 32 ms
27,068 KB
testcase_08 AC 57 ms
44,128 KB
testcase_09 AC 145 ms
56,616 KB
testcase_10 AC 226 ms
56,872 KB
testcase_11 AC 122 ms
52,080 KB
testcase_12 AC 86 ms
52,920 KB
testcase_13 AC 35 ms
31,200 KB
testcase_14 AC 216 ms
59,620 KB
testcase_15 AC 295 ms
61,912 KB
testcase_16 AC 263 ms
61,164 KB
testcase_17 AC 284 ms
66,168 KB
testcase_18 AC 34 ms
29,340 KB
testcase_19 AC 290 ms
63,980 KB
testcase_20 AC 29 ms
27,116 KB
testcase_21 AC 29 ms
24,944 KB
testcase_22 AC 215 ms
57,296 KB
testcase_23 AC 305 ms
66,084 KB
testcase_24 AC 288 ms
63,968 KB
testcase_25 AC 285 ms
64,168 KB
testcase_26 AC 26 ms
24,780 KB
testcase_27 AC 35 ms
29,096 KB
testcase_28 AC 254 ms
61,292 KB
testcase_29 AC 123 ms
51,788 KB
testcase_30 AC 29 ms
22,960 KB
testcase_31 AC 30 ms
24,800 KB
testcase_32 AC 110 ms
55,924 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5");
            //4
            //1->2->3->5
            //という経路をたどります。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("11");
            //1->2->3->5->7->10->8->9->11
            //という経路をたどると9移動数で到達します。
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("4");
            //4つしかマスがないときは到達することは出来ません。
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct JyoutaiDef
    {
        internal int Level;
        internal int CurrInd;
        internal string Path;
    }

    static void Main()
    {
        List<string> 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).Count(X => X == '1');
            BitCntArr[I] = wkBitCnt;
        }

        //各マスごとの最短手数
        var MinLevelDict = new Dictionary<int, int>();

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.Level = 1;
        WillPush.CurrInd = 1;
        WillPush.Path = "1";
        stk.Push(WillPush);

        int AnswerLevel = int.MaxValue;
        bool FoundAnswer = false;

        while (stk.Count > 0) {
            JyoutaiDef Popped = stk.Pop();
            int CurrInd = Popped.CurrInd;

            //クリア判定
            if (CurrInd == MasuCnt) {
                FoundAnswer = true;
                AnswerLevel = Popped.Level;
                //Console.WriteLine("{0}手の解候補。{1}を発見", Popped.Level, Popped.Path);
                continue;
            }

            //レベル制限
            if (AnswerLevel <= Popped.Level + 1) continue;

            //Push処理
            Action<int> PushSyori = (pNewInd) =>
            {
                if (pNewInd < 1) return;
                if (MasuCnt < pNewInd) return;

                WillPush.Level = Popped.Level + 1;
                WillPush.CurrInd = pNewInd;

                //各マスごとの最短手数でなければ枝切り
                if (MinLevelDict.ContainsKey(WillPush.CurrInd)) {
                    if (MinLevelDict[WillPush.CurrInd] <= WillPush.Level) {
                        return;
                    }
                }
                MinLevelDict[WillPush.CurrInd] = WillPush.Level;

                WillPush.Path = Popped.Path + "->" + pNewInd.ToString();
                stk.Push(WillPush);
            };

            PushSyori(CurrInd - BitCntArr[CurrInd]);
            PushSyori(CurrInd + BitCntArr[CurrInd]);
        }
        Console.WriteLine(FoundAnswer ? AnswerLevel : -1);
    }
}
0