結果

問題 No.3 ビットすごろく
ユーザー n120aosssssssss
提出日時 2017-05-21 21:49:21
言語 PHP
(843.2)
結果
AC  
実行時間 44 ms / 5,000 ms
コード長 505 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 32,148 KB
実行使用メモリ 31,396 KB
最終ジャッジ日時 2024-07-01 08:43:05
合計ジャッジ時間 3,887 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$N = trim(fgets(STDIN));
$route = array_fill(0, $N, -1);
$route[0] = $queue[0] = 1;

while(!empty($queue)) {
	$pos = array_shift($queue);
	$move = substr_count(decbin($pos), 1);
    if($pos + $move <= $N && $route[$pos + $move - 1] === -1) {
        $route[$pos + $move - 1] = $route[$pos - 1] + 1;
		$queue[] = $pos + $move;
	}
	if($pos - $move > 1 && $route[$pos - $move - 1] === -1) {
		$route[$pos - $move - 1] = $route[$pos - 1] + 1;
		$queue[] = $pos - $move;
	}
}

echo $route[$N -1].PHP_EOL;
0