php 人造迷宫 可刷新运行 刷新

生成小蝌蚪找妈妈路线。。。

代码:

<?php

$n=29;
$positions=[];
for ($i=0;$i<=$n;$i++){
    for ($j=0;$j<=$n;$j++){
        $positions[] =[$j,$i] ;
    }
}

//开始的点
$startDot=[0,0];
//已经放置的点
$placedDot=[];
//不可放置的点
$notPlacedDot=notPlacedDot($positions,$n);
$placedDot[]=$startDot;



//所有穿过的点
$lineDot=line($positions,$startDot,$placedDot,$notPlacedDot,$n);

echo getHtml($positions,$lineDot,$n);


function line($positions,$startDot,$placedDot,$notPlacedDot,$n){
    
    $dotWrapper=GetDotWrappers($startDot);
    $key=rand(0,3);
    $nextDot =  $dotWrapper[$key];

    $i=0;
    $notPlacedDotAll =array_merge($notPlacedDot,[]);
    while (in_array($nextDot,$placedDot)||in_array($nextDot,$notPlacedDotAll)){

        $i++;
       if(badDot($nextDot,$placedDot,$notPlacedDot,$n)){//是否是坏点

           $notPlacedDotAll[]=$nextDot;

          $startDot=$placedDot[count($placedDot)-1];
           $dotWrapper=GetDotWrappers($startDot);
           $key=rand(0,3);
           $nextDot =  $dotWrapper[$key];

          if(badDot($startDot,$placedDot,$notPlacedDotAll,$n)){//本身是否是坏点
              $notPlacedDotAll[]= array_pop($placedDot);

              $startDot=$placedDot[count($placedDot)-1];
              $dotWrapper=GetDotWrappers($startDot);
              $key=rand(0,3);
              $nextDot =  $dotWrapper[$key];

          }

        }else{
            $key=rand(0,3);
            $nextDot =  $dotWrapper[$key];

        }
        if($i>4&&count($placedDot)>1){//循环大于3次 还没找到去除该点
            $i=0;
            $notPlacedDot[]= array_pop($placedDot);
            $startDot=$placedDot[count($placedDot)-1];
            $dotWrapper=GetDotWrappers($startDot);
            $key=rand(0,3);
            $nextDot =  $dotWrapper[$key];


        }

    }


    $placedDot[]=$nextDot;

   if($nextDot[0]>=$n&&$nextDot[1]>0&&$nextDot[1]<=$n){
       return $placedDot;
   }
   return line($positions,$nextDot,$placedDot,$notPlacedDot,$n);
}

function getHtml($positions,$lineDot,$n){
    $str="<table border='1'>";

    foreach (array_chunk($positions,$n+1) as $row){
        $str.="<tr>";
        foreach ($row as $tr){
            if(in_array($tr,$lineDot)){
                $str.="<td style='color: white;background-color: red'> {$tr[0]},{$tr[1]}</td>";
            }else{
                $str.="<td>{$tr[0]},{$tr[1]}</td>";
            }
        }
        $str.="<tr>";

    };
    $str.="</table>";
    return $str;
}

function notPlacedDot($positions,$n)
{
    
    $notPlacedDot=[];
    foreach ($positions as $position){
        $dotWrapper=GetDotWrappers($position);

        foreach ($dotWrapper as $dot){
            if($dot[0]<0||$dot[1]<0||$dot[0]>$n||$dot[1]>$n){
                $notPlacedDot[]=$dot;
            }
        }
    }
    return $notPlacedDot;
}

/**
 * 是否是坏点
 * @param $dotWrapper
 * @param $placedDot
 * @param $notPlacedDot
 * @return bool
 */
function badDot($position,$placedDot,$notPlacedDot,$n){
    
    $dotWrapper=GetDotWrappers($position);
    $i=0;
    foreach ($dotWrapper as $dot){
        if($dot[0]<0||$dot[1]<0||$dot[1]>$n||in_array($dot,$placedDot)||in_array($dot,$notPlacedDot)){
            $i++;
        }
    }
    if($i==4){
        return true;
    }
    return false;

}


function GetDotWrappers($placementEd){
    $top = calculateXY($placementEd[0],$placementEd[1],'top');
    $bottom = calculateXY($placementEd[0],$placementEd[1],'bottom');
    $left = calculateXY($placementEd[0],$placementEd[1],'left');
    $right = calculateXY($placementEd[0],$placementEd[1],'right');
    return [$top,$bottom,$left,$right];
}

function calculateXY($x,$y,$default='top')
{
    switch ($default){
        case 'top':
            $y=$y+1;
            break;
        case 'bottom':
            $y=$y-1;
            break;
        case 'left':
            $x=$x-1;
            break;
        case 'right':
            $x=($x+1);
            break;
        case 'left_top':
            $x=($x-1);
            $y=($y+1);
            break;
        case 'right_top':
            $x=($x+1);
            $y=($y+1);
            break;
        case 'right_bottom':
            $x=($x+1);
            $y=($y-1);
            break;
        case 'left_bottom':
            $x=($x-1);
            $y=($y-1);
            break;
    }

    return [$x,$y];
}

输出:

