結果

問題 No.3 ビットすごろく
ユーザー stady0sh
提出日時 2017-06-01 11:27:18
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 4,408 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 114,224 KB
実行使用メモリ 21,888 KB
最終ジャッジ日時 2024-07-01 08:43:40
合計ジャッジ時間 3,477 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
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);
            Sugoroku.BFSSearch();
            var result = Sugoroku.GetShortRangeCount;
            Console.WriteLine($"{result}");
        }

        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 shortRange;     // 最短距離
        private int[] visitedArray; // 訪問済み配列
        private int startPos;       // スタートポジション
        private int goalPos;        // ゴールポジション

        public int GetShortRangeCount
        {
            get { return this.shortRange; }
        }

        enum move
        {
            Right,
            Left
        }

        // コンストラクタ
        public Sugoroku(int[] Sugoroku)
        {
            this.shortRange = -1;
            this.visitedArray = new int[Sugoroku.Length + 1];
            this.startPos = 1;              // スタートポジションは1固定
            this.goalPos = Sugoroku.Length;
        }

        // すごろく
        public bool BFSSearch()
        {
            var isGoal = false;

            this.InitVisitedArray();
            visitedArray[startPos] = startPos;

            // スタート位置をQueueに追加
            var queue = new Queue<int>();
            queue.Enqueue(startPos);

            // ***1の場合の特別処理***
            if (startPos == goalPos)
            {
                shortRange = 1;
                isGoal = true;
                return isGoal;
            }

            // Queueが0になるまでループ
            while (queue.Count > 0)
            {
                var location = queue.Dequeue();

                // 移動先を計算
                int moveCnt = Convert.ToString(location, 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 + moveCnt;
                            break;
                        case move.Left:
                            nextPlace = location - moveCnt;
                            break;
                    }

                    // 移動先が有効か、ゴールか等を判定
                    if (nextPlace < 1 || nextPlace > goalPos)
                    {
                        continue;
                    }

                    if (visitedArray[nextPlace] > 0)
                    {
                        continue;
                    }

                    // 移動先が有効ならキューに追加
                    visitedArray[nextPlace] = location;
                    queue.Enqueue(nextPlace);

                    // ゴールならキューに追加、ループを抜ける
                    if (nextPlace == goalPos)
                    {
                        isGoal = true;
                        break;
                    }
                }
                if (isGoal)
                {
                    break;
                }
            }

            // ゴール可否の処理
            if (isGoal)
            {
                this.shortRange = GetShortRange();
            }

            return isGoal;
        }

        private int GetShortRange()
        {
            int cnt = 0;
            int index = goalPos;
            int beforeIndex = 0;
            while (true)
            {
                beforeIndex = visitedArray[index];
                cnt++;
                if (index == 1) { break; }
                index = beforeIndex;
            }

            return cnt;
        }

        private void InitVisitedArray()
        {
            this.visitedArray = Enumerable.Repeat(-1, this.visitedArray.Length).ToArray();
        } 



    }
}
0