【发布时间】:2014-12-27 20:43:09
【问题描述】:
当我可以从每个单元格(在数组边界内)向右、向左、向下或向上移动时,我需要计算数组中从一个单元格到另一个单元格的路径数。
如何避免在检查路径时重复已通过的单元格?还有一条规则:
您不能使用其他数组、循环和静态变量。
我怎样才能从这里继续?
我的递归函数:
public static int calcPath(int [][] a, int currentx, int currenty, int destx, int desty)
{
if (currentx == destx && currenty == desty)
return 1;
if(!bounds(a,currentx,currenty)) // if the cell isnt in the array bounds
return 0;
return calcPath(math,currentx+1,currenty,destx,desty) +
calcPath(math,currentx-1,currenty,destx,desty)+
calcPath(math,currentx,currenty+1,destx,desty)+
calcPath(math,currentx,currenty-1,destx,desty);
}
【问题讨论】:
-
它可能会帮助我避免重复返回最后一个单元格,但如果我沿着圆形路径(右下左上)移动,我仍然可能会重复一次我已经通过的单元格。
标签: java arrays recursion maze