简体   繁体   English

使用递归在2D数组中查找路径

[英]Using recursion to find paths in a 2D array

I'm working on a project for my A level. 我正在为我的A级项目工作。 It involves finding the maximum flow of a network, and I'm using javascript. 它涉及寻找网络的最大流量,而我使用的是javascript。

I have a 2D array, with values in the array representing a distance between the two points. 我有一个2D数组,该数组中的值代表两点之间的距离。 An example of the array: 数组的示例:

0 2 2 0
0 0 1 2
0 0 0 2
0 0 0 0

I think I need to use a recursive technique to find a path; 我认为我需要使用递归技术来找到路径; below is some pseudocode, assuming that the array is 4x4. 下面是一些伪代码,假设数组为4x4。 a is (0,0), b is (3,3). a为(0,0),b为(3,3)。

function search(a,b)
  from a to b
    if element(i,j) != 0 then
      store value of element
      search(j,3)

I was wondering if that was the right construction for a depth first search. 我想知道这是否是进行深度优先搜索的正确结构。 Thanks for any help. 谢谢你的帮助。

Seriously consider using BFS. 认真考虑使用BFS。 Edmonds-Karp is the Ford-Fulkerson but the path finding method is fixed - BFS, which guarantees worst case O(V * E^2), which is not the case with DFS. Edmonds-KarpFord-Fulkerson,但路径查找方法是固定的-BFS,它可以保证最坏情况O(V * E ^ 2),而DFS则不能。 V is number of vertices and E - number of edges. V是顶点数,E是边数。 If you still insist on DFS, then at least you should check that the node which you are visiting next in the loop is not yet visited to prevent eternal recursion. 如果您仍然坚持使用DFS,则至少应检查是否要访问循环中下一个要访问的节点,以防止永恒递归。 You can use a boolean array for it. 您可以为其使用布尔数组。

Pathfinding can be easily achieved by using the floodfill algorithm, which can be written in a recursive form as 可以使用Floodfill算法轻松实现寻路,该算法可以递归形式编写为

function floodFill(x, y, prevPoints)
{
 var prevPoints = prevPoints.concat([]); //make a copy of the points list since JS uses ref

 if(grid[x][y].isExit) return prevPoints;
 grid[x][y].accessed = true;
 prevPoints.push([x, y]);

 var result;
 var cfr; //cellfillresult
 if(grid[x+1][y].isPath && !grid[x+1][y].accessed) cfr = floodFill(x+1, y, prevPoints);
 if(cfr != null) result = cfr;

 if(grid[x-1][y].isPath && !grid[x-1][y].accessed) cfr = floodFill(x-1, y, prevPoints);
 if(cfr != null) result = cfr;

 if(grid[x][y+1].isPath && !grid[x][y+1].accessed) cfr = floodFill(x, y+1, prevPoints);
 if(cfr != null) result = cfr;

 if(grid[x][y-1].isPath && !grid[x][y-1].accessed) cfr = floodFill(x, y-1, prevPoints);
 if(cfr != null) result = cfr;

 return result;
}

var pathToExit = floodFill(entranceX, entranceY, []);  

However, this is highly inefficient and will cause a stack overflow once you get to larger-ish grids... A better way to do this would be to make a software stack... 但是,这种方法效率极低,一旦进入较大的网格,就会导致堆栈溢出。一种更好的方法是制作软件堆栈。

Also, it only finds a path that works, but not the most efficient path. 此外,它只会找到一条可行的路径,而不是最有效的路径。 You'll have to add counting to the algorithm [which shouldn't take too much effort, hopefully] 您必须在算法中增加计数[希望不会花费太多的精力]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM