結果

問題 No.3 ビットすごろく
ユーザー 綾地寧々綾地寧々
提出日時 2015-05-19 22:38:33
言語 PHP
(8.3.4)
結果
WA  
実行時間 -
コード長 940 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 18,680 KB
実行使用メモリ 21,048 KB
最終ジャッジ日時 2023-09-20 10:17:24
合計ジャッジ時間 3,897 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
18,888 KB
testcase_01 AC 16 ms
18,896 KB
testcase_02 AC 16 ms
18,884 KB
testcase_03 AC 22 ms
18,952 KB
testcase_04 AC 17 ms
18,888 KB
testcase_05 AC 44 ms
18,956 KB
testcase_06 AC 24 ms
18,888 KB
testcase_07 AC 18 ms
18,900 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 42 ms
18,816 KB
testcase_13 AC 20 ms
18,724 KB
testcase_14 AC 76 ms
20,948 KB
testcase_15 AC 108 ms
20,916 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 20 ms
18,940 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 16 ms
18,784 KB
testcase_22 AC 78 ms
21,012 KB
testcase_23 AC 109 ms
20,952 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 16 ms
18,892 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 16 ms
18,788 KB
testcase_32 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = trim(fgets(STDIN));
$visited = array();
$ret = move(1, $visited, 1);
if ( $ret === FALSE ) {
	echo '-1'.PHP_EOL;
}
else {
	echo $ret.PHP_EOL;
}

function move ( $start, &$visited, $time ) {
	global $n;
	if (
		(array_search($start, $visited) !== FALSE) ||	// すでに訪問済
		($start < 1) ||									// 範囲外(マイナス方向)
		($start > $n)									// 範囲外(プラス方向)
	) {
		return FALSE;
	}
	$visited[] = $start;
	// ゴール到着
	if ( $start == $n ) {
		return $time;
	}
	
	$bit = decbin($start);
	$counts = count_chars($bit, 1);
	$count = $counts[ord('1')];
	$time++;
	return select_waypoint(move($start+$count, $visited, $time), move($start-$count, $visited, $time));
}

function select_waypoint($a, $b) {
	if ( $a === FALSE ) {
		return $b;
	}
	else if ( $b === FALSE ) {
		return $a;
	}
	else if ( ($a === FALSE) && ($b === FALSE) ) {
		return FALSE;
	}
	else {
		return min($a, $b);
	}
}
0