这个程序是关于迷宫递归的,我正在努力寻找如何阅读迷宫文件并打印已解决的迷宫,因为我是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
答案 0 :(得分:0)
我会做一些不同的事情,但重要的是你没有用任何数据填充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++;
}
此外,迷宫数组从未实际实例化,主要方法永远不会调用&#34; solve()&#34;或&#34; print()&#34;。这些是您可以在主方法中处理的事情。
最后,如果您打算打电话给&#34;解决(),&#34;您必须决定是否要实例化Maze类的实例并在其上调用solve(这将涉及相当多的代码更改,但这是正确的方法),否则&#34; solve()&#34 ;也应该是一个静态方法,这意味着&#34;有效()&#34;也必须是静态的。
P.S。:你的for循环中有错误
for (int c=0; columns < maze[r].length; c++)
你应该改变&#34;列&#34;到&#34; c&#34;。
P.P.S。,在你的求解方法中,你将int赋给char数组。将7和8放在单引号中以表示它们是字符,而不是整数。
maze[r][c] = '7'; // cell has been tried
这在你的valid()方法中尤其重要,因为你在迷宫[r] [c] == 1时测试,但你应该测试迷宫[r] [c] ==& #39; 1&#39;
我自己完成了所有这些更改并将其作为输出
000700000000000
000700007000010
000777777777000
000700000007000
000777770007000
000000070007000
000077770007000
000070070007010
000070070000000
000070000000000
000077777770000
000000000070000
000000000070000
000001000077770
000000000070000