Code of david
<?php
// Code by David Cann: david@battlebots.com
// Get data from map.txt
$mapfile = fopen ("map.txt", "r");
// Stack up output into one Array
while ($line = fgets ($mapfile)){
// Explode variables into map array
$mapsrc[] = explode(" ", $line);
}
foreach ($mapsrc AS $street){
if ($street[3] == 2){
$addstreet[0] = $street[2];
$addstreet[1] = $street[1];
$addstreet[2] = $street[0];
$addstreet[3] = $street[3];
$addtomap[] = $addstreet;
}
}
$mapsrc = array_merge($mapsrc, $addtomap);
//echo "<pre>";
//print_r ($mapsrc);
//echo "</pre>";
$letters = array();
foreach ($mapsrc AS $street){
$distances[$street[0] . $street[2]][0] = $street[1];
$distances[$street[0] . $street[2]][1] = $street[0] . $street[2];
if (!in_array($street[0], $letters)){
$letters[] = $street[0];
}
if (!in_array($street[2], $letters)){
$letters[] = $street[2];
}
}
//echo "<pre>";
//print_r ($distances);
//echo "</pre>";
$map = $distances;
$letters_temp = $letters;
function crawl($b, $trail, $traildistance){
global $letters_temp, $map, $bigtemthisarray, $d;
$d++;
$letters = $letters_temp;
foreach ($letters AS $letter){
$traildistance_temp = $traildistance;
$tempdistance = 0;
if (strpos($trail, $letter) === false){
$newstreet = substr($trail, 0, 1) . $letter;
$newcombo = $b . $letter;
$newtrail = $trail . $letter;
$tempdistance = $map[$newcombo][0];
if ($tempdistance > 0){
$traildistance_temp += $tempdistance;
$findshorter = $bigtemthisarray[$newstreet][0];
if (!is_numeric($findshorter) || $traildistance_temp < $findshorter){
$bigtemthisarray[$newstreet][0] = $traildistance_temp;
$bigtemthisarray[$newstreet][1] = $newtrail;
}
// if ($traildistance < 500){
crawl($letter, $newtrail, $traildistance_temp);
// }
}
}
}
}
//echo "<pre>";
//print_r ($distances);
//echo "</pre>";
$bigtemthisarray = $distances;
foreach ($letters AS $letter){
crawl($letter, $letter, 0);
}
//echo "<pre>";
//print_r ($bigtemthisarray);
//echo "</pre>";
$distances = $bigtemthisarray;
// Get data from dest.txt
$destfile = fopen ("dest.txt", "r");
// Stack up output into one Array
while ($line = fgets ($destfile)){
// Explode variables into map array
$route = explode(" ", $line);
}
//echo "<pre>";
//print_r ($route);
//echo "</pre>";
// Route
$thisarr = "";
$n = count($route);
for ($i=0; $i <= $n; $i++){
$thisarr[$i]=$i;
}
function movenext(){
global $thisarr,$n;
$k = $n - 1;
while ($thisarr[$k] > $thisarr[$k+1]){
$k--;
}
if ($k == 0){
return(0);
} else {
$j = $n;
while ($thisarr[$k] > $thisarr[$j]){
$j--;
}
flip($j,$k);
$r = $n;
$s = $k + 1;
while ($r > $s){
flip($r,$s);
$r--;
$s++;
}
}
addtoarray();
return(1);
}
function addtoarray(){
global $route, $thisarr, $n, $distances, $finalroutes, $time_start;
$lastl = "";
$thisl = "";
$noshow = 0;
for ($i=1; $i <= $n; $i++){
$thisl = $route[$thisarr[$i]-1];
if ($lastl != "" && $thisl != ""){
$thisdistance = $distances[$lastl . $thisl][0];
if ($thisdistance != ""){
$thistotal += $thisdistance;
} else {
$noshow = 1;
}
}
// Execute the first line for basic route output, second line for comprehensive route instructions
$thisstr .= $thisl;
// $thisstr .= $distances[$lastl . $thisl][1];
$lastl = $route[$thisarr[$i]-1];
}
if (!$noshow){
$finalroutes[$thisstr] = $thistotal;
}
return;
}
function flip($i,$j){
global $thisarr;
$temp1 = $thisarr[$i];
$thisarr[$i] = $thisarr[$j];
$thisarr[$j] = $temp1;
}
addtoarray();
while( movenext() );
//echo "<pre>";
//print_r ($finalroutes);
//echo "</pre>";
if (is_array($finalroutes)){
asort($finalroutes);
$k = 0;
foreach ($finalroutes AS $points => $distance){
if ($k == 0){
$i = 0;
$pointsstr = "";
$thischar = "";
$lastchar = "";
while ($i < strlen($points)){
$thischar = substr($points, $i, 1);
if ($thischar != $lastchar){
$pointsstr .= $thischar ." ";
}
$lastchar = $thischar;
$i++;
}
echo $pointsstr . $distance;
// echo "<br>";
} else {
exit();
}
// Hide the following line to show all possible routes
$k++;
}
}
?>
Back to results
|
|