1. 程式人生 > 程式設計 >Java專案實現尋找迷宮出路

Java專案實現尋找迷宮出路

本文例項為大家分享了Java實現尋找迷宮出路的具體程式碼,供大家參考,具體內容如下

專案名稱

尋找迷宮出路

專案描述

給定一個自定義迷宮,0表示能通過,1表示不能通過。通過程式找出正確的迷宮出路,並將正確的路線改為2輸出。

Java專案實現尋找迷宮出路

程式碼實現

測試類

public class Test {
 public static void main(String[] args) {
  Maze maze = new Maze();
  maze.begin();
 }
}

主類:實現主方法

public class Maze {
 private MazeNode[][] mazeNodes;
 private int row;
 private int col;
 private Stack<MazeNode> stack = new Stack<>();
 private static Scanner scanner = new Scanner(System.in);

 public Maze(){
  System.out.println("請輸入行列數");
  row = scanner.nextInt();
  col = scanner.nextInt();
  mazeNodes = new MazeNode[row][col];
 }
 public void initValue(){
  System.out.println("輸入迷宮路徑:");
  for(int i=0;i<row;i++){
   for(int j=0;j<col;j++){
    // i j
    mazeNodes[i][j] = new MazeNode(scanner.nextInt(),i,j);
   }
  }
 }
 //2. 根據當前節點的四個方向上面的value值
 // 初始化當前節點的四個方向行走狀態
 public void initWayState(){
  for(int i=0;i<row;i++){
   for(int j=0;j<col;j++){
    //i j
    if(mazeNodes[i][j].getValue()==0){
     //東 :看東邊節點的值是0
     if(j+1<col && mazeNodes[i][j+1].getValue() == 0){
      // 將當前節點i j 的東邊方向設定成可走狀態
      mazeNodes[i][j].setWayState(Constant.WAY_EAST,Constant.WAY_ABLE);
     }
     //西
     if(j-1>0 && mazeNodes[i][j-1].getValue() == 0){
      mazeNodes[i][j].setWayState(Constant.WAY_WEST,Constant.WAY_ABLE);
     }
     //南
     if(i+1<row && mazeNodes[i+1][j].getValue() == 0){
      mazeNodes[i][j].setWayState(Constant.WAY_SOUTH,Constant.WAY_ABLE);
     }
     //北
     if(i-1>0 && mazeNodes[i-1][j].getValue() == 0){
      mazeNodes[i][j].setWayState(Constant.WAY_NORTH,Constant.WAY_ABLE);
     }
    }
   }
  }
 }
 //走迷宮
 public void goMaze(){
  if(mazeNodes[0][0].getValue()!=0){
   System.out.println("沒有迷宮路徑");
   return;
  }
  stack.push(mazeNodes[0][0]);
  while (!stack.isEmpty()) {//TODO:??????
   MazeNode top = stack.peek();
   //獲取當前棧頂元素在二維陣列中的行列座標
   int i = top.getI();
   int j = top.getJ();
   if(i == row-1 && j==col-1){
    System.out.println("找到迷宮路徑");
    return;
   }
   //TODO:
   if(mazeNodes[i][j].getWayState(Constant.WAY_SOUTH) == Constant.WAY_DISABLE &&
     mazeNodes[i][j].getWayState(Constant.WAY_EAST) == Constant.WAY_DISABLE &&
     mazeNodes[i][j].getWayState(Constant.WAY_NORTH) == Constant.WAY_DISABLE &&
     mazeNodes[i][j].getWayState(Constant.WAY_WEST) == Constant.WAY_DISABLE){
    stack.pop();
   }
   //東
   else if (mazeNodes[i][j].getWayState(Constant.WAY_EAST)) {
    stack.push(mazeNodes[i][j + 1]);
    mazeNodes[i][j+1].setWayState(Constant.WAY_WEST,Constant.WAY_DISABLE);
    mazeNodes[i][j].setWayState(Constant.WAY_EAST,Constant.WAY_DISABLE);
   }//南
   else if (mazeNodes[i][j].getWayState(Constant.WAY_SOUTH)) {
    //如果南邊方向可走,將南邊節點進行入棧操作
    stack.push(mazeNodes[i + 1][j]);
    //封路1:將南邊節點的迴路(北邊)方向封掉
    mazeNodes[i+1][j].setWayState(Constant.WAY_NORTH,Constant.WAY_DISABLE);
    //封路2:將當前節點i,j 走過的路封掉 TODO:
    mazeNodes[i][j].setWayState(Constant.WAY_SOUTH,Constant.WAY_DISABLE);
   }
   //西
   else if (mazeNodes[i][j].getWayState(Constant.WAY_WEST)) {
    stack.push(mazeNodes[i][j - 1]);
    mazeNodes[i][j-1].setWayState(Constant.WAY_EAST,Constant.WAY_DISABLE);
    mazeNodes[i][j].setWayState(Constant.WAY_WEST,Constant.WAY_DISABLE);
   }
   //北
   else if (mazeNodes[i][j].getWayState(Constant.WAY_NORTH)) {
    stack.push(mazeNodes[i - 1][j]);
    mazeNodes[i-1][j].setWayState(Constant.WAY_SOUTH,Constant.WAY_DISABLE);
    mazeNodes[i][j].setWayState(Constant.WAY_NORTH,Constant.WAY_DISABLE);
   }
//   if(mazeNodes[i][j].getWayState(Constant.WAY_SOUTH) == Constant.WAY_DISABLE &&
//     mazeNodes[i][j].getWayState(Constant.WAY_EAST) == Constant.WAY_DISABLE &&
//     mazeNodes[i][j].getWayState(Constant.WAY_NORTH) == Constant.WAY_DISABLE &&
//     mazeNodes[i][j].getWayState(Constant.WAY_WEST) == Constant.WAY_DISABLE){
//    stack.pop();
//   }
  }
 }
 public void finish(){
  while (!stack.isEmpty()) {
   MazeNode top = stack.peek();
   top.setValue(2);
   stack.pop();
  }
  System.out.println("迷宮路徑為:");
  int i = 0,j = 0;
  while (i<row){
   for (j = 0; j < col; j++) {
    System.out.print(mazeNodes[i][j].getValue()+" ");
   }
   System.out.println();
   i++;
  }
 }
 public void begin(){
  initValue();
  initWayState();
  goMaze();
  finish();
 }


// public void show(){
//  for(int i=0;i<row;i++){
//   for(int j=0;j<colum;j++){
//    System.out.print(mazeNodes[i][j].value+" ");
//   }
//   System.out.println();
//  }
// }
}

MazeNode:結點類,用於迷宮結點

public class MazeNode {
 private int i;
 private int j;
 private int value;
 private boolean[] wayState;
 public MazeNode(int value,int i,int j){
  wayState = new boolean[Constant.WAYNUM];
  this.value = value;
  this.i = i;
  this.j = j;
 }

 public int getValue() {
  return value;
 }

 public void setWayState(int direction,boolean isAble) {
  wayState[direction] = isAble;
 }

 public boolean getWayState(int direction) {
  return wayState[direction];
 }

 public void setValue(int value){
  this.value = value;
 }

 public int getI() {
  return i;
 }

 public int getJ() {
  return j;
 }
}

Constant:常數類,方便使用

public class Constant {
 public static final int WAYNUM = 4;
 public static final int WAY_EAST = 0;
 public static final int WAY_WEST = 1;
 public static final int WAY_SOUTH = 2;
 public static final int WAY_NORTH = 3;
 public static final boolean WAY_ABLE = true;
 public static final boolean WAY_DISABLE = false;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。