我有一个班级,可以通过基本的迷宫找到路径。迷宫被传递到路径以及起始和结束位置。我使用递归来找到点之间的路径。该路径作为布尔数组返回。由于某种原因,传递给Path()的初始迷宫被改变了。所以我尝试制作迷宫数组的副本以避免更改任何值,但它仍然无效。
为什么path()更改为打开[] []?可能是C编程让我困惑。
public static boolean[][] path(boolean[][] open,
int start_i, int start_j,
int end_i, int end_j)
{
int n = open.length;
boolean[][] openCopy = new boolean[n][n]; //make a copy of open
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
openCopy[i][j] = open[i][j];
}
}
return findPath(openCopy, start_i, start_j, end_i, end_j);
}
public static boolean[][] findPath(boolean[][] openCopy, int start_i, int start_j, int end_i, int end_j)
{
boolean[][] path = new boolean[openCopy.length][openCopy[0].length];
if(openCopy[start_i][start_j] == false) //return false if current position is not open
return path;
else
openCopy[start_i][start_j] = false; //make current position false if not (to prevent infinite backtracking)
if(start_i == end_i && start_j == end_j) //if end found return true
{
path[start_i][start_j] = true;
return path;
}
path = findPath(openCopy, start_i+1, start_j, end_i, end_j); // Move North
path = findPath(openCopy, start_i, start_j+1, end_i, end_j); // Move East
path = findPath(openCopy, start_i-1, start_j, end_i, end_j); // Move South
path = findPath(openCopy, start_i, start_j-1, end_i, end_j); // Move West
return path;
}
答案 0 :(得分:3)
它没有,我知道这可能不是该方法的“适当”输入,但它证明了重点:
public class MainForm {
public static void main(String[] args) {
boolean[][] array = new boolean[][] {{false, false, true, false, false, false},
{false, false, true, false, false, false},
{false, false, true, false, false, false},
{false, false, true, false, false, false}};
boolean[] [] another = path(array, 0, 0, 3, 5);
for (boolean[] bArray : array) {
for (boolean b : bArray) {
System.out.println(b);
}
}
System.out.println("***********************");
for (boolean[] bArray : another) {
for (boolean b : bArray) {
System.out.println(b);
}
}
}
public static boolean[][] path(boolean[][] open, int start_i, int start_j, int end_i, int end_j) {
int n = open.length;
boolean[][] openCopy = new boolean[n][n]; // make a copy of open
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
openCopy[i][j] = open[i][j];
}
}
return findPath(openCopy, start_i, start_j, end_i, end_j);
}
public static boolean[][] findPath(boolean[][] openCopy, int start_i, int start_j, int end_i, int end_j) {
boolean[][] path = new boolean[openCopy.length][openCopy[0].length];
if (openCopy[start_i][start_j] == false) // return false if current position is not open
return path;
else
openCopy[start_i][start_j] = false; // make current position false if not (to prevent infinite backtracking)
if (start_i == end_i && start_j == end_j) // if end found return true
{
path[start_i][start_j] = true;
return path;
}
path = findPath(openCopy, start_i + 1, start_j, end_i, end_j); // Move North
path = findPath(openCopy, start_i, start_j + 1, end_i, end_j); // Move East
path = findPath(openCopy, start_i - 1, start_j, end_i, end_j); // Move South
path = findPath(openCopy, start_i, start_j - 1, end_i, end_j); // Move West
return path;
}
}
输出
false
false
true
false
false
false
false
false
true
false
false
false
false
false
true
false
false
false
false
false
true
false
false
false
***********************
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
显然,返回的数组和传递给path()的数组的内容不同,原始数组的原始值保持不变。我认为问题出在您的应用程序的其他地方。
答案 1 :(得分:0)
我认为问题在于,您正在传递内部中 -loop的引用。 尝试代替:
for(int j = 0; j < n; j++)
{
openCopy[i][j] = open[i][j];
}
这样:
for (int j = 0; j < n; j++) {
openCopy[i][j] = new Boolean(open[i][j]);
}
我认为它应该有所帮助。