迷宫问题(java实现)

1、

public class Direction {
    private int x;
    private int y;
    public Direction(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
}

2、

public class Position {
    private int x;
    private int y;
    private int d;
    public int getX() {
        return this.x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return this.y;
    }
    public void setY(int y) {
        this.y = y;
    }
    public Position(int x, int y, int d) {
        this.x = x;
        this.y = y;
        this.d = d;
    }
    public int getD() {
        return this.d;
    }
    public void setD(int d) {
        this.d = d;
    }
}

3、

import java.util.Iterator;
import java.util.Stack;
public class MazeProblem {
    public static Stack<Position> path(int[][] maze, Direction[] move) {
        Stack<Position> s = new Stack<Position>();
        // 起点位置 还未开始探索所以无方向
        Position start_p = new Position(1, 1, -1);
        s.push(start_p);
        maze[1][1] = -1; // 起点位置表示已经走过,不可以往回探索,pop的时候 可以退回
        while (!s.empty()) {
            Position temp = s.pop(); // 取出当前的位置 准备进行向下探索
            // 确定探索的位置方向
            int x = temp.getX(); // 当前处在正在探索的位置和方向
            int y = temp.getY();
            int d = temp.getD() + 1;// 探索的方向
            while (d < 8) { // 开始探索 一共八个方向
                int i = x + move[d].getX(); // 根据某个方向探的下一个位置
                int j = y + move[d].getY();
                if (maze[i][j] == 0) { // 如果下一个位置是可以进去的,则放入当前位置
                    s.push(new Position(x, y, d));
                    x = i; // 把当前探索位置 调整为放入的位置
                    y = j;
                    d = 0; // 调整方向为0,为下次探索做准备
                    maze[x][y] = -1; // 然后设置为已走过
                    if (x == Destination.m && y == Destination.n) { // 在判断是不是已经是终点位置 如果是 则程序退出
                        s.push(new Position(x, y, d));
                        return s;
                    }
                } else {
                    d++;  //// 如果下一个位置是不可以进去的,则放入调整方向
                }
            }
        }
        return new Stack<Position>();
    }
    //终点位置
    static class Destination {
        static int m = 6;
        static int n = 8;
    }
    public static void main(String[] arg) {
        // 0表示可进入 1 表示 不可以进去 -1 表示走过的路劲也不可进入
        // 每个位置 都有八个方向
        int[][] maze = {
            //    0  1  2  3  4  5  6  7  8  9
                { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, // 0
                { 1, 0, 1, 1, 1, 0, 1, 1, 1, 1 }, // 1
                { 1, 1, 0, 1, 0, 1, 1, 1, 1, 1 }, // 2
                { 1, 0, 1, 0, 0, 0, 0, 0, 0, 1 }, // 3
                { 1, 0, 1, 1, 1, 1, 1, 0, 1, 1 }, // 4
                { 1, 1, 0, 0, 1, 1, 1, 1, 0, 1 }, // 5
                { 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 }, // 6
                { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }  // 7
        };
        Direction[] move = 
        {
            new Direction(0,  1),
            new Direction(1,  1), 
            new Direction(1,  0),
            new Direction(1, -1),
            new Direction(0, -1),
            new Direction(-1,-1), 
            new Direction(-1, 0),
            new Direction(-1, 1)
        };
        Stack<Position> s = MazeProblem.path(maze, move);
        Iterator<Position> it = s.iterator();
        while (it.hasNext()) {
            Position e = it.next();
            System.out.println(e.getX() + ",-->" + e.getY());
        }
    }
}
原文地址:https://www.cnblogs.com/kongxianghao/p/7754607.html