結果

問題 No.3 ビットすごろく
ユーザー DevbotBotDevbotBot
提出日時 2015-07-01 17:21:37
言語 PHP
(843.2)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 519 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 30,560 KB
実行使用メモリ 31,396 KB
最終ジャッジ日時 2024-07-01 07:26:53
合計ジャッジ時間 2,933 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = trim(fgets(STDIN));
$map = array_fill(0, $n, -1);
$queue[0] = $map[0] = $step = 1;

while (!empty($queue)) {
		$pos = array_shift($queue);
		$step = $map[$pos -1 ];
		$bit = substr_count(decbin($pos),1,0);
		if ($pos + $bit <= $n && -1 === $map[$pos + $bit -1]){
			$move = $pos + $bit;
			$map[$move -1 ] = $step +1;
			$queue[] = $move;
		}
		if ($pos - $bit > 1 && -1 === $map[$pos - $bit -1]){
			$move = $pos - $bit;
			$map[$move -1 ] = $step +1;
			$queue[] = $move;
		}
}
echo $map[$n -1 ] . PHP_EOL;
0