java代码错误,迷宫递归算法
首先,maze.txt中的第一行是向后的,应该是7行(0到6)和20列的720行 此外,在执行递归之前,您需要找到S的位置,因此将代码块向上移动java代码错误,迷宫递归算法,java,recursion,Java,Recursion,首先,maze.txt中的第一行是向后的,应该是7行(0到6)和20列的720行 此外,在执行递归之前,您需要找到S的位置,因此将代码块向上移动 最后,您需要找到mazeSolver内部数组的尺寸,然后测试两个具有行+1和列+1的if测试块-也就是说,如果您从底部的6,12开始,然后尝试获取迷宫[7,12]元素(行+1)你的程序将爆炸。在java 20 7 XXXXXXXXXXXXXXXXXXXX EXX xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx XXXXXX xx
最后,您需要找到mazeSolver内部数组的尺寸,然后测试两个具有行+1和列+1的if测试块-也就是说,如果您从底部的6,12开始,然后尝试获取迷宫[7,12]元素(行+1)你的程序将爆炸。在java 20 7 XXXXXXXXXXXXXXXXXXXX EXX xxxxx xxxxx xxxxx xxxxx xxxxx xxxxx XXXXXX xx xx xx xx xx xx xx xx xx xx XXXXXXXXX xxxxxxxxxx SXXXXXX中删除这一行,找到一条穿过迷宫的路,S是起点6 12。结束于E,018。我建议注释掉main中的try/catch,以便从引发异常的点获得堆栈转储。如果输入字符串长于列数,则可以在打印/生成循环中获得异常。如果数据文件迷宫超出了声明的迷宫大小,我不确定应该怎么做。如果没有额外的说明,我会忽略额外的字符,如果其中任何字符是非空的,我会打印一条消息。
20 7
xxxxxxxxxxxxxxxxxxEx
x x xxxx x
x xxxxx xxxxx xx x
x xxxxx xxxxxxx xx x
x xx xx x
x xxxxxxxxxx xx x
xxxxxxxxxxxxSxxxxxxx
Finding a way through the maze, S is the starting 6 12. And ends at E, 0 18.
import java.util.*;
import java.io.*;
public class mazeSolver {
boolean wall = false;
char[][] maze;
boolean solved;
public mazeSolver(char[][] in_maze) {
maze = in_maze;
}
public void findPath(int row, int col) {
if (maze[row][col] == 'E') {
solved = true;
return;
}
maze[row][col] = 'b';
if (maze[row + 1][col] == ' ' || maze[row + 1][col] == 'E') {
findPath(row + 1, col);
}
else if (maze[row][col + 1] == ' ' || maze[row][col + 1] == 'E') {
findPath(row, col + 1);
}
else if (maze[row - 1][col] == ' ' || maze[row - 1][col] == 'E') {
findPath(row -1, col);
}
else if (maze[row][col - 1] == ' ' || maze[row][col - 1] == 'E') {
findPath(row, col - 1);
}
else {
wall = true;
return;
}
if (wall) {
wall = false;
findPath(row, col);
}
if (solved) {
maze[row][col] = '+';
}
}
public void printMaze(int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(maze[i][j]);
}
System.out.println();
}
}
public void solveCheck(int rows, int cols) {
boolean solveable = false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (maze[i][j] != '+') {
solveable = true;
}
break;
}
break;
}
if(!solveable){
System.out.println("S is unreachable");
}
}
import java.util.Scanner;
import java.io.File;
public class ADTmaze {
public static void main(String[] args) {
try {
Scanner myScanner = new Scanner(
new File("maze.txt"));
int numRows = myScanner.nextInt();
int numCols = myScanner.nextInt();
myScanner.nextLine();
int startX = 0;
int startY = 0;
// New maze
char[][] maze = new char[numRows][numCols];
System.out.println(numRows +","+ numCols);
for (int i = 0; i < numRows; i++) {
String nextLine = myScanner.nextLine();
for (int j = 0; j < numCols; j++) {
char nextChar = nextLine.charAt(j);
maze[i][j] = nextChar;
System.out.print(nextChar);
}
System.out.println();
}
// Solve the maze
mazeSolver newMaze = new mazeSolver(maze);
System.out.println();
newMaze.findPath(startX, startY);
newMaze.printMaze(numRows, numCols);
// Find the starting point
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (maze[i][j] == 'S') {
System.out.println("Starting coordinates: "
+ i + ", " + j);
startX = i;
startY = j;
}
}
}
} catch (Exception ex) {
System.out.println(ex);
}
}
20,7
xxxxxxx
x x
x xxxxx
x xxxxx
x
x xxxxx
xxxxxxx
java.util.NoSuchElementException: No line found