BFS最短路径问题新手快速入门

来源:互联网 发布:双轨制软件开发公司 编辑:程序博客网 时间:2024/04/29 04:33

我们遇到的迷宫问题中,有很大一部分可以用BFS来解。解决这类问题可以很大地提升你的能力与技巧,我会试着帮助你理解如何使用BFS来解题。这篇文章是基于一个简单实例展开来讲的


例题:
第一行两个整数n, m,为迷宫的长宽。
接下来n行,每行m个数为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
输出格式
  第一行一个数为需要的最少步数K。
  第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
 样例输入
Input Sample 1:
3 3
0 0 1
1 0 0
1 1 0

Input Sample 2:
3 3
0 0 0
0 0 0
0 0 0
样例输出
Output Sample 1:
4
RDRD

Output Sample 2:
4
DDRR


BFS,属于一种盲目搜寻法,目的是系统地展开并检查图中的所有节点,以找寻结果。换句话说,它并不考虑结果的可能位置,彻底地搜索整张图,直到找到结果为止。
伪代码

queue.add(起点);while(队列不为空){    取出队首点    if(如果为终点)        结束    //不为终点,继续向下走    for(方向)        ...}

下面贴上例题的代码


public class test3 {    static int[][] one = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };//上下左右移动坐标的变化    static String[] nextpath = { "U", "D", "L", "R" };//上下左右移动的表示    static class point {//点类记录当前坐标,步数,路径        int x, y, step;//step表示从出发到当前点经过几步        String path;//path表示从出发到当前点经过路径        public point(int x, int y, int step, String path) {            this.x = x;            this.y = y;            this.step = step;            this.path = path;        }    }    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        int n = in.nextInt();        int m = in.nextInt();        int[][] a = new int[n][m];        for (int i = 0; i < n; i++) {            for (int j = 0; j < m; j++) {                a[i][j] = in.nextInt();            }        }        bfs(a, n, m);    }    //按字典序较小选择路径    public static boolean judge(String A, String B) {        char[] arrayA = A.toCharArray();        char[] arrayB = B.toCharArray();        for (int i = 0, len = A.length(); i < len; i++) {            if (arrayA[i] < arrayB[i])                return false;        }        return true;    }    //判断点是否出界或被访问过    public static boolean check(int[][] matrix, point a) {        int n = matrix.length - 1, m = matrix[0].length - 1;        if (a.x < 0 || a.x > n || a.y < 0 || a.y > m || matrix[a.x][a.y] == 1)            return false;        return true;    }    //搜索    static void bfs(int[][] a, int n, int m) {        ArrayList<point> list = new ArrayList<point>();        list.add(new point(0, 0, 0, ""));//向队列中加入第一个点        int minStep = Integer.MAX_VALUE;//最小步数        String minPath = "";//最短路径        while (list.size() != 0) {            point b = list.get(0);//当队列中有点时,取出点比较是否为终点            list.remove(0);//删除该点            if (b.x == n - 1 && b.y == m - 1) {                if (minStep > b.step) {                    minStep = b.step;                    minPath = b.path;                } else if (minStep == b.step) {                    if (judge(minPath, b.path)) {                        minPath = b.path;                    }                }                continue;            }            //如果不是终点,依次尝试访问上下左右,并加入队列继续循环            for (int i = 0; i < 4; i++) {                int x = b.x + one[i][0];                int y = b.y + one[i][1];                int step = b.step + 1;                String path = b.path + nextpath[i];                point p = new point(x, y, step, path);                if (check(a, p)) {                    list.add(p);                    a[x][y] = 1;                }            }        }        System.out.println(minPath + "\n" + minStep);//循环结束输出最短步数及路径        return;    }}

运行结果

3 30 1 00 1 00 0 0DDRR4

如果无通路则会输出Integer.Max_Value

0 0
原创粉丝点击