PHP generates maze and maze addressing algorithm examples to solve the idea

Source: Internet
Author: User
Tags image line
PHP generates maze and maze addressing algorithm examples
has finally improved compared to the previous. The algorithm for generating the maze and the addressing algorithm are actually the same. Just one with the traversal one used recursion. Refer to the online Mike Gold algorithm.

PHP Code
  
  0) {$tmpArr = array ();        foreach ($_posarr as $val) {$nx = $x + $val [0];        $ny = $y + $val [1];        if (Isset ($maze [$NX] [$ny])) {//Not broken wall lattice $tmpArr [] = Array ($nx, $ny);        }} if ($TMPARR) {//There are no broken walls of the lattice, randomly out of one, broken wall list ($nx, $ny) = $TMPARR [Array_rand ($TMPARR)];        $maze 2[$nx] [$ny] = $maze [$NX] [$ny];        if (Empty ($maze 2[$x] [$y]) $maze 2[$x] [$y] = $maze [$x] [$y];        $pos = Array ($nx-$x, $ny-$y); foreach ($_posarr as $key + $val) {//Loop Four directions to find out which wall to break if ($pos = = $val) {$maze 2[$x] [$y] [$key] = 0;        Original lattice broken wall $maze 2[$nx] [$ny] [($key +2)%4] = 0;//new lattice Broken Wall}}//Set new current grid and return to continue while loop        $x = $nx;        $y = $ny;        $mazeOrder [] = Array ($x, $y);    Unset ($maze [$x] [$y])//Remove the lattice if (empty ($maze [$x])) of the Broken wall unset ($maze [$x]);        }else{//There are no broken walls around the current XY lattice, return to the previous lattice continue to break the wall Array_pop ($mazeOrder);    if ($mazeOrder) list ($x, $y) = $mazeOrder [Count ($mazeOrder)-1]; }}//Leave the Exit $maZe = $maze 2; $maze [0][0][3] = 0; $maze [m][m][1] = 0;//addressing $patharr = Findpath ($maze, 0, 0, false);p Rintmaze ($maze, $PATHARR); E Cho "Refresh";//print maze and addressing results by [email protected]function Printmaze ($maze, $pathArr) {$im = Imagecreate ((M + 1) * S + 1, (    M + 1) * S + 1);    $BG = Imagecolorallocate ($im, 236, 233, 216);    $pathColor =imagecolorallocate ($im, 255, 0, 0);    $exitColor =imagecolorallocate ($im, 134, 255, 0);    $borderColor = imagecolorallocate ($im, 0, 0, 0); Imagerectangle ($im, 0, 0, (M + 1) * s, (M + 1) * s, $borderColor);//Hemming Imageline ($im, 0, 0, 0, S, $BG);//Right Top opening Image Line ($im, (M + 1) * s, M * s, (M + 1) * s, (M + 1) * s, $BG);//Left Bottom open foreach ($maze as $x + $xarr) {//Generate lattice Forea CH ($xarr as $y = $unit) {if ($unit [0]) Imageline ($im, $x * s, $y * s, ($x + 1) * s, $y * s, $borderColor);//            Line if ($unit [1]) Imageline ($im, ($x + 1) * s, $y * s, ($x + 1) * s, ($y + 1) * s, $borderColor);//Right Wired if ($unit [2]) Imageline ($im, $x * S, ($y + 1)* S, ($x + 1) * s, ($y + 1) * s, $borderColor);//Under Wired if ($unit [3]) Imageline ($im, $x * s, $y * s, $x * s, ($y + 1) * s, $borderColor),//left Wired//if (In_array (Array ($x, $y), $PATHARR)) Imagefilledellipse ($im, $x * S + S/2, $y *  S + S/2, S, S, $pathColor);//Address Lattice if (In_array ($x, $y), $PATHARR) imagestring ($im, 1, $x * s + S/5, $y * s    + S/5, Array_search (Array ($x, $y), $PATHARR), $pathColor);//Addressing Lattice}} imagepng ($im, ' maze.png '); Imagedestroy ($im);}    addressing functions [Email protected]function findpath ($maze, $x, $y, $fromxy) {global $_posarr;    if ($x = = m && $y = = m) {//Arrival exit Return Array (Array ($x, $y));        The foreach ($_posarr as $key = $val) {if ($maze [$x] [$y] [$key]) continue;//1 is not $nx = $x + $val [0];        $ny = $y + $val [1]; if (!isset ($maze [$NX] [$ny]) | | $fromxy = = Array ($nx, $ny)) continue;//boundary exceeds or is the source point if ($pathArr = Findpath ($maze, $nx, $ NY, Array ($x, $y))) {Array_unshift ($PATHARR, ARRay ($x, $y)); return $PATHARR;//Can Reach Exit}} return false;


------Solution--------------------
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.