0,0 1,0 2,0 3,0 4,0 5,06,07,08,09,010,011,012,013,014,015,016,017,018,019,020,021,022,023,024,025,026,027,028,029,0
0,11,12,13,1 4,1 5,16,17,18,19,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,1
0,2 1,2 2,2 3,2 4,25,26,27,28,29,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,2
0,3 1,32,33,34,35,36,37,38,39,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,3
0,4 1,42,43,44,45,46,47,48,49,410,411,412,413,414,415,416,417,418,419,420,421,422,423,424,425,426,427,428,429,4
0,51,52,53,54,55,56,57,58,59,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,5
0,61,62,63,64,65,66,67,68,69,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,6
0,7 1,72,73,74,75,76,77,78,79,710,711,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,7
0,8 1,8 2,8 3,8 4,85,86,87,88,89,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,8
0,91,9 2,9 3,9 4,95,96,97,98,99,9 10,9 11,9 12,9 13,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,9
0,101,102,10 3,10 4,105,106,107,108,10 9,10 10,1011,10 12,10 13,1014,1015,10 16,10 17,1018,1019,1020,1021,1022,1023,1024,1025,1026,1027,1028,1029,10
0,111,11 2,11 3,114,115,116,11 7,11 8,11 9,1110,1111,11 12,11 13,11 14,11 15,11 16,11 17,1118,1119,1120,1121,1122,1123,1124,1125,1126,1127,1128,1129,11
0,121,12 2,123,12 4,12 5,12 6,12 7,12 8,12 9,1210,1211,1212,1213,1214,12 15,12 16,12 17,12 18,1219,1220,1221,1222,1223,1224,1225,1226,1227,1228,1229,12
0,131,13 2,133,13 4,135,13 6,13 7,138,139,1310,1311,1312,1313,1314,1315,1316,1317,13 18,13 19,13 20,1321,1322,1323,1324,1325,1326,1327,1328,1329,13
0,141,14 2,143,14 4,14 5,146,147,148,149,1410,1411,1412,14 13,14 14,14 15,14 16,14 17,14 18,14 19,14 20,1421,1422,1423,1424,1425,1426,1427,1428,1429,14
0,151,15 2,153,15 4,15 5,156,157,158,159,1510,1511,1512,15 13,1514,1515,15 16,15 17,1518,1519,1520,1521,1522,1523,1524,1525,1526,1527,1528,1529,15
0,16 1,16 2,163,16 4,165,166,167,168,169,1610,1611,1612,16 13,1614,16 15,16 16,16 17,16 18,16 19,1620,1621,1622,1623,1624,1625,1626,1627,1628,1629,16
0,17 1,17 2,17 3,17 4,175,17 6,17 7,17 8,17 9,17 10,1711,1712,17 13,1714,17 15,17 16,17 17,17 18,17 19,1720,1721,1722,1723,1724,1725,1726,1727,1728,1729,17
0,18 1,182,183,184,185,18 6,187,18 8,18 9,18 10,18 11,18 12,18 13,18 14,18 15,18 16,1817,18 18,18 19,1820,1821,1822,1823,1824,1825,1826,1827,1828,1829,18
0,191,192,193,194,195,19 6,19 7,19 8,19 9,19 10,19 11,1912,1913,19 14,19 15,19 16,1917,19 18,19 19,1920,1921,1922,1923,1924,1925,1926,1927,1928,1929,19
0,201,202,203,204,205,206,207,208,20 9,20 10,20 11,20 12,20 13,20 14,20 15,20 16,20 17,20 18,20 19,2020,2021,2022,2023,2024,2025,2026,2027,2028,2029,20
0,211,212,213,214,215,216,217,218,219,2110,2111,21 12,21 13,2114,21 15,2116,21 17,21 18,2119,2120,2121,2122,2123,2124,2125,2126,2127,2128,2129,21
0,221,222,223,224,225,226,227,228,229,2210,2211,22 12,22 13,22 14,22 15,2216,22 17,22 18,2219,2220,2221,2222,2223,2224,2225,2226,2227,2228,2229,22
0,231,232,233,234,235,236,237,238,23 9,23 10,23 11,23 12,23 13,23 14,23 15,23 16,23 17,23 18,23 19,2320,2321,2322,2323,2324,2325,2326,2327,2328,2329,23
0,241,242,243,244,245,246,247,24 8,24 9,2410,24 11,24 12,24 13,24 14,2415,24 16,24 17,24 18,24 19,2420,2421,24 22,24 23,24 24,24 25,2426,2427,2428,2429,24
0,251,252,253,254,255,256,257,25 8,259,2510,2511,2512,2513,25 14,2515,25 16,25 17,25 18,25 19,25 20,25 21,25 22,25 23,25 24,25 25,25 26,2527,2528,2529,25
0,261,262,263,264,265,266,267,26 8,26 9,2610,2611,26 12,26 13,26 14,26 15,26 16,26 17,26 18,26 19,26 20,26 21,26 22,26 23,26 24,26 25,26 26,26 27,26 28,26 29,26
0,271,272,273,274,275,276,277,278,27 9,2710,27 11,27 12,27 13,2714,2715,2716,2717,27 18,27 19,27 20,27 21,27 22,27 23,27 24,27 25,2726,2727,2728,2729,27
0,281,282,283,284,285,286,287,288,28 9,28 10,28 11,28 12,28 13,2814,2815,2816,2817,2818,28 19,28 20,28 21,28 22,28 23,28 24,2825,2826,2827,2828,2829,28
0,291,292,293,294,295,296,297,298,299,29 10,29 11,29 12,29 13,29 14,29 15,29 16,29 17,29 18,29 19,2920,29 21,29 22,29 23,29 24,2925,2926,2927,2928,2929,29