展示迷宮演算法
public class Maze { private int[][] maze = null;
 private int[] xx = { 1, 0, -1, 0 };
 private int[] yy = { 0, 1, 0, -1 };
 private Queue queue = null; public Maze(int[][] maze) {
  this.maze = maze;
  queue = new Queue(maze.length * maze.length);
 } public void go() {
  Point outPt = new Point(maze.length - 1, maze[0].length - 1);
  Point curPt = new Point(0, 0);
  Node curNode = new Node(curPt, null);
  maze[curPt.x][curPt.y] = 2;
  queue.entryQ(curNode);  while (!queue.isEmpty()) {
   curNode = queue.outQ();
   for (int i = 0; i < xx.length; ++i) {
    Point nextPt = new Point();
    nextPt.x = (curNode.point).x + xx[i];
    nextPt.y = (curNode.point).y + yy[i];
    if (check(nextPt)) {
     Node nextNode = new Node(nextPt, curNode);
     queue.entryQ(nextNode);
     maze[nextPt.x][nextPt.y] = 2;
     if (nextPt.equals(outPt)) {
      java.util.Stack<Node> stack = new java.util.Stack<Node>();
      stack.push(nextNode);
      while ((curNode = nextNode.previous) != null) {
       nextNode = curNode;
       stack.push(curNode);
      }
      System.out.println("A Path is:");
      while (!stack.isEmpty()) {
       curNode = stack.pop();
       System.out.println(curNode.point);
      }
      return;
     }
    }
   }
  }
  System.out.println("Non solution!");
 } private boolean check(Point p) {
  if (p.x < 0 || p.x >= maze.length || p.y < 0 || p.y >= maze[0].length) {
   return false;
  }
  if (maze[p.x][p.y] != 0) {
   return false;
  }
  return true;
 } public static void main(String[] args) {
  int[][] maze = {
   { 0, 0, 1, 0, 1, 0, 1, 0, 1, 0 },
   { 0, 0, 1, 1, 1, 0, 0, 0, 1, 0 },
   { 0, 1, 0, 0, 1, 0, 0, 0, 0, 1 },
   { 0, 0, 0, 0, 1, 0, 0, 0, 1, 1 },
   { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 },
   { 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 },
   { 0, 1, 0, 0, 1, 0, 0, 1, 0, 1 },
   { 1, 0, 1, 0, 1, 1, 0, 1, 0, 0 }
  };
  new Maze(maze).go();
 } private class Queue {  Node[] array = null;
  int size = 0;
  int len = 0;
  int head = 0;
  int tail = 0;  public Queue(int n) {
   array = new Node[n + 1];
   size = n + 1;
  }  public boolean entryQ(Node node) {
   if (isFull()) {
    return false;
   }
   tail = (tail + 1) % size;
   array[tail] = node;
   len++;
   return true;
  }  public Node outQ() {
   if (isEmpty()) {
    return null;
   }
   head = (head + 1) % size;
   len--;
   return array[head];
  }  public boolean isEmpty() {
   return (len == 0 || head == tail) ? true : false;
  }  public boolean isFull() {
   return ((tail + 1) % size == head) ? true : false;
  }
 } private class Node {  Point point = null;
  Node previous = null;  public Node() {
   this(null,null);
  }  public Node(Point point, Node node) {
   this.point = point;
   this.previous = node;
  }
 } private class Point {  int x = 0;
  int y = 0;  public Point() {
   this(0, 0);
  }  public Point(int x, int y) {
   this.x = x;
   this.y = y;
  }  public boolean equals(Point p) {
   return (x == p.x) && (y == p.y);
  }  @Override
  public String toString() {
   return "(" + x + "," + y + ")";
  }
 }
}
B. 迷宮演算法里輸入了迷宮具體的路徑信息之後怎麼用鍵盤輸出結果
迷宮演算法的輸出結果通常是迷宮的路徑,可以通過在控制台或命令行界面上輸出來展示。如果你想通過鍵盤輸入來控制迷宮演算法的執行過程,可以考慮使用以下方法:
- 在程序中添加鍵盤輸入功能,例如使用Java中的Scanner類或Python中的input函數等,讓用戶輸入迷宮的起點和終點等信息。 
- 在程序中添加控制台輸出功能,例如使用Java中的System.out.println方法或Python中的print函數等,讓程序在執行過程中輸出迷宮的狀態和路徑信息。 
- 在程序中添加控制台清屏功能,例如使用Java中的Runtime.getRuntime().exec("clear")方法或Python中的os.system("clear")函數等,讓程序在每次輸出前清空控制台,以便更好地展示迷宮的狀態和路徑信息。 
- 在程序中添加暫停功能,例如使用Java中的Thread.sleep方法或Python中的time.sleep函數等,在每次輸出後暫停一段時間,以便用戶觀察迷宮的狀態和路徑信息。 
通過以上方法,你可以在控制台上通過鍵盤輸入和輸出來展示迷宮演算法的執行過程和結果。
C. C++ DFS演算法實現走迷宮自動尋路
深度優先搜索(DFS)是一種遍歷圖或搜索樹的演算法。它的基本思想是從樹或圖的根節點開始,盡可能地深入到樹的某一分支中。當無法繼續向下探索時,回溯並嘗試其他分支。DFS演算法在圖或樹的遍歷中非常常用,但在迷宮問題中同樣適用。
在迷宮問題中,DFS演算法用於尋找從起點到終點的路徑。通過深度優先搜索,我們可以探索每個可能的路徑,直至找到通向目標的路線。
實現此演算法的代碼展示了手動操控與自動尋路兩種模式。手動模式允許用戶逐步探索迷宮,而自動模式則自動尋找並顯示從起點到終點的路徑。
程序的靈活性在於,只需調整迷宮地圖的二維數組和相關常量,即可改變地圖大小。自動尋路功能同樣可用。
理論上,DFS演算法在任何大小和復雜度的迷宮中都能有效尋找路徑。然而,這種演算法採用遞歸實現,導致空間佔用較大。當迷宮地圖增大到一定程度,如20*20,可能會觸發堆棧溢出異常。因此,程序中提供了18*18和15*15的迷宮地圖用於測試。
編譯環境為Windows VS2019。該演算法的代碼包含游戲類(Game.h)和主函數文件(迷宮.cpp),並提供了一定的調試支持。如果您在使用過程中發現任何問題,歡迎指出,感謝您對本項目的貢獻。
