結果

問題 No.3 ビットすごろく
ユーザー 綾地寧々
提出日時 2015-05-20 19:30:37
言語 PHP
(843.2)
結果
AC  
実行時間 117 ms / 5,000 ms
コード長 886 bytes
コンパイル時間 3,188 ms
コンパイル使用メモリ 32,144 KB
実行使用メモリ 34,616 KB
最終ジャッジ日時 2024-07-01 07:21:00
合計ジャッジ時間 6,537 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
No syntax errors detected in Main.php

ソースコード

diff #

<?php
$n = trim(fgets(STDIN));
$visited = array();
$maxtime = -1;
move(1,1,1);
echo $maxtime.PHP_EOL;

function move ( $prev, $start, $time ) {
	global $n, $visited, $maxtime;
	if ( ($start < 1) || ($start > $n) ) {				// 範囲外
		return;
	}
	if ( ($maxtime != -1) && ($maxtime <= $time) ) {	// 移動回数多すぎ
		return;
	}
	
	// ゴール到着
	if ( $start == $n ) {
		$maxtime = $time;
		return;
	}
	
	if ( isset($visited[$start-1]) === TRUE ) {			// 訪問済
		if ( $visited[$start-1] <= $time ) {			// ルート問わず、高コストで到達してしまった
			return;
		}
	}
	$visited[$start-1] = $time;
	$bit = decbin($start);
	$counts = count_chars($bit, 1);
	$count = $counts[ord('1')];
	$time++;
	if ( ($start+$count) != $prev ) {
		move($start, $start+$count, $time);
	}
	if ( ($start-$count) != $prev ) {
		move($start, $start-$count, $time);
	}
	
	return;
}
0