function get_result($t, $ladders, $snakes) { fill_graph($t, $ladders); fill_graph($t, $snakes); $len = count($t); $vertices = array(); for ($k = 0; $k < $len; $k++) { $vertices[$k] = new vertex($k); } $adjacencyList = array(); for ($u = 0; $u < $len; $u++) { $list = new SplDoublyLinkedList(); for ($v = 0; $v < $len; $v++) { if ($t[$u][$v] != 0) { $list->push(array('vertex' => $vertices[$v], 'distance' => $t[$u][$v])); } } $list->rewind(); $adjacencyList[] = $list; } calcShortestPaths($vertices[0], $adjacencyList); $path = end($vertices)->path; $result = $p = 0; for ($n = 0; $n < count($path); $n++) { $p++; if (@$path[$n + 1] - $path[$n] != 1) { $result = $result + ceil(($p - 1) / 6); $p = 0; } } //echo "[" . implode(', ', $path) . "]\n\n"; return $result; }
$q = array(); // push the starting vertex into the queue array_push($q, $start); // color it gray $start->color = 'gray'; // mark the distance to it 0 $start->distance = 0; // the path to the starting vertex $start->path = new SplDoublyLinkedList(); $start->path->push($start->key); while ($q) { // 1. pop from the queue $t = array_pop($q); // 2. foreach poped item find it's adjacent white vertices $l = $adjLists[$t->key]; while ($l->valid()) { // 3. mark them gray, increment their length with one from their parent if ($l->current()->color == 'white') { $l->current()->color = 'gray'; $l->current()->distance = $t->distance + 1; $l->current()->path = clone $t->path; $l->current()->path->push($l->current()->key); // 4. push them to the queue array_push($q, $l->current()); } $l->next(); } } } calcShortestPaths($v0, $adjacencyList); print_r($adjacencyList);
<table class="table table-hover"> <thead> <th> Vértice </th> <th> Somatório </th> </thead> <tbody> <?php $sum = []; for ($x = 0; $x < 6; $x++) { $sum[$x] = 0; $vertexName = "v{$x}"; calcShortestPaths(${$vertexName}, $adjacencyList); for ($j = 0; $j < 6; $j++) { $vertexJName = "v{$j}"; // echo '[' . implode(', ', $$vertexJName->path) . ']'.PHP_EOL; // echo $$vertexJName->distance.PHP_EOL; $sum[$x] += ${$vertexJName}->distance; } ?> <tr> <td> <?php echo $vertexName; ?> </td> <td> <?php