【ロボット暴走】FINAL問題 ロボットの暴走 (paizaランク A 相当) 解答例 – PHP編【クラス・構造体メニュー】
【クラス・構造体メニュー】 > 【ロボット暴走】FINAL問題 ロボットの暴走 (paizaランク A 相当)
※リンク先へ移動する為には「paiza」へのログインが必要です。
この問題おかしくない?
「工場の外に出ないことは保証されている」って条件があったからH、Wの範囲で収まるようにストッパーをかけてたら連続不正解になった。ストッパーを外したら正解って、なんか問題としておかしいような…。工場から余裕ででてるやーん!
解答例
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
<?php class robot{ public $id; public $lv; public $move; public $x; public $y; public function __construct($id ,$lv , $x , $y){ $this->id = $id; $this->lv = $lv; $this->x = $x; $this->y = $y; if($lv == 1){ $this->move = 1; } elseif($lv == 2){ $this->move = 2; } elseif($lv == 3){ $this->move = 5; } elseif($lv == 4){ $this->move = 10; } } public function lvup(){ $this->lv++; //echo $this->id."は".$this->lv."にレベルアップ"; if($this->lv == 2){ $this->move = 2; } elseif($this->lv == 3){ $this->move = 5; } elseif($this->lv == 4){ $this->move = 10; } } public function robot_move($a){ global $h; //グローバル宣言が必要 global $w; //グローバル宣言が必要 global $xbox; //グローバル宣言が必要 global $ybox; //グローバル宣言が必要 if($a == "N"){ $this->y -= $this->move; /*if($this->y < 0){ $this->y = 0; }*/ for($i = 0;$i < 10;$i++){ if($this->x == $xbox[$i] AND $this->y == $ybox[$i]){ if($this->lv < 4){ $this->lvup(); break; } } } } elseif($a == "W"){ $this->x -= $this->move; /*if($this->x < 0){ $this->x = 0; } */ for($i = 0;$i < 10;$i++){ if($this->x == $xbox[$i] AND $this->y == $ybox[$i]){ if($this->lv < 4){ $this->lvup(); break; } } } } elseif($a == "E"){ $this->x += $this->move; /*if($this->x > $w){ $this->x = $w; } */ for($i = 0;$i < 10;$i++){ if($this->x == $xbox[$i] AND $this->y == $ybox[$i]){ if($this->lv < 4){ $this->lvup(); break; } } } } elseif($a == "S"){ $this->y += $this->move; /*if($this->y > $h){ $this->y = $h; } */ for($i = 0;$i < 10;$i++){ if($this->x == $xbox[$i] AND $this->y == $ybox[$i]){ if($this->lv < 4){ $this->lvup(); break; } } } } } } $input = explode(" ",trim(fgets(STDIN))); $h = $input[0] - 1; //y座標マス数 $w = $input[1] - 1; //x座標マス数 $n = $input[2]; //ロボットの数 $k = $input[3]; //ロボットの移動数 $xbox = array(); //工具箱のx座標格納配列 $ybox = array(); //工具箱のy座標格納配列 for($i = 0;$i < 10;$i++){ $test = explode(" ",trim(fgets(STDIN))); $x = $test[0]; $y = $test[1]; $xbox[] = $x; $ybox[] = $y; } //print_r($ybox); $robot_array = array(); //ロボットの情報を格納する配列 for($i = 1;$i <= $n;$i++){ $test = explode(" ",trim(fgets(STDIN))); $x = $test[0]; $y = $test[1]; $lv = $test[2]; $robot_array[$i] = new robot($i , $lv , $x , $y); } //print_r($robot_array); for($i = 0;$i < $k;$i++){ $kk = explode(" ",trim(fgets(STDIN))); $num = $kk[0]; $way = $kk[1]; $robot_array[$num]->robot_move($way); } //print_r($robot_array); foreach($robot_array as $value){ echo $value->x." ".$value->y." ".$value->lv; echo "\n"; } ?> |