使用2d阵列解决迷宫。 Java的。递归

时间:2015-02-08 02:09:13

标签: java arrays

在java中使用迷宫解决程序需要一些帮助。

程序必须从文件中读取迷宫,将其存储到数组中,解决它,并在绘图面板中显示解决方案。我正在努力将它存储到一个数组中,我真的不确定如何解决它并显示它。但是,如果我能在阵列部分得到一些帮助,我会非常感激。

以下是输入文件的示例。我想让它适用于这种结构的任何迷宫,(带+, - ,S,E和|)。前两个数字(8,10)表示高度和宽度,行数和列数。

8 10
+-+-+-+-+-+-+-+-+-+  
|                 |  
+ +-+-+-+ +-+-+-+ +  
| |             | |  
+ + +-+-+-+-+-+ + +  
| | |         | | |  
+ + + +-+-+-+ + + +-+
| | | |     | | |  S|
+ + + + +-+ + + + +-+
| |   |   |E| | | |  
+ + + +-+ +-+ + + +  
| | |         | | |  
+ + +-+-+-+-+-+ + +  
| |             | |  
+ +-+-+-+-+-+-+-+ +  
|                 |  
+-+-+-+-+-+-+-+-+-+

到目前为止,这是我的代码:

import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class MazeSolver {

   // The name of the file describing the maze
   static String mazefile;
   static int width;
   static int height;
   public static void main(String[] args) throws FileNotFoundException {
      if (handleArguments(args)) {

         readMazeFile(mazefile);
         DrawMaze.draw();

         if (solveMaze())
            System.out.println("Solved!");
         else
            System.out.println("Maze has no solution.");
      }
      else {
      System.out.println("The arguments are invalid.");
      }
   }

   // Handle the input arguments
   static boolean handleArguments(String[] args) {
      if (args.length > 4 || args.length < 1) {
         System.out.println("There are too many or too few command line arguments");
         return false;
      }
      if (args.length == 1) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         return true;
      }
      if (args.length == 2) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         if (cellsize < 10) {
            return false;
         }
         return true;
      }
      if (args.length == 3) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         if (borderwidth < 5) {
            return false;
         }
         return true;
      }
      if (args.length == 4) {
         String mazefile = args[0];
         File file = new File(mazefile);
         if (!file.canRead()) {
            return false;
         }
         int cellsize = Integer.parseInt(args[1]);
         int borderwidth = Integer.parseInt(args[2]);
         int sleeptime = Integer.parseInt(args[3]);
         if (sleeptime < 0 || sleeptime > 10000) {
            return false;
         }
         return true;
      }   
      return false;
   }

   // Read the file describing the maze.
   static char[][] readMazeFile(String mazefile) throws FileNotFoundException {

      Scanner scanner = new Scanner(new File(mazefile));
      height = scanner.nextInt();
      width = scanner.nextInt();
      int arrayHeight = 2  * height + 1;
      int arrayWidth = 2 * width + 1;
      char[][] mazeArrays = new char[arrayHeight][arrayWidth];
      while (scanner.hasNextLine()) {
         String line = scanner.nextLine();
         System.out.println(line);
         for (int row = 0; row < arrayHeight; row++) {
            for (int col = 0; col < arrayWidth; col++) {
               mazeArrays[row][col] = line.charAt(col);
            }
         }

      }
      return mazeArrays;
   }

   // Solve the maze.      
   static boolean solveMaze() {
      return true;
   }
}

我认为我已经处理了命令行参数。 readMazeFile方法是我目前正在努力的方法。我只是无法将迷宫包裹在阵列中并将其解决。

谢谢!

1 个答案:

答案 0 :(得分:0)

要做的第一件事是为你设计一个数据结构来存储迷宫。我建议使用一种结构,使解决方案尽可能简单,即使打印更复杂。这是一个简单的例子:

class Node {
    private final int row;
    private final int col;
    private final List<Node> paths;
}

class Maze {
    private final int rowCount;
    private final int colCount;
    private final List<Node> nodes;
    private Node start;
    private Node end;
}

在我看来,这比数组之类的东西更有用。阵列可以很容易地打印迷宫,但这不是操作中最困难的部分。路径查找算法需要能够轻松地从该数据结构允许的任何位置获取路径。

你在阅读迷宫时要求一些帮助。我建议让阅读方法成为一个静态的“建设者”。 Maze内的方法。一般来说,结构将类似于:

class Maze {
    public static Maze buildMaze(String mazeFile) {
        // read row & col size from file
        Maze maze = new Maze(rows, cols);
        // skip first line (invariant)
        for (int row = 0; row < rows; row++) {
            // get next 2 lines (for horizontal & vertical paths)
            for (int col = 0; col < cols; col++) {
                // get corresponding horizontal wall or space
                if (isHorizontalPath) {
                    maze.getNode(row,col).addHorizontalPath();
                }
                if (hasVerticalPath) {
                    maze.getNode(row, col).addVerticalPath();
                }
                // check for S and E
                if (isStart) {
                    maze.setStart(row, col);
                } else if (isEnd) {
                    maze.setEnd(row, col);
                }
            }
        }
        return maze;
    }
}
相关问题