Guest User

Untitled

a guest
Dec 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. <?php
  2.  
  3. class Paiza {
  4. protected function getInput() {
  5. $array = [];
  6. while ($line = fgets(STDIN)) {
  7. if ($line !== '') {
  8. $array[] = trim($line);
  9. }
  10. }
  11. $input = [];
  12. foreach ($array as $key => $value) {
  13. $input[] = explode(" ", $value);
  14. }
  15. return $input;
  16. }
  17. protected function println($str) {
  18. echo $str."\n";
  19. }
  20. protected function key_max($array) {
  21. return max(array_keys($array));
  22. }
  23. }
  24.  
  25. class B044 extends Paiza {
  26.  
  27. private $state;
  28.  
  29. public function __construct() {
  30. foreach ($input = parent::getInput() as $key => $line) {
  31. if ($key === 0 || $key === 1 || $key === parent::key_max($input)) continue;
  32. $tmp = str_split($line[0]);
  33. unset($tmp[0]);
  34. array_pop($tmp);
  35. $this->state[] = array_values($tmp);
  36. }
  37. }
  38.  
  39. public function main() {
  40. foreach ($this->state as $row => $line) {
  41. foreach ($line as $col => $val) {
  42. if (preg_match('/^[1-9]$/', $val)) {
  43. $this->explode($row, $col, $val);
  44. }
  45. }
  46. }
  47. $this->judge();
  48. }
  49.  
  50. private function explode($row, $col, $power) {
  51. $stop_left = false;
  52. $stop_right = false;
  53. $stop_above = false;
  54. $stop_under = false;
  55. for ($i = 1; $i <= $power; $i++) {
  56. // 左に爆発
  57. if (array_key_exists($col - $i, $this->state[$row]) && !$stop_left) {
  58. if ($this->state[$row][$col - $i] === '#') {
  59. $stop_left = true;
  60. } else if (preg_match('/^[X\.]$/', $this->state[$row][$col - $i])) {
  61. $this->state[$row][$col - $i] = '.';
  62. }
  63. }
  64. // 右に爆発
  65. if (array_key_exists($col + $i, $this->state[$row]) && !$stop_right) {
  66. if ($this->state[$row][$col + $i] === '#') {
  67. $stop_right = true;
  68. } else if (preg_match('/^[X\.]$/', $this->state[$row][$col + $i])) {
  69. $this->state[$row][$col + $i] = '.';
  70. }
  71. }
  72. // 上に爆発
  73. if (array_key_exists($row - $i, $this->state) && !$stop_above) {
  74. if ($this->state[$row - $i][$col] === '#') {
  75. $stop_above = true;
  76. } else if (preg_match('/^[X\.]$/', $this->state[$row - $i][$col])) {
  77. $this->state[$row - $i][$col] = '.';
  78. }
  79. }
  80. // 下に爆発
  81. if (array_key_exists($row + $i, $this->state) && !$stop_under) {
  82. if ($this->state[$row + $i][$col] === '#') {
  83. $stop_under = true;
  84. } else if (preg_match('/^[X\.]$/', $this->state[$row + $i][$col])) {
  85. $this->state[$row + $i][$col] = '.';
  86. }
  87. }
  88. }
  89. }
  90.  
  91. private function judge() {
  92. foreach ($this->state as $row => $line) {
  93. foreach ($line as $col => $val) {
  94. if ($val === 'X') {
  95. parent::println('NO');
  96. exit;
  97. }
  98. }
  99. }
  100. parent::println('YES');
  101. }
  102. }
  103.  
  104. $b044 = new B044();
  105. $b044->main();
Add Comment
Please, Sign In to add comment