亲宝软件园·资讯

展开

Java寻找迷宫出路 Java项目实现寻找迷宫出路

Sampson_S 人气:0

项目名称

寻找迷宫出路

项目描述

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

代码实现

测试类

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

加载全部内容

相关教程
猜你喜欢
用户评论