Javascript 我无法从迷宫中找到最短路径(广度优先搜索)
我有一个迷宫,以矩阵的形式表示。有一个起点和一个终点。我只能在包含零的单元格中移动。我编写了一个查找所有可用路径的函数。但是函数没有找到最短路径。请帮忙完成这个功能Javascript 我无法从迷宫中找到最短路径(广度优先搜索),javascript,algorithm,Javascript,Algorithm,我有一个迷宫,以矩阵的形式表示。有一个起点和一个终点。我只能在包含零的单元格中移动。我编写了一个查找所有可用路径的函数。但是函数没有找到最短路径。请帮忙完成这个功能 矩阵=[ [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 0, 0], [0, 1, 0, 1, 0] ]; var start=[4,0]; var end=[1,1]; 功能findWay(位置,结束) { 变量队列=[]; var=[]; 矩阵[位置[
矩阵=[
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 1, 0, 1, 0]
];
var start=[4,0];
var end=[1,1];
功能findWay(位置,结束)
{
变量队列=[];
var=[];
矩阵[位置[0]][位置[1]]=1;
推(位);
排队推(位置);
while(queue.length>0){
var pos=queue.shift();
变量方向=[[pos[0]+1,pos[1]],[pos[0],pos[1]+1],
[pos[0]-1,pos[1]],[pos[0],pos[1]-1]];
对于(变量i=0;i=矩阵[0]。长度)继续;
如果(方向[i][1]<0 | |方向[i][1]>=矩阵[0]。长度)继续;
如果(方向[i][0]==end[0]&方向[i][1]==end[1])返回访问;
如果(矩阵[方向[i][0]][方向[i][1]!=0)继续;
矩阵[方向[i][0]][方向[i][1]]=1;
按(方向[i]);
排队推送(方向[i]);
}
}
回访;
}
findWay(开始、结束)代码>更改背后的想法是,您要记住所采取的每个步骤的路径。向队列中添加新点时,请添加路径
通过此路径,您可以在执行下一步时检查是否已访问某个点。然后,您不必操纵矩阵/迷宫来记住访问的点
如果找到新点,请将新点和路径添加到队列中。如果该点包含在您的路径中,则您遇到了死胡同,并且没有将其添加到队列中
如果您迈出一步并到达终点,请将带有终点的对应路径添加到“有效路径”列表中。如果您只对最短路径感兴趣,那么第一条有效路径应该是最短路径之一
如果您想要全部,请在队列为空时中断,因为最终您将访问每条路径中的每个点
function findWay(position, end)
{
var queue = [];
var validpaths = [];
// New points, where we did not check the surroundings:
// remember the position and how we got there
// initially our starting point and a path containing only this point
queue.push({pos: position, path: [position]});
// as long as we have unchecked points
while(queue.length > 0){
// get next position to check viable directions
var obj = queue.shift();
var pos = obj.pos;
var path = obj.path;
// all points in each direction
var direction = [ [ pos[0] + 1, pos[1] ], [ pos[0], pos[1] + 1 ],
[ pos[0] - 1, pos[1] ], [ pos[0], pos[1] - 1 ] ];
for(var i = 0; i < direction.length; i++){
// check if out of bounds or in a "wall"
if (direction[i][0] < 0 || direction[i][0] >= matrix[0].length) continue;
if (direction[i][1] < 0 || direction[i][1] >= matrix[0].length) continue;
if (matrix[direction[i][0]][direction[i][1]] != 0) continue;
// check if we were at this point with this path already:
var visited = false;
for (var j = 0; j < path.length; j ++) {
if ((path[j][0] == direction[i][0] && path[j][1] == direction[i][1])) {
visited = true;
break;
}
}
if (visited) continue;
// copy path
var newpath = path.slice(0);
// add new point
newpath.push(direction[i]);
// check if we are at end
if (direction[i][0] != end[0] || direction[i][1] != end[1]) {
// remember position and the path to it
queue.push({pos: direction[i], path: newpath});
} else {
// remember this path from start to end
validpaths.push(newpath);
// break here if you want only one shortest path
}
}
}
return validpaths;
}
var paths = findWay(start, end);
函数findWay(位置,结束)
{
变量队列=[];
var validpaths=[];
//我们没有检查周围环境的新点:
//还记得我们的位置和我们是如何到达那里的吗
//最初,我们的起点和路径仅包含此点
push({pos:position,path:[position]});
//只要我们有未检查的点
while(queue.length>0){
//找到下一个位置,检查可行的方向
var obj=queue.shift();
var pos=obj.pos;
var path=obj.path;
//每个方向上的所有点
变量方向=[[pos[0]+1,pos[1]],[pos[0],pos[1]+1],
[pos[0]-1,pos[1]],[pos[0],pos[1]-1]];
对于(变量i=0;i=矩阵[0]。长度)继续;
如果(方向[i][1]<0 | |方向[i][1]>=矩阵[0]。长度)继续;
如果(矩阵[方向[i][0]][方向[i][1]!=0)继续;
//检查我们是否已经在这一点上使用此路径:
var=false;
对于(var j=0;j
这些变化背后的理念是,你要记住你所采取的每一个步骤的路径。向队列中添加新点时,请添加路径
通过此路径,您可以在执行下一步时检查是否已访问某个点。然后,您不必操纵矩阵/迷宫来记住访问的点
如果找到新点,请将新点和路径添加到队列中。如果该点包含在您的路径中,则您遇到了死胡同,并且没有将其添加到队列中
如果您迈出一步并到达终点,请将带有终点的对应路径添加到“有效路径”列表中。如果您只对最短路径感兴趣,那么第一条有效路径应该是最短路径之一
如果您想要全部,请在队列为空时中断,因为最终您将访问每条路径中的每个点
function findWay(position, end)
{
var queue = [];
var validpaths = [];
// New points, where we did not check the surroundings:
// remember the position and how we got there
// initially our starting point and a path containing only this point
queue.push({pos: position, path: [position]});
// as long as we have unchecked points
while(queue.length > 0){
// get next position to check viable directions
var obj = queue.shift();
var pos = obj.pos;
var path = obj.path;
// all points in each direction
var direction = [ [ pos[0] + 1, pos[1] ], [ pos[0], pos[1] + 1 ],
[ pos[0] - 1, pos[1] ], [ pos[0], pos[1] - 1 ] ];
for(var i = 0; i < direction.length; i++){
// check if out of bounds or in a "wall"
if (direction[i][0] < 0 || direction[i][0] >= matrix[0].length) continue;
if (direction[i][1] < 0 || direction[i][1] >= matrix[0].length) continue;
if (matrix[direction[i][0]][direction[i][1]] != 0) continue;
// check if we were at this point with this path already:
var visited = false;
for (var j = 0; j < path.length; j ++) {
if ((path[j][0] == direction[i][0] && path[j][1] == direction[i][1])) {
visited = true;
break;
}
}
if (visited) continue;
// copy path
var newpath = path.slice(0);
// add new point
newpath.push(direction[i]);
// check if we are at end
if (direction[i][0] != end[0] || direction[i][1] != end[1]) {
// remember position and the path to it
queue.push({pos: direction[i], path: newpath});
} else {
// remember this path from start to end
validpaths.push(newpath);
// break here if you want only one shortest path
}
}
}
return validpaths;
}
var paths = findWay(start, end);
函数findWay(位置,结束)
{
变量队列=[];
var validpaths=[];
//我们没有检查周围环境的新点:
//还记得我们的位置和我们是如何到达那里的吗
//最初,我们的起点和路径仅包含此点
push({pos:position,path:[position]});
//只要我们有未检查的点
while(queue.length>0){
//找到下一个位置,检查可行的方向
var obj=queue.shift();
var pos=obj.pos;
var path=obj.path;
//每个方向上的所有点
变量方向=[[pos[0]+1,pos[1]],[pos[0],pos[1]+1],
[pos[0]-1,pos[1]],[pos[0],pos[1]-1]];
对于(变量i=0;i