updated on 2019-03-07
<?php $array=explode(" ",fgets(STDIN)); $array=filter_var_array($array,FILTER_SANITIZE_NUMBER_INT); $height=$array[0]; $width=$array[1]; for ($i=0; $i < $height; $i++) { $line[$i]=str_split(fgets(STDIN)); } $x = 0; $y = 0; $count = 0; $beam = 'x_go'; while( 0<= $x && $x < $width && 0 <= $y && $y < $height){ // マスが\の時 \の時 _の時で場合分け if($line[$y][$x] === "\\"){ if($beam === 'x_go'){ $beam = 'y_back'; } elseif($beam === 'x_back'){ $beam = 'y_go'; } elseif($beam === 'y_go'){ $beam = 'x_back'; } elseif($beam === 'y_back'){ $beam = 'x_go'; } } elseif($line[$y][$x] === '/'){ if($beam === 'x_go'){ $beam = 'y_go'; } elseif($beam === 'x_back'){ $beam = 'y_back'; } elseif($beam === 'y_go'){ $beam = 'x_go'; } elseif($beam === 'y_back'){ $beam = 'x_back'; } } switch ($beam) { case 'x_go': $x++; break; case 'x_back': $x--; break; case 'y_go': $y--; break; case 'y_back': $y++; break; default: echo "no test case!"; break; } $count++; } echo $count.PHP_EOL;