結果

問題 No.3 ビットすごろく
ユーザー AreTrash
提出日時 2016-04-09 07:52:34
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 27 ms / 5,000 ms
コード長 1,175 bytes
コンパイル時間 2,407 ms
コンパイル使用メモリ 108,052 KB
実行使用メモリ 27,744 KB
最終ジャッジ日時 2024-07-01 07:48:50
合計ジャッジ時間 4,402 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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;

namespace No3_3{
    public class Program{
        public static void Main(string[] args){
            Console.WriteLine(Function(int.Parse(Console.ReadLine())));
        }

        public static int Function(int n){
            var que = new Queue<int>();
            var result = new int[n + 1];
            var place = 1;

            que.Enqueue(1);
            result[1] = 1;

            while(que.Count > 0 && place != n){
                place = que.Dequeue();
                int move = 0, tmp = place;
                while(true){
                    if(tmp % 2 == 1) move++;
                    if((tmp /= 2) == 0) break;
                }

                if(place + move <= n && result[place + move] == 0){
                    result[place + move] = result[place] + 1;
                    que.Enqueue(place + move);
                }

                if(1 <= place - move && result[place - move] == 0){
                    result[place - move] = result[place] + 1;
                    que.Enqueue(place - move);
                }
            }
            return result[n] == 0 ? -1 : result[n];
        }
    }
}
0