java数据结构与算法之走迷宫

深度优先实现了一个走迷宫程序

运行:

原始迷宫:
1    1    1    1    1    1    1    
1    0    0    0    0    0    1    
1    0    0    0    1    0    1    
1    1    1    0    1    1    1    
1    0    0    0    0    0    1    
1    0    0    0    0    0    1    
1    0    0    0    0    0    1    
1    1    1    1    1    1    1    
路径(从终点到原点打印):
[(6,5), (5,5), (4,5), (4,4), (4,3), (3,3), (2,3), (1,3), (1,2), (1,1)]
算法走过的痕迹:
1    1    1    1    1    1    1    
1    2    2    2    2    2    1    
1    0    0    2    1    2    1    
1    1    1    2    1    1    1    
1    0    0    2    2    2    1    
1    0    0    0    0    2    1    
1    0    0    0    0    0    1    
1    1    1    1    1    1    1

代码:

 

package cn.agan.recursive;

import java.util.Objects;
import java.util.Stack;

/**
 * 递归解决迷宫问题:
 *   深度优先走迷宫
 */
public class MiGong {
    public static void main(String[] args) {
        Map map = new Map(8, 7);
        map.setLimit1(); //设置挡板
        System.out.println("原始迷宫:");
        map.show();
        map.setWay(1,1, 6, 5);
       // map.show();
    }

}

//地图类
class Map {
    //二维数组模拟迷宫
    private int[][] map;
    private int row;
    private int col;

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    public Map(int row, int col) {
        this.row = row;
        this.col = col;
        map = new int[row][col];
        //1表示墙,上下都是墙
        for(int i = 0; i < col; i++) {
            map[0][i] = 1;
            map[row-1][i] = 1;
        }
        //左右两边也是墙
        for (int i = 0; i < row; i++) {
            map[i][0] = 1;
            map[i][col-1] = 1;
        }

    }

    /**
     * 查找路线
     * @param startI 起始地址 行
     * @param startJ 起始地址 列
     * @param endI   目的地 行
     * @param endJ   目的地 列
     * @return 返回TRUE说明能够找到路径, 返回false表示不能够找到路径
     */
    public boolean setWay(int startI, int startJ, int endI, int endJ) {
        Map map1 = new Map(this);
        Stack<Point> res = new Stack<>();

        Point start = new Point(startI, startJ);
        Point end = new Point(endI, endJ);
        boolean wayFouned = map1.findWay(res, start, end);
        if (wayFouned) {
            System.out.println("路径(从终点到原点打印):");
            System.out.println(res);
        } else {
            System.out.println("走不到终点");
        }
        System.out.println("算法走过的痕迹:");
        map1.show();
        return true;
    }

    public boolean findWay(Stack<Point> res, Point cur, Point end) {
        if (cur.equals(end)) {
            res.push(cur);
            return true;
        }

        if (pointVal(cur) == 1 || pointVal(cur) == 2 ) {
            return false;
        }

        if ( pointVal(cur) == 0 ) {
            setPointVal(cur, 2);
        }

        for (int i = 0; i < 4; i++) {
            Point next = new Point(cur, i);
            if (findWay(res, next, end)) {
                res.push(cur);
                return true;
            }
        }

        return false;

    }

    public int pointVal(Point p) {
        return map[p.getRow()][p.getCol()];
    }
    public void setPointVal(Point p, int val) {
        map[p.getRow()][p.getCol()] = val;
    }
    //拷贝一份地图
    public  Map(Map m) {
        this.row = m.row;
        this.col = m.col;
        this.map = new int[this.row][this.col];
        for (int i = 0; i < this.row; i++) {
            for (int j = 0; j < this.col; j++ ) {
                this.map[i][j] = m.map[i][j];
            }
        }
    }

    //一种设置挡板的方案
    public void setLimit1() {
        map[3][1] = 1;
        map[3][2] = 1;

        map[3][4] = 1;
        map[3][5] = 1;

        map[2][4] = 1;
    }
    public void show() {
        if (map == null) {
            System.out.println("地图是空的");
            return;
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.printf("%d\t", map[i][j]);
            }
            System.out.println();
        }

    }

}

class Point {
    private int row;
    private int col;

    public Point(int row, int col) {
        this.row = row;
        this.col = col;
    }
    @Override
    public String toString() {
        return "(" + row +
                "," + col +
                ')';
    }

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getCol() {
        return col;
    }

    public void setCol(int col) {
        this.col = col;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Point point = (Point) o;
        return row == point.row &&
                col == point.col;
    }

    public  Point(Point p, int direct) {
        switch (direct) {
            case 0 ://"up":
                row = p.getRow()-1;
                col = p.getCol();
                break;
            case 1: //"left":
                row = p.getRow();
                col = p.getCol()+1;
                break;
            case 2: //"down":
                row = p.getRow()+1;
                col = p.getCol();
                break;
            case 3: //"right":
                row = p.getRow();
                col = p.getCol()-1;
                break;
            default:
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值