跪求 迷宫问题 Java 算法描述
探路过程publicvoidexitMaze(PrintStreamout){currentCell=entryCell;currentList.add(currentC...
探路过程
public void exitMaze(PrintStream out) {
currentCell = entryCell;
currentList.add(currentCell);
out.println();
while (!currentCell.equals(exitCell)) {
int row = currentCell.getX();
int col = currentCell.getY();
display(System.out, store);
if (!currentCell.equals(entryCell))
store[row][col] = VISITED;
pushUnvisited(row - 1, col);
pushUnvisited(row + 1, col);
pushUnvisited(row, col - 1);
pushUnvisited(row, col + 1);
if (mazeStack.empty()) {
display(out, store);
out.println("Failure");
return;
} else {
currentCell = mazeStack.pop();
currentList.add(currentCell);
}
}
display(out, store);
out.println("Success");
请最好详细解说每条语句的意思,谢谢 展开
public void exitMaze(PrintStream out) {
currentCell = entryCell;
currentList.add(currentCell);
out.println();
while (!currentCell.equals(exitCell)) {
int row = currentCell.getX();
int col = currentCell.getY();
display(System.out, store);
if (!currentCell.equals(entryCell))
store[row][col] = VISITED;
pushUnvisited(row - 1, col);
pushUnvisited(row + 1, col);
pushUnvisited(row, col - 1);
pushUnvisited(row, col + 1);
if (mazeStack.empty()) {
display(out, store);
out.println("Failure");
return;
} else {
currentCell = mazeStack.pop();
currentList.add(currentCell);
}
}
display(out, store);
out.println("Success");
请最好详细解说每条语句的意思,谢谢 展开
展开全部
public void exitMaze(PrintStream out) {
//设置入口为当前格子
currentCell = entryCell;
//走过的格子
currentList.add(currentCell);
out.println();
//如果没有找到出口,则循环
while (!currentCell.equals(exitCell)) {
//当前格子的x,y坐标
int row = currentCell.getX();
int col = currentCell.getY();
display(System.out, store);
//如果当前格子不是入口
if (!currentCell.equals(entryCell))
//设置一个标记,表明当前格子已经被访问过
store[row][col] = VISITED;
//把上下左右4个方向的格子放到未访问列表
pushUnvisited(row - 1, col);
pushUnvisited(row + 1, col);
pushUnvisited(row, col - 1);
pushUnvisited(row, col + 1);
//如果迷宫空,则失败
if (mazeStack.empty()) {
display(out, store);
out.println("Failure");
return;
//否则
} else {
//从迷宫栈顶端拿掉一个格子作为当前格子
currentCell = mazeStack.pop();
//并且添加到走过的格子列表
currentList.add(currentCell);
}
}
//因为已经退出循环,并且没有失败,所以打印成功消息
display(out, store);
out.println("Success");
//设置入口为当前格子
currentCell = entryCell;
//走过的格子
currentList.add(currentCell);
out.println();
//如果没有找到出口,则循环
while (!currentCell.equals(exitCell)) {
//当前格子的x,y坐标
int row = currentCell.getX();
int col = currentCell.getY();
display(System.out, store);
//如果当前格子不是入口
if (!currentCell.equals(entryCell))
//设置一个标记,表明当前格子已经被访问过
store[row][col] = VISITED;
//把上下左右4个方向的格子放到未访问列表
pushUnvisited(row - 1, col);
pushUnvisited(row + 1, col);
pushUnvisited(row, col - 1);
pushUnvisited(row, col + 1);
//如果迷宫空,则失败
if (mazeStack.empty()) {
display(out, store);
out.println("Failure");
return;
//否则
} else {
//从迷宫栈顶端拿掉一个格子作为当前格子
currentCell = mazeStack.pop();
//并且添加到走过的格子列表
currentList.add(currentCell);
}
}
//因为已经退出循环,并且没有失败,所以打印成功消息
display(out, store);
out.println("Success");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询