我需要实现一个递归算法,它给出了二维数组中所有最短路径。我想知道是否可以使用Dijkstra算法或对其进行调整,或者如果您建议使用其他算法来查找它们。所以我有r行和c列,并且r * c点连接到正交和对角相邻点。我需要一个算法来计算从右下角(mat [r] [c])到左上角(mat [0] [0])的最短路径的数量。
这个算法是否已经完成了这项工作,我的意思是问题只是存在多少条路径而不是这些路径的长度。
static int numShortestLinks(int r, int c) {
if ((r==0) || (c==0)) {
return 0;
}
if ((r==1) || (c==1)) {
return 1;
}
return numShortestLinks(r-1,c-1) + numShortestLinks(r-1,c);
}
int mat[][] = new int [4][2];
System.out.println(numShortestLinks(mat.length,mat[0].length));
这段代码返回4,据我所知,有4条最短路径。