fork download
  1. <?php
  2.  
  3. /* http://d...content-available-to-author-only...2.net/ */
  4.  
  5. define('SUBWAY', 'sub');
  6. define('FOOT', 'foot');
  7. define('BUS', 'bus');
  8.  
  9. $transportName = array(
  10. SUBWAY => 'едешь на метро',
  11. FOOT => 'идешь пешком',
  12. BUS => 'едешь на автобусе'
  13. );
  14.  
  15. $startPoint = 'pet'; // Петроградская
  16. $endPoint = 'nov'; // Новая Голландия
  17.  
  18. $pointNames = array(
  19. 'pet' => 'ст. м. Петроградская',
  20. 'chk' => 'ст. м. Чкаловская',
  21. 'gor' => 'ст. м. Горьковская',
  22. 'spo' => 'ст. м. Спортивная',
  23. 'vas' => 'ст. м. Василеостровская',
  24. 'kre' => 'Петропавловская крепость',
  25. 'let' => 'Летний сад',
  26. 'dvo' => 'Дворцовая площадь',
  27. 'isa' => 'Исакиевский собор',
  28. 'nov' => 'Новая Голландия',
  29. 'ras' => 'Дом Раскольникова',
  30. 'gos' => 'Гостиный Двор',
  31. 'sen' => 'Сенная Площадь',
  32. 'vla' => 'ст. м. Владимирская',
  33. 'vit' => 'Витебский вокзал',
  34. 'teh' => 'Технологический Институт'
  35. );
  36.  
  37. $paths = array(
  38. 'pet' => array(
  39. 'chk' => canGet(10, BUS),
  40. 'gor' => canGet(3, SUBWAY)
  41. ),
  42.  
  43. 'chk' => array(
  44. 'pet' => canGet(10, BUS),
  45. 'spo' => canGet(3, SUBWAY)
  46. ),
  47.  
  48. 'gor' => array(
  49. 'pet' => canGet(3, BUS),
  50. 'kre' => canGet(5, FOOT),
  51. 'gos' => canGet(6, SUBWAY)
  52. ),
  53.  
  54. 'spo' => array(
  55. 'chk' => canGet(3, SUBWAY),
  56. 'vas' => canGet(10, BUS),
  57. 'sen' => canGet(7, SUBWAY)
  58. ),
  59.  
  60. 'vas' => array(
  61. 'spo' => canGet(10, BUS),
  62. 'gos' => canGet(7, SUBWAY),
  63. 'nov' => canGet(11, FOOT)
  64. ),
  65.  
  66. 'kre' => array(
  67. 'gor' => canGet(5, FOOT)
  68. ),
  69.  
  70. 'let' => array(
  71. 'dvo' => canGet(6, FOOT),
  72. 'gos' => canGet(7, FOOT)
  73. ),
  74.  
  75. 'dvo' => array(
  76. 'isa' => canGet(6, FOOT),
  77. 'gos' => canGet(6, FOOT),
  78. 'let' => canGet(6, FOOT)
  79. ),
  80.  
  81. 'isa' => array(
  82. 'dvo' => canGet(6, FOOT),
  83. 'nov' => canGet(5, FOOT)
  84. ),
  85.  
  86. 'nov' => array(
  87. 'vas' => canGet(11, FOOT),
  88. 'isa' => canGet(5, FOOT),
  89. 'ras' => canGet(7, BUS)
  90. ),
  91.  
  92. 'ras' => array(
  93. 'nov' => canGet(7, BUS),
  94. 'sen' => canGet(3, FOOT)
  95. ),
  96.  
  97. 'gos' => array(
  98. 'vas' => canGet(7, SUBWAY),
  99. 'sen' => canGet(3, SUBWAY),
  100. 'dvo' => canGet(6, FOOT),
  101. 'gor' => canGet(6, SUBWAY),
  102. 'let' => canGet(7, FOOT),
  103. 'vla' => canGet(7, FOOT)
  104. ),
  105.  
  106. 'sen' => array(
  107. 'ras' => canGet(3, FOOT),
  108. 'spo' => canGet(7, SUBWAY),
  109. 'gos' => canGet(3, SUBWAY),
  110. 'vla' => canGet(4, SUBWAY),
  111. 'vit' => canGet(2, SUBWAY),
  112. 'teh' => canGet(3, SUBWAY)
  113. ),
  114.  
  115. 'vla' => array(
  116. 'sen' => canGet(4, SUBWAY),
  117. 'gos' => canGet(7, FOOT),
  118. 'vit' => canGet(3, SUBWAY)
  119. ),
  120.  
  121. 'vit' => array(
  122. 'sen' => canGet(2, SUBWAY),
  123. 'teh' => canGet(2, SUBWAY),
  124. 'vla' => canGet(3, SUBWAY)
  125. ),
  126.  
  127. 'teh' => array(
  128. 'sen' => canGet(3, SUBWAY),
  129. 'vit' => canGet(2, SUBWAY)
  130. )
  131. );
  132.  
  133. /* Чтобы не писать много раз array('time' => ..., 'by' => ...), используем функцию.
  134. «canGet» переводится как «можно попасть» */
  135. function canGet($time, $byWhat)
  136. {
  137. return array(
  138. 'time' => $time,
  139. 'by' => $byWhat
  140. );
  141. }
  142.  
  143. function oneStep($paths, $pathDone, $time, $point, $target)
  144. {
  145. $result = array();
  146.  
  147. if (!$paths[$point][$target]) {
  148. foreach ($paths[$point] as $key => $val) {
  149. if (in_array($key, $pathDone)) {
  150. continue;
  151. }
  152.  
  153. $pathDone[] = $key;
  154. $time = $time + $paths[$point][$key]['time'];
  155. $container = oneStep($paths, $pathDone, $time, $key, $target);
  156.  
  157. if ($time > $container['time']) {
  158. continue;
  159. }
  160.  
  161. return $container;
  162.  
  163. }
  164.  
  165.  
  166.  
  167.  
  168.  
  169. }
  170.  
  171. else {
  172.  
  173.  
  174. $result['path'] = $pathDone;
  175. $result['time'] = $paths[$point][$target]['time'] + $time;
  176. $pathDone = array();
  177. $time = 0;
  178. return $result;
  179.  
  180. }
  181. }
  182. $testar = array();
  183. $testvar = oneStep($paths, $testar, 0, 'vas', 'teh');
  184. var_dump($testvar);
Success #stdin #stdout #stderr 0.01s 20520KB
stdin
Standard input is empty
stdout
array(2) {
  ["path"]=>
  array(11) {
    [0]=>
    string(3) "spo"
    [1]=>
    string(3) "chk"
    [2]=>
    string(3) "pet"
    [3]=>
    string(3) "gor"
    [4]=>
    string(3) "kre"
    [5]=>
    string(3) "gos"
    [6]=>
    string(3) "vas"
    [7]=>
    string(3) "nov"
    [8]=>
    string(3) "isa"
    [9]=>
    string(3) "ras"
    [10]=>
    string(3) "sen"
  }
  ["time"]=>
  int(73)
}
stderr
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148
PHP Notice:  Undefined index: teh in /home/SGapDo/prog.php on line 148