算法复习 - 迷宫问题

5 篇文章 0 订阅
  • MazePuzzle.java
package algorithm;

import java.util.Stack;

/**
* Created by pokerface_lx on 16/6/19.
*/
public class MazePuzzle {

   private static Stack<Position> openStack;
   private static Stack<Position> closeStack;

   /**
    * 1表示此处有路,2表示是能走到此处但是不是解,3表示此路径是解
    * @param maze
    */
   public static void findPath (int[][] maze) {
       openStack = new Stack<>();
       closeStack = new Stack<>();
       openStack.push(new Position(0, 0));
       while (openStack.size()!=0) {
           Position currentPos = openStack.pop();
           int x = currentPos.getxPosition();
           int y = currentPos.getyPosition();
           closeStack.push(currentPos);
           maze[x][y]=2;
           if ((x == maze.length-1) && (y == maze[x].length-1)) {
               break;
           }
           boolean isDeadEnd = true;
           if (isValid(maze, x-1, y) && maze[x-1][y] == 1) {
               openStack.push(new Position(x-1, y));
               isDeadEnd = false;
           }
           if (isValid(maze, x+1, y) && maze[x+1][y] ==1) {
               openStack.push(new Position(x+1, y));
               isDeadEnd = false;
           }
           if (isValid(maze, x, y-1) && maze[x][y-1] == 1) {
               openStack.push(new Position(x, y-1));
               isDeadEnd = false;
           }
           if (isValid(maze, x, y+1) && maze[x][y+1]==1) {
               openStack.push(new Position(x, y+1));
               isDeadEnd = false;
           }
           if (isDeadEnd) {
               closeStack.pop();
           }
       }
       while (!closeStack.isEmpty()) {
           Position position = closeStack.pop();
           int x = position.getxPosition();
           int y = position.getyPosition();
           maze[x][y] = 3;
       }
       showMaze(maze);
   }

   private static boolean isValid(int[][] maze, int x, int y) {
       if (x >= 0 && y >= 0 && x < maze.length && y < maze[x].length) {
           return true;
       } else {
           return false;
       }
   }

   private static void showMaze (int[][] maze) {
       for (int[] a: maze) {
           for (int b: a) {
               System.out.print(b+" ");
           }
           System.out.println();
       }
   }

   private static class Position {
       private Integer xPosition;
       private Integer yPosition;
       public Position(int xPosition, int yPosition) {
           this.xPosition = xPosition;
           this.yPosition = yPosition;
       }

       public Integer getxPosition() {
           return xPosition;
       }

       public Integer getyPosition() {
           return yPosition;
       }
   }

}
  • MazeFactory.java
package algorithm;

/**
* Created by pokerface_lx on 16/6/19.
*/
public class MazeFactory {

   private static int[][] maze;

   public MazeFactory() {

   }

   public static int[][] getDefaultMaze() {
       maze = new int[6][6];
       maze[0] = new int[]{1, 1, 0, 1, 1, 0};
       maze[1] = new int[]{0, 1, 1, 0, 0, 1};
       maze[2] = new int[]{0, 1, 0, 1, 1, 1};
       maze[3] = new int[]{1, 1, 0, 1, 0, 1};
       maze[4] = new int[]{0, 1, 1, 1, 0, 1};
       maze[5] = new int[]{1, 1, 0, 0, 1, 1};
       return maze;
   }

   public static int[][] getMaze(int length, int width) {
       maze = new int[length][width];
       for (int i = 0; i < length; i++) {
           for (int j = 0; j < width; j++) {
               if (Math.random() < 0.5) {
                   maze[i][j] = 0;
               } else {
                   maze[i][j] = 1;
               }
           }
       }
       return maze;
   }

}

- #### HelloMaze.java
```java
package algorithm;

/**
* Created by pokerface_lx on 16/6/19.
*/
public class HelloMaze {

   public static void main(String[] args) {
       MazePuzzle puzzle = new MazePuzzle();
       puzzle.findPath(MazeFactory.getMaze(10,10));
   }

}

结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值