Dragon Arrow written by Tatsuya Nakaji, all rights reserved animated-dragon-image-0164

ビームの反射

Feb 07, 2019

ビームの反射

<?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;