0
我想写一个使用递归方法的程序查找从两维数组开始到最后的路径数。步骤应从[0] [0]开始直到结束。数组由0到99之间的整数填充。例如,如果[0] [0]是21,下一步可能是[0 + 2] [0 + 1]或[0 + 1] [0] [0] [0] [0] [0] [1] 0 + 2]。 该方法应返回不同路径的数量以达到最终点。修复递归方法
这里是我的代码:
在comilation一个具有溢出时方法获取[] []数组
public class two
{
int[][] _my=new int[0][0];
public two()
{
}
static int count=0;
public static int count;
ountPath(int[][] mat)
{
int nextX=0;
int nextY=0;
int current=mat[0][0];
if (current<=9)
{
nextX=current;
nextY=current;
if (checkBorders(0,0,nextX,nextY,mat)==0)
{
return 0;
}
}
else
{
nextX=(int)current/10;
nextY=current%10;
if (checkBorders(0,0,nextX,nextY,mat)==0)
{
return 0;
}
}
countPath(mat,nextX,nextY);
countPath(mat,nextY,nextX);
return count;
}
public static int countPath(int[][] mat,int x,int y)
{
int current=mat[x][y];
int nextX=0;
int nextY=0;
int terminate=0;
if (current<=9)
{
nextX=current;
nextY=current;
if (checkBorders(x,y,nextX,nextY,mat)==0)
{
return 0;
}
}
else
{
nextX=(int)current/10;
nextY=current%10;
if (checkBorders(x,y,nextX,nextY,mat)==0)
{
return 0;
}
}
if (((mat.length-1)==nextY)&&((mat[0].length-1)==nextX))
{
terminate=1;
}
if (terminate==1)
count++;
else
{
countPath(mat,nextX,nextY);
countPath(mat,nextY,nextX);
}
return count;
}
public static int checkBorders(int x,int y,int x1,int y1,int[][] mat)
{
int maxX=mat[0].length;
int maxY=mat.length;
if ((x+x1)>maxX)
return 0;
else if ((y+y1)>maxY)
return 0;
else return 1;
}
}
请帮我修复代码。
它不起作用?计数是一个静态变量吗?顺便说一句,在Java中,你可以使用'布尔' – UmNyobe
如果代码是自包含的,不仅可以说出问题的出处,还可以发布完整的代码。 – Sandro
是的,我使用静态计数变量。 – user2513643