Main Menu |
|
|
Forums |
|
|
Programming
Contest |
|
|
Documentation
|
|
|
Partner
Sites |
|
|
Sponsors |
|
|
|
Code of bahadyr
<?Php
$fp = FOpen("map.txt", "r");
$fread = FRead($fp, FileSize("map.txt"));
FClose($fp);
/* Creating my own map */
$map = Array();
For ($i = 0; $i < 12; $i++) {
For ($j = 0; $j < 12; $j++) {
$map[$i][$j] = -1;
}
}
$lines = Explode("\n", $fread);
ForEach ($lines as $info) {
$info_arr = Explode(" ", $info);
$i = Ord($info_arr[0]) - 65;
$j = Ord($info_arr[2]) - 65;
$dist = $info_arr[1];
If ($map[$i][$j] == -1 || $dist < $map[$i][$j]) {
$map[$i][$j] = $dist;
}
If ($info_arr[3] == 2) {
If ($map[$j][$i] == -1 || $dist < $map[$j][$i]) {
$map[$j][$i] = $dist;
}
}
}
/* My map is ready :) */
/* Creating Route and Distance table */
$route = Array();
$distance = Array();
For ($i = 0; $i < 12; $i++) {
For ($j = 0; $j < 12; $j++) {
If ($map[$i][$j] != -1) {
$distance[$i][$j] = $map[$i][$j];
$route[$i][$j] = $i;
}
Else {
$distance[$i][$j] = 999999;
$route[$i][$j] = -1;
}
}
$distance[$i][$i] = 0;
}
For ($k = 0; $k < 12; $k++) {
For ($i = 0; $i < 12; $i++) {
For ($j = 0; $j < 12; $j++) {
If ($distance[$i][$k] + $distance[$k][$j] < $distance[$i][$j]) {
$distance[$i][$j] = $distance[$i][$k] + $distance[$k][$j];
$route[$i][$j] = $k;
}
}
}
}
/* They are ready :) */
/* Read dest.txt and print shortest ways */
$fp = FOpen("dest.txt", "r");
$fread = FRead($fp, FileSize("dest.txt"));
FClose($fp);
$way = Array();
$w = -1;
$list = Explode(" ", $fread);
For ($i = 0; $i < Count($list)-1; $i++) {
$f = Ord($list[$i]) - 65;
$t = Ord($list[$i+1]) - 65;
If ($route[$f][$t] == $f) {
If ($way[$w] != Chr($f+65)) { $way[++$w] = Chr($f+65); }
If ($way[$w] != Chr($t+65)) { $way[++$w] = Chr($t+65); }
}
Else {
$f2 = $f;
While ($route[$f2][$t] != $f2) {
If ($way[$w] != Chr($f2+65)) { $way[++$w] = Chr($f2+65); }
$f2 = $route[$f2][$t];
}
If ($way[$w] != Chr($f2+65)) { $way[++$w] = Chr($f2+65); }
}
$total_dist += $distance[$f][$t];
}
If ($way[$w] != Chr($t+65)) { $way[++$w] = Chr($t+65); }
Echo Join(" ", $way) . " " . $total_dist;
?>
Back to results
|
|
|