使用递归在二维数组中查找路径

发布于 2024-08-28 17:06:39 字数 395 浏览 4 评论 0原文

我正在为我的 A level 做一个项目。它涉及找到网络的最大流量,我正在使用 javascript。

我有一个二维数组,数组中的值表示两点之间的距离。数组的一个例子:

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

我想我需要使用递归技术来查找路径;下面是一些伪代码,假设数组是 4x4。 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'm working on a project for my A level. It involves finding the maximum flow of a network, and I'm using javascript.

I have a 2D array, with values in the array representing a distance between the two points. 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. a is (0,0), b is (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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落在眉间の轻吻 2024-09-04 17:06:39

认真考虑使用 BFS。 Edmonds-KarpFord-Fulkerson 但寻路方法是固定的 - BFS,保证最坏情况 O(V * E^ 2),DFS则不然。 V 是顶点数,E - 边数。如果你仍然坚持DFS,那么至少你应该检查循环中下一个要访问的节点是否还没有被访问过,以防止永远递归。您可以为其使用布尔数组。

Seriously consider using 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. V is number of vertices and E - number of edges. 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. You can use a boolean array for it.

葬﹪忆之殇 2024-09-04 17:06:39

寻路可以通过使用洪水填充算法轻松实现,该算法可以以递归形式编写为

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, []);  

然而,这是非常低效的,并且一旦到达较大的网格就会导致堆栈溢出......更好的方法是是做一个软件栈...

而且,它只找到一条可行的路径,而不是最有效的路径。您必须在算法中添加计数[希望这不会花费太多精力]

Pathfinding can be easily achieved by using the floodfill algorithm, which can be written in a recursive form as

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文