当前位置:网站首页>PHP uses queues to solve maze problems
PHP uses queues to solve maze problems
2022-06-30 18:41:00 【liuliang514218119】
<?php
# php Using queues to solve maze problems
# Ideas : Start with a node , Look for all the points below that you can continue to walk . Keep looking for , Until we find the exit . -- Breadth first search
# Method : Create an empty queue , Enter the starting position into the team . Loop when queue is not empty : Once out of the team . If the current position is exit , Then end the algorithm ; Otherwise, find the current square 4 A walkable square of adjacent squares , All in the team .
# maze
$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],
];
# Get the four directions of the node
function get_direction($x, $y)
{
return [
[$x - 1, $y], # On
[$x, $y + 1], # Right
[$x + 1, $y], # Next
[$x, $y - 1], # Left
];
}
function maze_path_queue($x1, $y1, $x2, $y2, $maze)
{
$queue = []; # Create a queue
$path = []; # Record the nodes after the queue
array_push($queue, [$x1, $y1, -1]);
$maze[$x1][$y1] = 2; #2 Indicates the point that has been passed
while (count($queue) > 0) {
$cur_node = array_shift($queue); # Get the current node from the queue
array_push($path, $cur_node);
if ($cur_node[0] == $x2 && $cur_node[1] == $y2) {
# End
$real_path = [];
$end_path = end($path);
array_push($real_path, [$end_path[0], $end_path[1]]);# Set the end coordinates push To real_path in
$i = $end_path[2];
while ($i >= 0) {
$node = $path[$i]; #node It's a Yuanzu (x coordinate ,y coordinate , At this point leader)
array_push($real_path, [$node[0], $node[1]]); # As long as the coordinates
$i = $node[2];
}
$real_path = array_reverse($real_path); # The sequence is from start point to end point only after reversing
foreach ($real_path as $v) {
echo implode(',', $v) . "\n";
}
# print_r($real_path);
return True;
}
$direction = get_direction($cur_node[0], $cur_node[1]); # Get the four directions of the current coordinate
$i = 0;
while ($i < 4) {
$next_node = $direction[$i]; # Next node , Whether the four directions of the cycle can go
if ($maze[$next_node[0]][$next_node[1]] == 0) {
# If the next node can go Just put the coordinates in the queue
array_push($queue, [$next_node[0], $next_node[1], (count($path) - 1)]); #(x coordinate ,y coordinate ,path The index of the last element : Record which node brought it ) Enter the queue
$maze[$next_node[0]][$next_node[1]] = 2; # Mark as having passed
}
$i++;
}
}
print(" No way out ");
return False;
}
maze_path_queue(1, 1, 8, 8, $maze);

边栏推荐
- Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
- Rust 文件系统处理之文件读写 - Rust 实践指南
- How to solve the lock-in read-only alarm of AutoCAD Chinese language?
- Solve the problem of unable to connect to command metric stream and related problems in the hystrix dashboard
- The new Post-00 Software Test Engineer in 2022 is a champion
- [cloud resident co creation] Huawei iconnect enables IOT terminals to connect at one touch
- Redis (VIII) - enterprise level solution (I)
- Sword finger offer 16 Integer power of numeric value
- C language structure
- Tsinghua only ranks third? 2022 release of AI major ranking of Chinese Universities of soft science
猜你喜欢

又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队

Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)

Deep understanding of JVM (V) - garbage collection (II)

漏洞复现----38、ThinkPHP5 5.0.23 远程代码执行漏洞

OneFlow源码解析:算子签名的自动推断

ASP. Net generate verification code

先写API文档还是先写代码?

火山引擎入选国内首个《边缘计算产业全景图》

AI首席架构师10-AICA-蓝翔 《飞桨框架设计与核心技术》

Alexnet of CNN classic network (Theory)
随机推荐
Customer relationship CRM management system based on SSH
Summary of methods for offline installation of chrome extensions in China
What if icloud photos cannot be uploaded or synchronized?
MRO工业品采购管理系统:赋能MRO企业采购各节点,构建数字化采购新体系
What if the apple watch fails to power on? Apple watch can not boot solution!
Compilation problems and solutions of teamtalk winclient
医疗行业企业供应链系统解决方案:实现医疗数智化供应链协同可视
又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队
使用excel快速生成sql语句
分布式事务
剑指 Offer 16. 数值的整数次方
C语言结构体
Type ~ storage ~ variable in C #
Force deduction solution summary 1175- prime number arrangement
先写API文档还是先写代码?
News management system based on SSM
Deep understanding of JVM (IV) - garbage collection (I)
Apple Watch无法开机怎么办?苹果手表不能开机解决方法!
Grep output with multiple colors- Grep output with multiple Colors?
Redis (IV) - delete policy