結果

問題 No.3 ビットすごろく
ユーザー papinianuspapinianus
提出日時 2016-07-29 09:40:08
言語 PHP
(8.3.4)
結果
WA  
実行時間 -
コード長 1,320 bytes
コンパイル時間 678 ms
コンパイル使用メモリ 30,996 KB
実行使用メモリ 40,080 KB
最終ジャッジ日時 2024-04-24 10:41:46
合計ジャッジ時間 9,966 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
31,044 KB
testcase_01 AC 41 ms
30,916 KB
testcase_02 AC 41 ms
31,232 KB
testcase_03 AC 59 ms
31,080 KB
testcase_04 WA -
testcase_05 AC 1,855 ms
32,784 KB
testcase_06 AC 62 ms
31,180 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$num = trim(fgets(STDIN));
$step = [0=>0]; //pos=>step
$queue = [[1,0]]; //[pos,src]
while(count($queue) > 0) {
    $cur = array_shift($queue);
    $curPlace = $cur[0]; //今いる位置
    $curStep = $step[$cur[1]]+1; //今いる位置に至るステップ=元いた位置+1
    $width = substr_count(decbin($curPlace), "1"); //今いる位置から進める距離
    $forward = $curPlace+$width; //前方の移動可能な位置
    $backward = $curPlace-$width; // 公報の移動可能な位置
    $step[$curPlace] = $curStep; //今いる位置を経路に追記
    if($forward <= $num) { // 前方が移動可能圏内
        if(isset($step[$forward])) { // 到達した事のある位置
            if($step[$forward] > $curStep+1) {
                $step[$forward] = $curStep+1;
            }
        } else { // 未到達ならキューにプッシュ
            array_push($queue, [$forward, $curPlace]);
        }
    }
    if($backward > 0) { // 後方が移動可能圏内
        if(isset($step[$backward])) {
            if($step[$backward] > $curStep+1) {
                $step[$backward] = $curStep+1;
            }
        } else {
            array_push($queue, [$backward, $curPlace]);
        }
    }
}
if(isset($step[$num])) {
    echo $step[$num];
} else {
    echo -1;
}
echo PHP_EOL;
0