当前位置:网站首页>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);

边栏推荐
- MySQL advanced - Architecture
- Elastic 8.0: opening a new era of speed, scale, relevance and simplicity
- 剑指 Offer 16. 数值的整数次方
- Dependencies tool to view exe and DLL dependencies
- 医疗行业企业供应链系统解决方案:实现医疗数智化供应链协同可视
- Unity实战之一个脚本实现雷达图
- Three methods of modifying time zone in MySQL
- MySQL找不到mysql.sock文件的临时解
- Conception d'un centre commercial en ligne basé sur SSH
- 深度学习编译器的理解
猜你喜欢

What if the apple watch fails to power on? Apple watch can not boot solution!

Talk about the SQL server version of DTM sub transaction barrier function

挑选智能音箱时,首选“智能”还是“音质”?这篇文章给你答案

iCloud照片无法上传或同步怎么办?

漏洞复现----37、Apache Unomi 远程代码执行漏洞 (CVE-2020-13942)

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

Tsinghua only ranks third? 2022 release of AI major ranking of Chinese Universities of soft science

Redis (III) - transaction

Switching routing (VLAN) experiment

Deep understanding of JVM (III) - memory structure (III)
随机推荐
The company was jailed for nonstandard bug during the test ~ [cartoon version]
Unity开发bug记录100例子(第1例)——打包后shader失效或者bug
Sword finger offer 16 Integer power of numeric value
麻烦问下 Flink支持同步数据到 sqlserver么
如何做好软件系统的需求调研,七种武器让你轻松搞定
剑指 Offer 16. 数值的整数次方
Advanced customization of uni app [day13]
TCP session hijacking based on hunt1.5
MySQL找不到mysql.sock文件的临时解
Flink系列:checkpoint调优
Tsinghua only ranks third? 2022 release of AI major ranking of Chinese Universities of soft science
火山引擎入选国内首个《边缘计算产业全景图》
VScode 状态条 StatusBar
Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team
电子元器件行业在线采购系统精准匹配采购需求,撬动电子产业数字化发展
Rhai - Rust 的嵌入式脚本引擎
The secondary menu of the magic article system v5.4.0 supports the optimization of form display
漏洞复现----38、ThinkPHP5 5.0.23 远程代码执行漏洞
Communication network electronic billing system based on SSH
LeetCode动态规划经典题(一)