fork download
  1. #!/usr/bin/php
  2. <?php
  3.  
  4. class GridHelper
  5. {
  6. private $d1 = 1;
  7. private $d2 = 1;
  8.  
  9. private $saved_paths = array();
  10. private $visited = array("1,1"=>true);
  11. private $path = "";
  12. private $fail = 0;
  13.  
  14. public function __tostring()
  15. {
  16. return "GridHelper";
  17. }
  18.  
  19. public function seek()
  20. {
  21. while ($this->fail < 2000)
  22. {
  23. $this->reset();
  24. $this->path();
  25. }
  26. return count($this->saved_paths);
  27. }
  28.  
  29. public function path()
  30. {
  31. $point = $this->getNextPoint();
  32. $point_as_string = join(",",$point);
  33. if ($point_as_string == "0,0")
  34. {
  35. $this->fail += 1;
  36. $this->path = "";
  37. return;
  38. }
  39. if ($point_as_string == "4,4")
  40. {
  41. if (!isset($this->saved_paths[$this->path]))
  42. {
  43. $this->saved_paths[$this->path] = $this->path;
  44. $this->fail = 0;
  45. }
  46. $this->path = "";
  47. return;
  48. }
  49. return $this->path();
  50. }
  51.  
  52. public function reset()
  53. {
  54. $this->d1 = 1;
  55. $this->d2 = 1;
  56. $this->visited = array("1,1"=>true);
  57. }
  58.  
  59.  
  60. private function getNextPoint()
  61. {
  62. $x = array();
  63.  
  64. if (($this->d1 != 1)&&
  65. (!isset($this->visited[join(",",array($this->d1 -1,$this->d2))])))
  66. {
  67. array_push($x,array($this->d1 -1,$this->d2));
  68. }
  69. if (($this->d1 != 4)
  70. &&(!isset($this->visited[join(",",array($this->d1 +1,$this->d2))])))
  71. {
  72. array_push($x,array($this->d1 +1,$this->d2));
  73. }
  74. if (($this->d2 != 1)
  75. &&(!isset($this->visited[join(",",array($this->d1,$this->d2 -1))])))
  76. {
  77. array_push($x,array($this->d1,$this->d2 -1));
  78. }
  79. if (($this->d2 != 4)&&
  80. (!isset($this->visited[join(",",array($this->d1,$this->d2 +1))])))
  81. {
  82. array_push($x,array($this->d1,$this->d2 +1));
  83. }
  84.  
  85. $l = count($x);
  86. $new_point_array = array(0,0);
  87. if ($l > 0)
  88. {
  89. shuffle($x);
  90. $new_point_array = $x[array_rand($x,1)];
  91. $this->d1 = $new_point_array[0];
  92. $this->d2 = $new_point_array[1];
  93. $new_point_array_as_string = join(",",$new_point_array);
  94. $this->visited[$new_point_array_as_string] = true;
  95. $this->path .= $new_point_array_as_string." ";
  96. }
  97. return $new_point_array;
  98. }
  99.  
  100. }
  101.  
  102. $g = new GridHelper();
  103. print $g->seek()."\n";
  104.  
  105. exit;
  106. ?>
Success #stdin #stdout 0.01s 4980KB
stdin
Standard input is empty
stdout
Standard output is empty