我正在编写一个递归方法来查找二维数组中的所有可能路径。从左上角的点(0,0)到右下角的最后一点。并返回路径的总和。
public static void printPathWeights(int[][] m)
{
printPathWeights(m, 0, 0, 0);
}
public static void printPathWeights(int[][] m, int row, int col, int sum)
{
if(row == 0 && col ==0)
sum = 0;
if (row == m.length - 1 && col == m[row].length - 1)
System.out.println(sum);
else
{
if (row >= 0 && row < m.length && col >= 0 && col < m[row].length)
printPathWeights(m, row - 1, col, sum += m[row][col]); // Up
if (row >= 0 && row < m.length && col >= 0 && col < m[row].length)
printPathWeights(m, row + 1, col, sum += m[row][col]); // Down
if (row >= 0 && row < m.length && col >= 0 && col < m[row].length)
printPathWeights(m, row, col - 1, sum += m[row][col]); // Left
if (row >= 0 && row < m.length && col >= 0 && col < m[row].length)
printPathWeights(m, row, col + 1, sum += m[row][col]); // Right
}
}
目前我的问题是这个函数进入无限循环而不打印我的总和
答案 0 :(得分:1)
我认为它会陷入困境:
if (row >= 0 && row < m.length && col >= 0 && col < m[row].length)
printPathWeights(m, row, col - 1, sum += m[row][col]); // Left
if (row >= 0 && row < m.length && col >= 0 && col < m[row].length)
printPathWeights(m, row, col + 1, sum += m[row][col]); // Right
它将永远地来回跳跃。
而且,正如米克尔所指出的那样,为什么路径不能上升?
解决方案:(假设路径可能不会自行交叉,否则总和会变为无穷大)
跟踪你去过的地方。将该历史记录传递给下一次递归。
将您所使用的图块的值添加到作为参数传递的总和值。
如果您已经完成,请打印总和。
否则: 尝试进入四个可能的方向。如果在那个方向上没有单元格,那么这将失败,IE就在你的边缘。如果你已经去过那里也会失败。
如果你无法移动到任何地方,IE就会被卡住,你没有做任何事情就会回来。
答案 1 :(得分:0)
您需要将单元格标记为已访问,否则您的算法将让您永远在同一行上左右移动。另外,为什么你不能上去呢?
答案 2 :(得分:0)
public static void printPathWeights(int [][] m)
{
printPathWeights (m, 0, 0, 0);
}
private static void printPathWeights(int [][]m, int i, int j,int sum)
{
if (i<0 || i>=m.length || j<0 || j>=m[0].length)
return;
if (m[i][j] == -1)
return;
if (i==m.length-1 && j==m[0].length-1)
System.out.println (sum + m[m.length-1][m[0].length-1]);
int temp = m[i][j];
m[i][j] = -1;
printPathWeights (m, i+1, j, sum+temp);
printPathWeights (m, i, j+1, sum+temp);
printPathWeights (m, i-1, j, sum+temp);
printPathWeights (m, i, j-1, sum+temp);
m[i][j] = temp;
}
试试这个,你把-1放在你已经比较早的地方,然后在折叠处你放回数字,准备好接下来的路径
答案 3 :(得分:0)
完成工作代码。测试... 强>
public static void printPathWeights(int [] [] m) {
printPathWeights( m, 0, 0, 0, 0 );
}
// 1 - 如果右侧细胞清除,则2 - 如果左侧细胞清除,则为0 - 中性
private static int printPathWeights(int [] [] m,int sum,int flag,int row,int col) {
if ( row == m.length - 1 && col == m[0].length - 1 ){
System.out.print( (sum + m[row][col]) + " " );
return sum;
}
//Check if the RIGHT cell legal - (That's to say current cell was not called by the rigth cell
// and the boundry is legal)
if( flag != 1 && ((col+1) <= m[0].length - 1) )
printPathWeights( m, sum + m[row][col], 2, row, col + 1 );
//Check if the DONN/Below cell is legal - (That is to say the boundry is legal. We do not check
// if we came from below cell since we do not have 'UP' call)
if( ((row+1) <= m.length - 1) )
printPathWeights( m, sum + m[row][col], 0, row + 1, col );
//Check if the LEFT cell legal - (That's to say current cell was not called by the LEFT cell
// and the boundry is legal and if We are at the top level we DO NOT TURN LEFT)
if( flag != 2 && ((col - 1) >= 0) && row != m.length - 1 )
printPathWeights( m, sum + m[row][col], 1, row, col - 1 );
return sum;
}