結果

問題 No.3 ビットすごろく
ユーザー stady0shstady0sh
提出日時 2017-06-01 12:43:40
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 91 ms / 5,000 ms
コード長 3,763 bytes
コンパイル時間 4,865 ms
コンパイル使用メモリ 110,472 KB
実行使用メモリ 26,128 KB
最終ジャッジ日時 2023-09-14 00:44:45
合計ジャッジ時間 6,739 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
21,960 KB
testcase_01 AC 65 ms
24,128 KB
testcase_02 AC 63 ms
20,020 KB
testcase_03 AC 69 ms
24,032 KB
testcase_04 AC 63 ms
21,992 KB
testcase_05 AC 74 ms
21,932 KB
testcase_06 AC 71 ms
21,988 KB
testcase_07 AC 69 ms
21,860 KB
testcase_08 AC 77 ms
21,944 KB
testcase_09 AC 85 ms
24,144 KB
testcase_10 AC 90 ms
26,128 KB
testcase_11 AC 83 ms
25,960 KB
testcase_12 AC 79 ms
19,860 KB
testcase_13 AC 71 ms
19,868 KB
testcase_14 AC 87 ms
26,076 KB
testcase_15 AC 91 ms
24,024 KB
testcase_16 AC 87 ms
24,164 KB
testcase_17 AC 88 ms
23,988 KB
testcase_18 AC 69 ms
22,056 KB
testcase_19 AC 89 ms
24,228 KB
testcase_20 AC 66 ms
21,948 KB
testcase_21 AC 65 ms
23,996 KB
testcase_22 AC 84 ms
24,004 KB
testcase_23 AC 89 ms
24,024 KB
testcase_24 AC 90 ms
26,072 KB
testcase_25 AC 91 ms
22,180 KB
testcase_26 AC 58 ms
23,796 KB
testcase_27 AC 70 ms
22,068 KB
testcase_28 AC 87 ms
24,096 KB
testcase_29 AC 82 ms
24,036 KB
testcase_30 AC 66 ms
23,968 KB
testcase_31 AC 64 ms
22,076 KB
testcase_32 AC 79 ms
22,088 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;
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<Location>();
            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;
        }        
    }
}
0