Solution created in $time seconds\n
";
//Return an array of affected tiles.
function getAffected($route) {
//Determin starting location.
$start = explode(",", $route['start']);
$x=$start[0];
$y=$start[1];
$tog = false;
//Run through the path
$pathary = str_split($route['path']);
//echo "path:".$route['path'];
foreach ($pathary as $position => $char) {
switch($char) {
case 'u': //tp1
case 'n': //tp2
case 'h': //tp3
case 'j': //tp4
case 'l': //tp5
if ($tog == true) {
$tog = false;
continue;
}
$tog = true;
//Get teleport coords
$tmp = split($char, $route['path']);
$loc = split(',', $tmp[1]);
$x = $loc[0];
$y = $loc[1];
//echo "....$x,$y....";
}
if ($tog == true)
continue;
switch($char) {
case 1: $y--; break; //up
case 2: $x++; break; //right
case 3: $y++; break; //down
case 4: $x--; break; //left
default: continue 2;
}
//add the positions
$affected["$y,$x"]++;
}
//arsort($affected);
return $affected;
}
// http://awesomescreenshot.com/0d7nyx9b2
function executeTryMaze($mapMatrix, $intensity = 1) {
$mapdata['height'] = $mapMatrix[0][0];
$mapdata['width'] = $mapMatrix[0][1];
$mapdata['points'] = $mapMatrix[0][2];
$mapdata['rocks'] = $mapMatrix[0][3];
$mapdata['walls'] = $mapMatrix[0][4];
//Override hack.
//$mapdata['walls'] = 12;
$mapdata['teleports'] = $mapMatrix[0][5];
//PROCSES BEGIN
$routes = 0;
$wallcount = 0;
$iterations = 0;
$lastRefresh = 0;
do {
$iterations++;
$done = false;
$path = routePath($mapMatrix);
$routes++;
$affected = getAffected($path);
$curcount = 1000;
$bestmoves = 0;
$bestcount = 0;
foreach ($affected as $position => $count) {
//Find location
$loc = explode(",", $position);
$x=$loc[0];
$y=$loc[1];
//Is it open?
if ($mapMatrix[$x][$y] != "o") {
continue;
}
//Set a wall and calculate the new path
$mapMatrix[$x][$y] = 'R';
$curpath = routePath($mapMatrix);
$routes++;
$trymoves = $curpath['moves'];
if ($curpath['blocked'] == true) {
//Sometimes it's better to remove a different wall;
//Put a wall here.
//if ($iterations % 8 == 4) {
if (1 == 12) {
$arrayWalls[$x][$y] = true;
//determin wall values.
$walllist = wallvalues($mapMatrix, $arrayWalls);
$routes = $routes + $wallcount;
//Put the best wall to remove on top; - A mono directional bubble sort would be better here.
$walllist["$x,$y"]--;
asort($walllist);
//Get location of wall to remove.
$nwallposition = key($walllist);
echo "*$nwallposition vs $x,$y*\n";
$nloc = explode(",", $nwallposition);
$bx=$nloc[0];
$by=$nloc[1];
$mapMatrix[$bx][$by] = 'R';
$mapMatrix[$x][$y] = 'o';
// $tmp = routePath($mapMatrix);
// if ($tmp['blocked']) {
// $mapMatrix[$bx][$by] = 'R';
// $mapMatrix[$x][$y] = 'o';
// echo "Unblocked";
// continue;
// }
unset($arrayWalls[$bx][$by]);
continue;
}
$mapMatrix[$x][$y] = 'o';
continue;
}
//The last choice, or first choice?
//Can't decide, let's do both.
if ($trymoves > $bestmoves OR ($trymoves >= $bestmoves AND $iterations % 4 >= 2)) {
if ($bestcount <= $count) {
$bestcount = $count;
$bestmoves = $trymoves;
$bestx = $x;
$besty = $y;
$mapMatrix[$x][$y] = 'o';
}
}
$mapMatrix[$x][$y] = 'o';
}
//Place the wall in the best spot we found;
$arrayWalls[$bestx][$besty] = true;
echo $mapMatrix[$bestx][$besty];
$mapMatrix[$bestx][$besty] = 'R';
$wallcount++;
echo "B($bestx,$besty)\n:";
//Refresh walls (Remove obsolete walls)
//Do a smidgens less than every other one.
if ( ($iterations % 2 == 1 AND $iterations % 20 <> 1)
AND ($wallcount > ($mapdata['walls'] * .6)) )
{
echo "RW";
echo "MU";
echo memory_get_usage(true);
echo "MGPU:";
//echo memory_get_peak_usage();
//echo memory_get_peak_usage();
echo "::";
$walllist = wallvalues($mapMatrix, $arrayWalls);
$routes = $routes + $wallcount;
//Remove low valued wall;
$lowvaluewall = true;
$nullwalls = 0;
foreach ($walllist as $nwallposition => $wallvalue) {
if ($wallvalue == 0 OR ($lowvaluewall == true AND $wallvalue == 1)) {
$nloc = explode(",", $nwallposition);
$x=$nloc[0];
$y=$nloc[1];
if ($x == $bestx AND $y == $besty) {
//echo "LVW USED";
//$lowvaluewall = false;
continue;
}
$mapMatrix[$x][$y] = 'o';
unset($arrayWalls[$x][$y]);
$wallcount--;
echo "RM($x, $y)";
$nullwalls++;
if ($nullwalls >= 2) {
break;
}
if ($lowvaluewall == true AND $wallvalue == 1) {
$lowvaluewall = false;
}
}
}
}
//Refresh walls if we hit the wall count limit, once.
if($wallcount >= $mapdata['walls'] && $lastRefresh == 0)
{
$lastRefresh = 1;
$walllist = wallvalue($mapMatrix, "R");
$routes++;
foreach ($walllist as $nwallposition => $wallvalue) {
if ($wallvalue < 1) {
$nloc = explode(",", $nwallposition);
$i=$nloc[0];
$j=$nloc[1];
$mapMatrix[$i][$j] = 'o';
unset($arrayWalls[$i][$j]);
$wallcount--;
echo "LRM($i, $j)";
}
}
echo "End Refresh walls to:[$wallcount]";
}
//If all walls are placed even after refresh, we're done
if ($wallcount >= $mapdata['walls']) {
echo "END;($wallcount) During $iterations iterations 'TryMaze' routed the path $routes times. Placing $wallcount walls";
break;
}
} while ($done == false);
//echo "print";
//print_r(wallvalue($mapMatrix, "R"));
//echo "endprint";
return $mapMatrix;
}
//Returns the essential 'move value' of individual walls in an array.
function wallValues($mapMatrix, $arrayWalls) {
$curpath = routePath($mapMatrix);
$curmoves = $curpath['moves'];
if ($curpath['blocked'])
$curmoves = 0;
//Prepare to return an array.
$r = Array();
foreach ($arrayWalls as $x => $a) {
foreach ($a as $y => $v) {
//echo "{O".$mapMatrix[$x][$y]."O}";
if ($mapMatrix[$x][$y] <> 'R') {
continue;
}
//Imagine the maze without the wall.
$mapMatrix[$x][$y] = 'o';
$newpath = routePath($mapMatrix);
$newmoves = $newpath['moves'];
//Great; so the value of that wall is:
$r["$x,$y"] = $curmoves - $newmoves;
//Okay, you can have your wall back.
$mapMatrix[$x][$y] = 'R';
}
}
return $r;
}
//Returns the essential 'move value' of a wall.
function wallvalue($mapMatrix, $wall) {
$curpath = routePath($mapMatrix);
$curmoves = $curpath['moves'];
for( $i = 1; $i <= $mapMatrix[0][1]; $i++) { //Number of Rows
for( $j = 0; $j < $mapMatrix[0][0]; $j++) { //Number of Columns
if ($mapMatrix[$i][$j] == $wall) {
$mapMatrix[$i][$j] = 'o';
$newpath = routePath($mapMatrix);
$mapMatrix[$i][$j] = 'r';
$newmoves = $newpath['moves'];
$r["$i,$j"] = $curmoves - $newmoves;
}
}
}
return $r;
}
echo "
Path: ";
echo $bla['moves'];
echo "
";
$mapdisplay = DisplayMap($map);
$code = GenerateMapCode($map);
echo "
$mapdisplay";
echo "
$code";
print_r ($map);
echo "
";
echo "
";
echo "
";
echo weight(1,2,3,4,5,6);
echo "
";
echo weight(1,2,3,4,5,6);
echo "
";
echo weight(1,2,3,4,5,6);
echo "
";
echo weight(1,2,3,4,5,6);
echo "
";
echo weight(1,2,3,4,5,6);
echo "
";
echo weight(1,2,3,4,5,6);
?>