Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2. $fieldSize = intval(readline());
  3. $bugIndexes = explode(" ", readline());
  4. $field = array_fill(0, $fieldSize, 0);
  5.  for($i = 0; $i < count($bugIndexes); $i++) {
  6.      if($bugIndexes[$i] >= 0 && $bugIndexes[$i] < $fieldSize) {
  7.          $field[$bugIndexes[$i]] = 1;
  8.      }
  9.  }
  10. while(($command = readline()) != "end") {
  11.     $command = explode(" ", $command);
  12.     $newBugIndex = $command[0];
  13.     $flightDirection = $command[1];
  14.     $flightLength = $command[2];
  15.      if($flightLength < 0) {
  16.          switch($flightDirection) {
  17.              case "left": $flightDirection = "right"; break;
  18.              case "right": $flightDirection = "left"; break;
  19.          }
  20.      }
  21.       if($newBugIndex >= 0 && $newBugIndex < $fieldSize) {
  22.           if($field[$newBugIndex] == 1) {
  23.               $field[$newBugIndex] = 0;
  24.                if($flightDirection == "left") {
  25.                    $flightLength = abs($flightLength);
  26.                    for($i = $newBugIndex - $flightLength; $i >= 0; $i = $i - $flightLength) {
  27.                        if($field[$i] == 0) {
  28.                            $field[$i] = 1;
  29.                            break;
  30.                        }
  31.                    }
  32.                }
  33.                if($flightDirection == "right") {
  34.                    $flightLength = abs($flightLength);
  35.                    for($i = $newBugIndex + $flightLength; $i < count($field); $i = $i + $flightLength) {
  36.                        if($field[$i] == 0) {
  37.                            $field[$i] = 1;
  38.                            break;
  39.                        }
  40.                    }
  41.                }
  42.           }
  43.       }
  44. }
  45.  foreach($field as $output) {
  46.      echo "$output ";
  47.  }
  48. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement