Java项目实现寻找迷宫出路

本文实例为大家分享了Java实现寻找迷宫出路的具体代码,供大家参考,具体内容如下

项目名称

寻找迷宫出路

项目描述

给定一个自定义迷宫,0表示能通过,1表示不能通过。通过程序找出正确的迷宫出路,并将正确的路线改为2输出。

Java项目实现寻找迷宫出路_第1张图片

代码实现

测试类

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 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;i0 && mazeNodes[i][j-1].getValue() == 0){
      mazeNodes[i][j].setWayState(Constant.WAY_WEST,Constant.WAY_ABLE);
     }
     //南
     if(i+10 && 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 
 

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Java项目实现寻找迷宫出路)