迷宫问题2-在给定的迷宫中计算从起点到终点的路径数目

问题描述

给定一个迷宫,也即一个矩阵,矩阵中由数字0和1 组成,其中,0表示当前位置可以通行,而1表示当前位置是障碍物,如迷宫中的砖墙,无法通行,再给定一个起点和一个目标点,请计算从起点到目标点可行的路径条数。(注:起点和终点一定可通行,即对一个的位置都是0)

解法

这里使用深度优先搜索+回溯的方法进行可行路径的判断

输入输出及状态说明

0–可通行
1–障碍物

迷宫矩阵5×5
{
{0,1,0,0,1},
{0,0,1,1,1},
{1,0,0,0,0},
{1,0,1,1,0},
{1,0,0,0,0},
}

起点 (0,0)
终点(4,4)

输出
起点–>终点的输出路径的条数
如果条数大于0,则输出每一条路径的行进过程

算法描述

import java.util.LinkedList;

/** * Created by ChaoNi on 2016/9/19. */

public class DFS {
    //输入的迷宫矩阵
    private static int[][] matrix= {
            {0,1,0,0,1},
            {0,0,1,1,1},
            {1,0,0,0,0},
            {1,0,1,1,0},
            {1,0,0,0,0},
    };

    //迷宫的宽度
    private static  int width=5;
    //迷宫的高度
    private static int heigth=5;

    //标记迷宫中各个位置是否已经被走过
    private static boolean[][] flag=null;
    //在迷宫中的移动方向
    private static int[][] direction={
            {0,1},
            {0,-1},
            {-1,0},
            {1,0}
    };

    //存在的路径条数
    private static int count=0;

    public static void main(String[] args) {
        //起始点
        Point start=new Point(0,0);
        //目标点
        Point end=new Point(width-1,heigth-1);

        flag=new boolean[width][heigth];
        LinkedList<Point> linkedList=new LinkedList<>();
        linkedList.add(start);
        countPath(start,end,linkedList);
        System.out.println("exist "+count+" pathes"
        );


    }

    //找到有多少条路径
    public static void countPath(Point start,Point end,LinkedList<Point> linkedList){
        //到达目标点,表明找到一条路径
        if(start.getX()==end.getX()&&start.getY()==end.getY()){
            count++;
            //输出路径
            for(Point p:linkedList){
                System.out.print(p+"->");
            }
            System.out.println();
            return;
        }
        //从四个方向进行再次遍历
        for(int i=0;i<4;i++){
            int nx=start.getX()+direction[i][0];
            int ny=start.getY()+direction[i][1];

            //检验方向的合法性
            if(nx>=0&&nx<width&&ny>=0&&ny<heigth&&flag[nx][ny]==false&&matrix[nx][ny]==0){
                Point next=new Point(nx,ny);
                flag[nx][ny]=true;
                linkedList.add(next);
                countPath(next,end,linkedList);
                //当前这个方向不能完成,则回退到前一个结果,进行再次探索
                flag[nx][ny]=false;
                linkedList.removeLast();
            }
        }
    }
}


//位置点的辅助类
class Point {
    private int x;
    private int y;
    public Point(int xx, int yy){
        this.x=xx;
        this.y=yy;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    @Override
    public String toString() {
        return "[" + x +
                ", "+y+"]";
    }
}

结果

在以上给定的矩阵中,输入的路径条数是2,对应的行走路径如下:
[0, 0]->[1, 0]->[1, 1]->[2, 1]->[2, 2]->[2, 3]->[2, 4]->[3, 4]->[4, 4]->[4, 4]
[0, 0]->[1, 0]->[1, 1]->[2, 1]->[3, 1]->[4, 1]->[4, 2]->[4, 3]->[4, 4]->[4, 4]

exist 2 pathes

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/jack_nichao/article/details/52583887
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