php生成迷宫和迷宫寻址算法实例

较之前的终于有所改善。生成迷宫的算法和寻址算法其实是一样。只是一个用了遍历一个用了递归。参考了网上的Mike Gold的算法。

  1 <?php
  2 //zairwolf z@cot8.com
  3 header('Content-Type: text/html; charset=utf-8');
  4 error_reporting(E_ALL);
  5 
  6 //n宫格迷宫
  7 define('M', 39);//宫数
  8 define("S", 20);//迷宫格大小
  9 $_posArr = array(array(0, -1), array(1, 0), array(0, 1), array(-1, 0));//当前点寻址的四个xy方向 上右下左
 10 
 11 //生成迷宫
 12 $maze = array();
 13 $mazeUnit = array(1, 1, 1, 1);//上右下左
 14 for($x=0; $x<=M; $x++){
 15     for($y=0; $y<=M; $y++){
 16         $maze[$x][$y] = $mazeUnit;
 17     }
 18 }
 19 $maze2 = array();//破墙后的已访问格子
 20 $mazeOrder = array();//破墙顺序
 21 $x = $y = 0;//初始入口
 22 while(count($maze)>0){
 23     $tmpArr = array();
 24     foreach($_posArr as $val){
 25         $nx = $x + $val[0];
 26         $ny = $y + $val[1];
 27         if(isset($maze[$nx][$ny])){//未破墙过的格子
 28             $tmpArr[] = array($nx, $ny);
 29         }
 30     }
 31     if($tmpArr){//有未破墙的格子,随机出一个,破墙
 32         list($nx, $ny) = $tmpArr[array_rand($tmpArr)];
 33         $maze2[$nx][$ny] = $maze[$nx][$ny];
 34         if(empty($maze2[$x][$y])) $maze2[$x][$y] = $maze[$x][$y];
 35         $pos = array($nx - $x, $ny - $y);
 36         foreach($_posArr as $key=>$val){//循环四个方向,找出需要破的墙
 37             if($pos == $val) {
 38                 $maze2[$x][$y][$key] = 0;//原格子破墙
 39                 $maze2[$nx][$ny][($key+2)%4] = 0;//新格子破墙
 40             }
 41         }
 42         //设置新的当前格后返回继续while循环
 43         $x = $nx;
 44         $y = $ny;
 45         $mazeOrder[] = array($x, $y);
 46         unset($maze[$x][$y]);//去掉已破墙的格子
 47         if(empty($maze[$x])) unset($maze[$x]);
 48     }else{//当前xy周围不存在未破墙的格子,返回上一个格子继续破墙
 49         array_pop($mazeOrder);
 50         if($mazeOrder) list($x, $y) = $mazeOrder[count($mazeOrder) - 1];
 51     }
 52 }
 53 //留出出口
 54 $maze = $maze2;
 55 $maze[0][0][3] = 0;
 56 $maze[M][M][1] = 0;
 57 
 58 //寻址
 59 $pathArr = findPath($maze, 0, 0, false);
 60 printMaze($maze, $pathArr);
 61 
 62 echo "<img src='maze.png'> <a href='javascript:;' onclick='location.reload();'>刷新</a>";
 63 
 64 //打印迷宫和寻址结果by z@cot8.com
 65 function printMaze($maze, $pathArr){
 66     $im = ImageCreate((M + 1) * S + 1, (M + 1) * S + 1);
 67     $bg = ImageColorAllocate($im, 236, 233, 216);
 68     $pathColor=ImageColorAllocate($im, 255, 0, 0);
 69     $exitColor=ImageColorAllocate($im, 134, 255, 0);
 70     $borderColor = ImageColorAllocate($im, 0, 0, 0);
 71     ImageRectangle($im, 0, 0, (M + 1) * S, (M + 1) * S, $borderColor);//包边
 72     ImageLine($im, 0, 0, 0, S, $bg);//右上边开口
 73     ImageLine($im, (M + 1) * S, M * S, (M + 1) * S, (M + 1) * S, $bg);//左下边开口
 74     foreach($maze as $x=>$xarr){//生成格子
 75         foreach($xarr as $y=>$unit){
 76             if($unit[0]) ImageLine($im, $x * S, $y * S, ($x + 1) * S, $y * S, $borderColor);//上有线
 77             if($unit[1]) ImageLine($im, ($x + 1) * S, $y * S, ($x + 1) * S, ($y + 1) * S, $borderColor);//右有线
 78             if($unit[2]) ImageLine($im, $x * S, ($y + 1) * S, ($x + 1) * S, ($y + 1) * S, $borderColor);//下有线
 79             if($unit[3]) ImageLine($im, $x * S, $y * S, $x * S, ($y + 1) * S, $borderColor);//左有线
 80             //if(in_array(array($x, $y), $pathArr)) ImageFilledEllipse($im, $x * S + S/2, $y * S + S/2, S, S, $pathColor);//寻址格
 81             if(in_array(array($x, $y), $pathArr)) ImageString($im, 1, $x * S + S/5, $y * S + S/5, array_search(array($x, $y), $pathArr), $pathColor);//寻址格
 82         }
 83     }
 84     ImagePNG($im, 'maze.png');
 85     ImageDestroy($im);
 86 }
 87 
 88 //寻址函数 z@cot8.com
 89 function findPath($maze, $x, $y, $fromxy){
 90     global $_posArr;
 91     if($x == M && $y == M){//到达出口
 92         Return array(array($x, $y));
 93     }
 94     foreach($_posArr as $key=>$val){
 95         if($maze[$x][$y][$key]) continue;//为1则不通
 96         $nx = $x + $val[0];
 97         $ny = $y + $val[1];
 98         if(!isset($maze[$nx][$ny]) || $fromxy == array($nx, $ny)) continue;//边界超出或为来源点
 99         if($pathArr = findPath($maze, $nx, $ny, array($x, $y))) {
100             array_unshift($pathArr, array($x, $y));
101             Return $pathArr;//能到达出口
102         }
103     }
104     Return false;
105 }
原文地址:https://www.cnblogs.com/zqifa/p/php-maze-1.html