读取迷宫文件并打印



这个程序是关于迷宫递归的,我正在努力找出如何读取迷宫文件并打印已解决的迷宫,因为我是新来的Java。谁能向我解释如何从Main方法调用打印方法?预先感谢您!

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++;
 }

此外,迷宫阵列实际上永远不会实例化,主要方法永远不会调用" solve(("或" print(("。这些是您可以在主要方法中处理的事情。

最后,如果您要调用" solve((",则必须决定是否要实例化迷宫类的实例并在此上进行求解(这涉及大量的代码更改,但是是正确的方法(,否则" solve(("也应该是静态方法,这意味着"有效(("也必须是静态的。

p.s。:您的前面有错误

for (int c=0; columns < maze[r].length; c++)

您应该将"列"更改为" C"。

p.p.s。,在您的求解方法中,您将INT分配给char数组。将7和8单点引用,以表明它们是chars,而不是ints。

 maze[r][c] = '7';  // cell has been tried

这在您的有效((方法中尤其重要,因为您正在测试迷宫[r] [c] == 1,但是如果迷宫[r] [c] =='1',您应该测试。

我自己进行了所有这些更改,并将其作为输出

 000700000000000  
 000700007000010  
 000777777777000   
 000700000007000  
 000777770007000  
 000000070007000  
 000077770007000  
 000070070007010  
 000070070000000  
 000070000000000  
 000077777770000  
 000000000070000  
 000000000070000  
 000001000077770  
 000000000070000

最新更新