search
HomeBackend DevelopmentPHP Tutorial php生成迷宫和迷宫寻址算法实例解决思路

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

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php //zairwolf z@cot8.com
header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL);

//n宫格迷宫
define('M', 39);//宫数
define("S", 20);//迷宫格大小
$_posArr = array(array(0, -1), array(1, 0), array(0, 1), array(-1, 0));//当前点寻址的四个xy方向 上右下左

//生成迷宫
$maze = array();
$mazeUnit = array(1, 1, 1, 1);//上右下左
for($x=0; $x<=M; $x++){
    for($y=0; $y<=M; $y++){
        $maze[$x][$y] = $mazeUnit;
    }
}
$maze2 = array();//破墙后的已访问格子
$mazeOrder = array();//破墙顺序
$x = $y = 0;//初始入口
while(count($maze)>0){
    $tmpArr = array();
    foreach($_posArr as $val){
        $nx = $x + $val[0];
        $ny = $y + $val[1];
        if(isset($maze[$nx][$ny])){//未破墙过的格子
            $tmpArr[] = array($nx, $ny);
        }
    }
    if($tmpArr){//有未破墙的格子,随机出一个,破墙
        list($nx, $ny) = $tmpArr[array_rand($tmpArr)];
        $maze2[$nx][$ny] = $maze[$nx][$ny];
        if(empty($maze2[$x][$y])) $maze2[$x][$y] = $maze[$x][$y];
        $pos = array($nx - $x, $ny - $y);
        foreach($_posArr as $key=>$val){//循环四个方向,找出需要破的墙
            if($pos == $val) {
                $maze2[$x][$y][$key] = 0;//原格子破墙
                $maze2[$nx][$ny][($key+2)%4] = 0;//新格子破墙
            }
        }
        //设置新的当前格后返回继续while循环
        $x = $nx;
        $y = $ny;
        $mazeOrder[] = array($x, $y);
        unset($maze[$x][$y]);//去掉已破墙的格子
        if(empty($maze[$x])) unset($maze[$x]);
    }else{//当前xy周围不存在未破墙的格子,返回上一个格子继续破墙
        array_pop($mazeOrder);
        if($mazeOrder) list($x, $y) = $mazeOrder[count($mazeOrder) - 1];
    }
}
//留出出口
$maze = $maze2;
$maze[0][0][3] = 0;
$maze[M][M][1] = 0;

//寻址
$pathArr = findPath($maze, 0, 0, false);
printMaze($maze, $pathArr);

echo "<img  src="maze.png" alt=" php生成迷宫和迷宫寻址算法实例解决思路 " > <a href="javascript:;" onclick="location.reload();">刷新</a>";

//打印迷宫和寻址结果by z@cot8.com
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);//包边
    ImageLine($im, 0, 0, 0, S, $bg);//右上边开口
    ImageLine($im, (M + 1) * S, M * S, (M + 1) * S, (M + 1) * S, $bg);//左下边开口
    foreach($maze as $x=>$xarr){//生成格子
        foreach($xarr as $y=>$unit){
            if($unit[0]) ImageLine($im, $x * S, $y * S, ($x + 1) * S, $y * S, $borderColor);//上有线
            if($unit[1]) ImageLine($im, ($x + 1) * S, $y * S, ($x + 1) * S, ($y + 1) * S, $borderColor);//右有线
            if($unit[2]) ImageLine($im, $x * S, ($y + 1) * S, ($x + 1) * S, ($y + 1) * S, $borderColor);//下有线
            if($unit[3]) ImageLine($im, $x * S, $y * S, $x * S, ($y + 1) * S, $borderColor);//左有线
            //if(in_array(array($x, $y), $pathArr)) ImageFilledEllipse($im, $x * S + S/2, $y * S + S/2, S, S, $pathColor);//寻址格
            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);//寻址格
        }
    }
    ImagePNG($im, 'maze.png');
    ImageDestroy($im);
}

