Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Paiza {
- protected function getInput() {
- $array = [];
- while ($line = fgets(STDIN)) {
- if ($line !== '') {
- $array[] = trim($line);
- }
- }
- $input = [];
- foreach ($array as $key => $value) {
- $input[] = explode(" ", $value);
- }
- return $input;
- }
- protected function println($str) {
- echo $str."\n";
- }
- protected function key_max($array) {
- return max(array_keys($array));
- }
- }
- class B044 extends Paiza {
- private $state;
- public function __construct() {
- foreach ($input = parent::getInput() as $key => $line) {
- if ($key === 0 || $key === 1 || $key === parent::key_max($input)) continue;
- $tmp = str_split($line[0]);
- unset($tmp[0]);
- array_pop($tmp);
- $this->state[] = array_values($tmp);
- }
- }
- public function main() {
- foreach ($this->state as $row => $line) {
- foreach ($line as $col => $val) {
- if (preg_match('/^[1-9]$/', $val)) {
- $this->explode($row, $col, $val);
- }
- }
- }
- $this->judge();
- }
- private function explode($row, $col, $power) {
- $stop_left = false;
- $stop_right = false;
- $stop_above = false;
- $stop_under = false;
- for ($i = 1; $i <= $power; $i++) {
- // 左に爆発
- if (array_key_exists($col - $i, $this->state[$row]) && !$stop_left) {
- if ($this->state[$row][$col - $i] === '#') {
- $stop_left = true;
- } else if (preg_match('/^[X\.]$/', $this->state[$row][$col - $i])) {
- $this->state[$row][$col - $i] = '.';
- }
- }
- // 右に爆発
- if (array_key_exists($col + $i, $this->state[$row]) && !$stop_right) {
- if ($this->state[$row][$col + $i] === '#') {
- $stop_right = true;
- } else if (preg_match('/^[X\.]$/', $this->state[$row][$col + $i])) {
- $this->state[$row][$col + $i] = '.';
- }
- }
- // 上に爆発
- if (array_key_exists($row - $i, $this->state) && !$stop_above) {
- if ($this->state[$row - $i][$col] === '#') {
- $stop_above = true;
- } else if (preg_match('/^[X\.]$/', $this->state[$row - $i][$col])) {
- $this->state[$row - $i][$col] = '.';
- }
- }
- // 下に爆発
- if (array_key_exists($row + $i, $this->state) && !$stop_under) {
- if ($this->state[$row + $i][$col] === '#') {
- $stop_under = true;
- } else if (preg_match('/^[X\.]$/', $this->state[$row + $i][$col])) {
- $this->state[$row + $i][$col] = '.';
- }
- }
- }
- }
- private function judge() {
- foreach ($this->state as $row => $line) {
- foreach ($line as $col => $val) {
- if ($val === 'X') {
- parent::println('NO');
- exit;
- }
- }
- }
- parent::println('YES');
- }
- }
- $b044 = new B044();
- $b044->main();
Add Comment
Please, Sign In to add comment