Created
May 13, 2015 11:47
-
-
Save backy22/fd37236a8907bf001b0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$inputData = explode(' ', fgets(STDIN)); | |
$depthLength = $inputData[0]; | |
$countPosition = $inputData[1]; | |
$maxMove = $inputData[2]; | |
$moves = []; | |
for($i=1; $i<=$maxMove; $i++){ | |
$eachData = explode(' ', fgets(STDIN)); | |
$moves[] = [ | |
'position' => $eachData[0], | |
'startDepth' => $eachData[1], | |
'endDepth' => $eachData[2], | |
]; | |
} | |
$currentPosition = 1; | |
$currentDepth = 0; | |
$prevPosition = 0; | |
$prevDepth = 0; | |
while(1==1){ | |
if($prevPosition != $currentPosition or $prevDepth != $currentDepth){ | |
list($currentPosition, $currentDepth) = move($currentPosition, $currentDepth, $moves, $countPosition); | |
}else{ | |
break; | |
} | |
$prevPosition = $currentPosition; | |
$prevDepth = $currentDepth; | |
} | |
echo $currentPosition; | |
function move($position, $depth, $moves, $countPosition){ | |
//右行きで一番近い線 | |
if($position <$countPosition){ | |
$rightMove = []; | |
$minRightBetween = 10000; | |
foreach($moves as $move){ | |
if($move['position'] == $position && $move['startDepth'] > $depth){ | |
if($minRightBetween > ($move['startDepth'] - $depth)){ | |
$minRightBetween = $move['startDepth'] - $depth; | |
$rightMove = $move; | |
} | |
} | |
} | |
}else{ | |
$minRightBetween = 10000; | |
} | |
//左行きで一番近い線 | |
if($position > 0){ | |
$leftMove = []; | |
$minLeftBetween = 10000; | |
foreach($moves as $move){ | |
if(($move['position'] + 1) == $position && $move['endDepth'] > $depth){ | |
if($minLeftBetween > ($move['endDepth'] - $depth)){ | |
$minLeftBetween = $move['endDepth'] - $depth; | |
$leftMove = $move; | |
} | |
} | |
} | |
}else{ | |
$minLeftBetween = 10000; | |
} | |
if(!isset($rightMove['position']) && !isset($leftMove['position'])){ | |
return [$position, $depth]; | |
} | |
if($minRightBetween <= $minLeftBetween){ | |
return [$rightMove['position'], $rightMove['endDepth']]; | |
}else{ | |
return [$leftMove['position'], $leftMove['startDepth']]; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment