当前位置:网站首页>php利用队列解决迷宫问题
php利用队列解决迷宫问题
2022-06-30 17:16:00 【liuliang514218119】
<?php
# php利用队列解决迷宫问题
# 思路:从一个节点开始,寻找所有下面能继续走的点。继续寻找,直到找到出口。 -- 广度优先搜索
# 方法:创建一个空队列,将起点位置进队。在队列不为空时循环:出队一次。如果当前位置为出口,则结束算法;否则找出当前方块的4个相邻方块中可走的方块,全部进队。
# 迷宫
$maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
];
# 获取节点的四个方向
function get_direction($x, $y)
{
return [
[$x - 1, $y], #上
[$x, $y + 1], #右
[$x + 1, $y], #下
[$x, $y - 1], #左
];
}
function maze_path_queue($x1, $y1, $x2, $y2, $maze)
{
$queue = []; #创建队列
$path = []; # 记录出队之后的节点
array_push($queue, [$x1, $y1, -1]);
$maze[$x1][$y1] = 2; #2表示已经走过的点
while (count($queue) > 0) {
$cur_node = array_shift($queue); # 出队列获取当前节点
array_push($path, $cur_node);
if ($cur_node[0] == $x2 && $cur_node[1] == $y2) {
# 终点
$real_path = [];
$end_path = end($path);
array_push($real_path, [$end_path[0], $end_path[1]]);#将终点坐标push到real_path中
$i = $end_path[2];
while ($i >= 0) {
$node = $path[$i]; #node是一个元祖(x坐标,y坐标,该点的leader)
array_push($real_path, [$node[0], $node[1]]); #只要坐标
$i = $node[2];
}
$real_path = array_reverse($real_path); #反转后顺序才为从起点到终点
foreach ($real_path as $v) {
echo implode(',', $v) . "\n";
}
# print_r($real_path);
return True;
}
$direction = get_direction($cur_node[0], $cur_node[1]); #获取当前坐标的四个方向
$i = 0;
while ($i < 4) {
$next_node = $direction[$i]; #下一个节点,循环四个方向是否可以走
if ($maze[$next_node[0]][$next_node[1]] == 0) {
#如果下一个节点可以走 就将坐标存入队列
array_push($queue, [$next_node[0], $next_node[1], (count($path) - 1)]); #(x坐标,y坐标,path最后一个元素的索引:记录哪个节点带它来的)进入队列
$maze[$next_node[0]][$next_node[1]] = 2; #标记为已经走过
}
$i++;
}
}
print("无路可走");
return False;
}
maze_path_queue(1, 1, 8, 8, $maze);

边栏推荐
- Techo Youth2022学年高校公开课:直播连麦的背后,探索音视频技术如何应用
- Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
- Vulnerability recurrence ----- 35. Uwsgi PHP directory traversal vulnerability (cve-2018-7490)
- 【二叉树】前序遍历构造二叉搜索树
- Babbitt | yuanuniverse daily must read: minors ask for a refund after a reward. The virtual anchor says he is a big wrongdoer. How do you think of this regulatory loophole
- Dropout: immediate deactivation
- Deep understanding of JVM (IV) - garbage collection (I)
- ASP. Net password encryption and password login
- Do you write API documents or code first?
- 力扣解法汇总1175-质数排列
猜你喜欢

Vulnerability recurrence ----37. Apache unomi Remote Code Execution Vulnerability (cve-2020-13942)

LeetCode动态规划经典题(一)

Alexnet of CNN classic network (Theory)

Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence

How to solve the lock-in read-only alarm of AutoCAD Chinese language?

ASP. Net password encryption and password login

Shortcut keys for the rainbow brackets plug-in

基于SSH的客户关系CRM管理系统

ABAP-发布Restful服务

基於SSH的網上商城設計
随机推荐
基于SSH的网上商城设计
Deep understanding of JVM (IV) - garbage collection (I)
What should I pay attention to when playing futures? Where is safe to open an account? It's my first contact
Multipass中文文档-设置图形界面
程序员女友给我做了一个疲劳驾驶检测
NFT挖矿游GameFi链游系统开发搭建
Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
助力极致体验,火山引擎边缘计算最佳实践
Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)
Optimization of interface display for general kernel upgrade of mobo video management system v3.5.0
C language structure
ASP. Net password encryption and password login
现在玩期货需要注意什么,在哪里开户比较安全,我第一次接触
The new Post-00 Software Test Engineer in 2022 is a champion
VS code 树视图 treeView
「经验」我对用户增长的理解『新用户篇』
基于SSH的通讯网络电子计费系统
Ardunio esp32 obtains real-time temperature and humidity in mqtt protocol (DH11)
基于SSM的新闻管理系统
力扣解法汇总1175-质数排列