結果

問題 No.3 ビットすごろく
ユーザー AreTrash
提出日時 2016-04-09 07:40:27
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 26 ms / 5,000 ms
コード長 1,138 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 107,680 KB
実行使用メモリ 25,860 KB
最終ジャッジ日時 2024-07-01 07:49:02
合計ジャッジ時間 2,581 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;

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];

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

            while(que.Count > 0){
                var 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