結果

問題 No.3 ビットすごろく
ユーザー 綾地寧々綾地寧々
提出日時 2015-05-21 17:45:24
言語 PHP
(8.3.4)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 747 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 18,456 KB
実行使用メモリ 18,940 KB
最終ジャッジ日時 2023-09-13 23:10:36
合計ジャッジ時間 2,196 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
18,860 KB
testcase_01 AC 15 ms
18,732 KB
testcase_02 AC 14 ms
18,732 KB
testcase_03 AC 15 ms
18,928 KB
testcase_04 AC 15 ms
18,868 KB
testcase_05 AC 16 ms
18,672 KB
testcase_06 AC 16 ms
18,788 KB
testcase_07 AC 16 ms
18,820 KB
testcase_08 AC 19 ms
18,792 KB
testcase_09 AC 18 ms
18,732 KB
testcase_10 AC 19 ms
18,864 KB
testcase_11 AC 19 ms
18,940 KB
testcase_12 AC 17 ms
18,864 KB
testcase_13 AC 17 ms
18,896 KB
testcase_14 AC 19 ms
18,884 KB
testcase_15 AC 20 ms
18,916 KB
testcase_16 AC 22 ms
18,868 KB
testcase_17 AC 22 ms
18,772 KB
testcase_18 AC 16 ms
18,796 KB
testcase_19 AC 21 ms
18,672 KB
testcase_20 AC 16 ms
18,820 KB
testcase_21 AC 16 ms
18,728 KB
testcase_22 AC 20 ms
18,820 KB
testcase_23 AC 20 ms
18,764 KB
testcase_24 AC 19 ms
18,756 KB
testcase_25 AC 20 ms
18,864 KB
testcase_26 AC 13 ms
18,748 KB
testcase_27 AC 16 ms
18,764 KB
testcase_28 AC 20 ms
18,840 KB
testcase_29 AC 17 ms
18,836 KB
testcase_30 AC 14 ms
18,668 KB
testcase_31 AC 13 ms
18,768 KB
testcase_32 AC 17 ms
18,844 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

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

// 幅優先探索
while (count($queue) > 0) {
	// 処理対象のマスを取り出す
	$value = array_shift($queue);
	
	// 移動個数を計算する
	$bit = decbin($value);
	$counts = count_chars($bit, 1);
	$count = $counts[ord('1')];
	
	// 移動回数を更新
	$move = $point[$value-1]+1;
	
	// 移動先を訪問
	if ( (($value+$count) <= $n) && ($point[$value+$count-1] === -1) ) {
		$point[$value+$count-1] = $move;
		$queue[] = $value+$count;
	}
	if ( (($value-$count) > 1) && ($point[$value-$count-1] === -1) ) {
		$point[$value-$count-1] = $move;
		$queue[] = $value-$count;
	}
}
echo $point[$n-1] .PHP_EOL;
0