我正在尝试编写递归代码来查找方形网格的所有退出路径,但在 Project4.escape(Project4.java:41) 处出现一些错误,但我不知道错误是什么,因为以下行:
在 Project4.escape(Project4.java:41)
重复如此频繁以至于错误消息不可见。请帮我找出问题,并解决它。下面是我的逃生方法。
private static void escape(int row, int col, int[][] swamp,String escapePath)
{
if (row == swamp.length || row==0 || col == 0 || col == swamp.length)
{
//winner
System.out.println(escapePath);
}
else
{
for (int i = -1; i < 2; i++)
{
for (int j=-1;j<2;j++)
{
if (row+i>=0 && j+col>=0 && row+i<swamp.length && col+j<swamp.length &&
swamp[row + i][col + j] == 1)
{
escapePath+="["+row+","+col+"]";
swamp[row][col]=2;
escape(row,col,swamp,escapePath);
}
}
}
}
swamp[row][col]=1;
}
//编辑:下面是主要方法,它显示了我最初所说的逃逸。
public static void main(String[] args) throws Exception
{
int[] dropInPt = new int[2]; // row and col will be on the 2nd line of input file;
int[][] swamp = loadSwamp( args[0], dropInPt ); //returns an 8 by 8 grid of 0s and 1s and fills dropInPt with 1s
int row=dropInPt[0], col = dropInPt[1];
printSwamp( "\n SWAMP: dropped in at: ["+row+","+col+"]\n",swamp );
System.out.println("\n ESCAPE PATHS:\n");
String escapePath = "["+dropInPt[0]+","+dropInPt[1]+"]";
escape(row,col,swamp,escapePath);
}