//寻址函数 z@cot8.com
function findPath($maze, $x, $y, $fromxy){
    global $_posArr;
    if($x == M && $y == M){//到达出口
        Return array(array($x, $y));
    }
    foreach($_posArr as $key=>$val){
        if($maze[$x][$y][$key]) continue;//为1则不通
        $nx = $x + $val[0];
        $ny = $y + $val[1];
        if(!isset($maze[$nx][$ny]) || $fromxy == array($nx, $ny)) continue;//边界超出或为来源点
        if($pathArr = findPath($maze, $nx, $ny, array($x, $y))) {
            array_unshift($pathArr, array($x, $y));
            Return $pathArr;//能到达出口
        }
    }
    Return false;
}




------解决方案--------------------
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
使用C#中的Array.Sort函数对数组进行排序使用C#中的Array.Sort函数对数组进行排序Nov 18, 2023 am 10:37 AM

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

简单明了的PHP array_merge_recursive()函数使用方法简单明了的PHP array_merge_recursive()函数使用方法Jun 27, 2023 pm 01:48 PM

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

如何使用PHP中的array_combine函数将两个数组拼成关联数组如何使用PHP中的array_combine函数将两个数组拼成关联数组Jun 26, 2023 pm 01:41 PM

在PHP中,有许多强大的数组函数可以使数组的操作更加方便和快捷。当我们需要将两个数组拼成一个关联数组时,可以使用PHP的array_combine函数来实现这一操作。这个函数实际上是用来将一个数组的键作为另一个数组的值,合并成一个新的关联数组。接下来,我们将会讲解如何使用PHP中的array_combine函数将两个数组拼成关联数组。了解array_comb

PHP array_fill()函数用法详解PHP array_fill()函数用法详解Jun 27, 2023 am 08:42 AM

在PHP编程中,数组是一种非常重要的数据结构,能够轻松地处理大量数据。PHP中提供了许多数组相关的函数,array_fill()就是其中之一。本篇文章将详细介绍array_fill()函数的用法,以及在实际应用中的一些技巧。一、array_fill()函数概述array_fill()函数的作用是创建一个指定长度的、由相同的值组成的数组。具体来说,该函数的语法

Python中的Array模块怎么使用Python中的Array模块怎么使用May 01, 2023 am 09:13 AM

Python中的array模块是一个预定义的数组,因此其在内存中占用的空间比标准列表小得多,同时也可以执行快速的元素级别操作,例如添加、删除、索引和切片等操作。此外,数组中的所有元素都是同一种类型,因此可以使用数组提供的高效数值运算函数,例如计算平均值、最大值和最小值等。另外,array模块还支持将数组对象直接写入和读取到二进制文件中,这使得在处理大量数值数据时更加高效。因此,如果您需要处理大量同质数据,可以考虑使用Python的array模块来优化代码的执行效率。要使用array模块,首先需要

Java中的ArrayStoreException异常的常见原因是什么?Java中的ArrayStoreException异常的常见原因是什么?Jun 25, 2023 am 09:48 AM

在Java编程中,数组是一种重要的数据结构。数组可以在一个变量中存储多个值,更重要的是可以使用索引访问每个值。但是在使用数组时,可能会出现一些异常,其中之一是ArrayStoreException。本文将讨论ArrayStoreException异常的常见原因。1.类型不匹配数组在创建时必须指定元素类型。当我们试图将不兼容的数据类型存储到一个数组中时,就会抛

PHP array_change_key_case()函数使用方法介绍PHP array_change_key_case()函数使用方法介绍Jun 27, 2023 am 10:43 AM

在PHP编程中,数组是一个经常用到的数据类型。而关于数组的操作函数也是相当多的,其中包括了array_change_key_case()函数。该函数可以将数组中键名的大小写进行转换,从而方便我们进行数据的处理。本文就来介绍一下PHP中array_change_key_case()函数的使用方法。一、函数语法及参数array_change_ke

Java中的ArrayIndexOutOfBoundsException异常常见原因是什么?Java中的ArrayIndexOutOfBoundsException异常常见原因是什么?Jun 24, 2023 pm 10:39 PM

Java是一种非常强大的编程语言,广泛应用于各种开发领域。但是,在Java编程过程中,开发人员经常会遇到ArrayIndexOutOfBoundsException异常。那么,这个异常的常见原因是什么呢?ArrayIndexOutOfBoundsException是Java中常见的一个运行时异常。它表示在访问数据时,数组下标超出了数组的范围。常见的原因包括以

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft