fork download
  1. <style>
  2. span {
  3. width: 30px;
  4. border: 1px solid #ccc;
  5. background: #eee;
  6. border-radius: 3px;
  7. display: inline-block;
  8. text-align: center;
  9. font-family: Tahoma;
  10. margin: 1px;
  11. padding: 5px 0;
  12. }
  13. </style>
  14.  
  15.  
  16. <?php
  17.  
  18.  
  19. class Triangle {
  20.  
  21. /**
  22.   * @var integer
  23.   */
  24. private $levels_count;
  25.  
  26. /**
  27.   * @var array
  28.   */
  29. private $structure = [];
  30.  
  31. /**
  32.   * @var array
  33.   */
  34. private $path = [];
  35.  
  36. /**
  37.   * Class Constuctor.
  38.   *
  39.   * @param integer $levels_count Count of trianlge levels
  40.   */
  41. public function __construct($levels_count = 3) {
  42. $this->levels_count = $levels_count;
  43. }
  44.  
  45.  
  46. /**
  47.   * Build the triangle structure
  48.   *
  49.   * @return Trianlge
  50.   */
  51. public function build() {
  52. for($i = 1; $i <= $this->levels_count; $i++) {
  53. $level = [];
  54. while(true) {
  55. $level[] = rand(0, 9);
  56. $level = array_unique($level);
  57. if(count($level) == $i)
  58. break;
  59. }
  60. $this->structure[] = $level;
  61. }
  62. return $this;
  63. }
  64.  
  65.  
  66. /**
  67.   * Find the shortest path
  68.   *
  69.   * @return array
  70.   */
  71. public function findPath() {
  72. $node = 0;
  73. foreach ($this->getStructure() as $level) {
  74. if(count($level) == 1) {
  75. $node = [
  76. 'index' => 0,
  77. 'value' => $level[0],
  78. ];
  79. }
  80. else {
  81. // $slice = ($node['index'] == 0) ? array_slice($level, 0, 2) : array_slice($level, $node['index']-1, 3);
  82. if($node['index'] == 0) {
  83. $slice = array_slice($level, 0, 2);
  84. }
  85. else
  86. $slice = array_slice($level, $node['index']-1, 3);
  87.  
  88. print_r('index '.$node['index']);
  89. echo ' ';
  90. print_r($slice);
  91.  
  92. $node = [
  93. 'index' => array_search(min($slice), $level),
  94. 'value' => min($slice),
  95. ];
  96. print_r(' / new index '.$node['index']);
  97. echo '<br>';
  98. }
  99. $this->path[] = $node;
  100. }
  101. return $this;
  102. }
  103.  
  104.  
  105. /**
  106.   * Gets the triangle structure.
  107.   *
  108.   * @return array
  109.   */
  110. public function getStructure() {
  111. return $this->structure;
  112. }
  113.  
  114.  
  115. /**
  116.   * Gets the triangle path.
  117.   *
  118.   * @return array
  119.   */
  120. public function getPath() {
  121. return $this->path;
  122. }
  123. }
  124.  
  125. $triangle = new Triangle(8);
  126. $triangle->build()->findPath();
  127. echo '<hr>';
  128.  
  129. foreach ($triangle->getPath() as $node) {
  130. echo $node['value'] . ' + ';
  131. }
  132.  
  133.  
  134. echo '<hr>';
  135. foreach ($triangle->getStructure() as $k => $level) {
  136. $current_node = $triangle->getPath()[$k];
  137. echo '<div>';
  138. foreach($level AS $key => $value) {
  139. $class = ($current_node['index'] == $key) ? 'red' : '';
  140. echo '<span style="color:'.$class.'">'.$value.'</span>';
  141. }
  142. echo '</div>';
  143. }
  144.  
Success #stdin #stdout 0.02s 82880KB
stdin
Standard input is empty
stdout
<style>
	span {
		width: 30px;
		border: 1px solid #ccc;
		background: #eee;
		border-radius: 3px;
		display: inline-block;
		text-align: center;
		font-family: Tahoma;
		margin: 1px;
		padding: 5px 0;
	}
</style>


index 0 Array
(
    [0] => 1
    [1] => 4
)
 / new index 0<br>index 0 Array
(
    [0] => 9
    [1] => 4
)
 / new index 2<br>index 2 Array
(
    [0] => 7
    [1] => 0
    [2] => 5
)
 / new index 2<br>index 2 Array
(
    [0] => 9
    [1] => 8
    [2] => 7
)
 / new index 3<br>index 3 Array
(
    [0] => 8
    [1] => 6
    [2] => 2
)
 / new index 4<br>index 4 Array
(
    [0] => 5
    [1] => 9
    [2] => 2
)
 / new index 7<br>index 7 Array
(
    [0] => 7
    [1] => 0
)
 / new index 9<br><hr>7 + 1 + 4 + 0 + 7 + 2 + 2 + 0 + <hr><div><span style="color:red">7</span></div><div><span style="color:red">1</span><span style="color:">4</span></div><div><span style="color:">9</span><span style="color:red">4</span><span style="color:">1</span></div><div><span style="color:">6</span><span style="color:">7</span><span style="color:red">0</span><span style="color:">5</span></div><div><span style="color:">2</span><span style="color:">9</span><span style="color:">8</span><span style="color:red">7</span><span style="color:">5</span></div><div><span style="color:">9</span><span style="color:">3</span><span style="color:">8</span><span style="color:">6</span><span style="color:red">2</span><span style="color:">0</span></div><div><span style="color:">4</span><span style="color:">6</span><span style="color:">7</span><span style="color:">5</span><span style="color:">9</span><span style="color:red">2</span><span style="color:">8</span></div><div><span style="color:">8</span><span style="color:">3</span><span style="color:">9</span><span style="color:">6</span><span style="color:">1</span><span style="color:">2</span><span style="color:">7</span><span style="color:red">0</span></div>