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