读取迷宫文件并打印
reading the maze file and print it
这个程序是关于迷宫递归的,由于我是 Java 的新手,所以我正在努力寻找如何读取迷宫文件并打印已解决的迷宫。谁能给我解释一下如何从 main 方法调用 print 方法?提前致谢!
public class Maze {
private static char[][] maze;
private static int rows = 0;
private static int columns = 0;
public Maze(char[][] mazeIn) {
maze = mazeIn;
}
private static boolean valid (int r, int c) {
boolean result = false;
// check if cell is in the bounds of the matrix
if (r >= 0 && r < maze.length &&
c >= 0 && c < maze[0].length)
// check if cell is not blocked and not previously tried
if (maze[r][c] == '1')
result = true;
return result;
} // method valid
public static boolean solve (int r, int c) {
boolean done = false;
if (valid (r, c)) {
maze[r][c] = '7'; // cell has been tried
if (r == maze.length-1 && c == maze[0].length-1)
done = true; // maze is solved
else {
done = solve (r+1, c); // down
if (!done)
done = solve (r, c+1); // right
if (!done)
done = solve (r-1, c); // up
if (!done)
done = solve (r, c-1); // left
}
if (done) // part of the final path
maze[r][c] = '8';
}
return done;
} // method solve
public static void print () {
System.out.println();
for (int r=0; r < maze.length; r++) {
for (int c=0; c < maze[r].length; c++)
System.out.print (maze[r][c]);
System.out.println();
}
System.out.println();
} // method print_maze
public static void main(String[] args) throws IOException {
String fileName = "Maze.txt";
try {
String readline;
FileReader fileReader =
new FileReader(fileName);
BufferedReader br =
new BufferedReader(fileReader);
int line = 0;
while((readline = br.readLine()) != null) {
System.out.println(readline); //loads the maze
char[] charArr = readline.toCharArray();
maze[line] = charArr; // error here
line++;
}
br.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
}
maze.txt 文件看起来像这样
000100000000000
000100001000010
000111111111000
000100000001000
000111110001000
000000010001000
000011110001000
000010010001010
000010010000000
000010000000000
000011111110000
000000000010000
000000000010000
000001000011110
000000000010000
有些事情我会做不同的事情,但重要的是您没有用任何数据填充 char 数组。在读取数据时,您需要将其填充到 char 数组中。像这样的东西会起作用:
int line = 0;
while((readline = br.readLine()) != null) {
System.out.println(readline); //loads the maze
char[] charArr = readline.toCharArray();
maze[line] = charArr;
line++;
}
此外,迷宫数组从未实际实例化,而且 main 方法也从未调用 "solve()" 或 "print()"。这些是您可以在主方法中处理的事情。
最后,如果您要调用 "solve(),",您必须决定是否要实例化 Maze class 的实例并对其调用 solve(这会涉及很多代码更改,但这是正确的方法),否则 "solve()" 也应该是静态方法,这意味着 "valid()" 也必须是静态的。
P.S.: 你的for循环有错误
for (int c=0; columns < maze[r].length; c++)
您应该将 "columns" 更改为 "c"。
P.P.S., 在您的求解方法中,您将整数分配给 char 数组。将 7 和 8 放在单引号中以表明它们是字符,而不是整数。
maze[r][c] = '7'; // cell has been tried
这在您的 valid() 方法中尤为重要,因为您正在测试 maze[r][c]==1,但您应该测试 maze[r][c]==' 1'.
我自己做了所有这些更改并将其作为输出
000700000000000
000700007000010
000777777777000
000700000007000
000777770007000
000000070007000
000077770007000
000070070007010
000070070000000
000070000000000
000077777770000
000000000070000
000000000070000
000001000077770
000000000070000
这个程序是关于迷宫递归的,由于我是 Java 的新手,所以我正在努力寻找如何读取迷宫文件并打印已解决的迷宫。谁能给我解释一下如何从 main 方法调用 print 方法?提前致谢!
public class Maze {
private static char[][] maze;
private static int rows = 0;
private static int columns = 0;
public Maze(char[][] mazeIn) {
maze = mazeIn;
}
private static boolean valid (int r, int c) {
boolean result = false;
// check if cell is in the bounds of the matrix
if (r >= 0 && r < maze.length &&
c >= 0 && c < maze[0].length)
// check if cell is not blocked and not previously tried
if (maze[r][c] == '1')
result = true;
return result;
} // method valid
public static boolean solve (int r, int c) {
boolean done = false;
if (valid (r, c)) {
maze[r][c] = '7'; // cell has been tried
if (r == maze.length-1 && c == maze[0].length-1)
done = true; // maze is solved
else {
done = solve (r+1, c); // down
if (!done)
done = solve (r, c+1); // right
if (!done)
done = solve (r-1, c); // up
if (!done)
done = solve (r, c-1); // left
}
if (done) // part of the final path
maze[r][c] = '8';
}
return done;
} // method solve
public static void print () {
System.out.println();
for (int r=0; r < maze.length; r++) {
for (int c=0; c < maze[r].length; c++)
System.out.print (maze[r][c]);
System.out.println();
}
System.out.println();
} // method print_maze
public static void main(String[] args) throws IOException {
String fileName = "Maze.txt";
try {
String readline;
FileReader fileReader =
new FileReader(fileName);
BufferedReader br =
new BufferedReader(fileReader);
int line = 0;
while((readline = br.readLine()) != null) {
System.out.println(readline); //loads the maze
char[] charArr = readline.toCharArray();
maze[line] = charArr; // error here
line++;
}
br.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
}
}
maze.txt 文件看起来像这样
000100000000000
000100001000010
000111111111000
000100000001000
000111110001000
000000010001000
000011110001000
000010010001010
000010010000000
000010000000000
000011111110000
000000000010000
000000000010000
000001000011110
000000000010000
有些事情我会做不同的事情,但重要的是您没有用任何数据填充 char 数组。在读取数据时,您需要将其填充到 char 数组中。像这样的东西会起作用:
int line = 0;
while((readline = br.readLine()) != null) {
System.out.println(readline); //loads the maze
char[] charArr = readline.toCharArray();
maze[line] = charArr;
line++;
}
此外,迷宫数组从未实际实例化,而且 main 方法也从未调用 "solve()" 或 "print()"。这些是您可以在主方法中处理的事情。
最后,如果您要调用 "solve(),",您必须决定是否要实例化 Maze class 的实例并对其调用 solve(这会涉及很多代码更改,但这是正确的方法),否则 "solve()" 也应该是静态方法,这意味着 "valid()" 也必须是静态的。
P.S.: 你的for循环有错误
for (int c=0; columns < maze[r].length; c++)
您应该将 "columns" 更改为 "c"。
P.P.S., 在您的求解方法中,您将整数分配给 char 数组。将 7 和 8 放在单引号中以表明它们是字符,而不是整数。
maze[r][c] = '7'; // cell has been tried
这在您的 valid() 方法中尤为重要,因为您正在测试 maze[r][c]==1,但您应该测试 maze[r][c]==' 1'.
我自己做了所有这些更改并将其作为输出
000700000000000
000700007000010
000777777777000
000700000007000
000777770007000
000000070007000
000077770007000
000070070007010
000070070000000
000070000000000
000077777770000
000000000070000
000000000070000
000001000077770
000000000070000