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
- BFS最短路径问题新手快速入门
- 最短路径问题(BFS)
- BFS 迷宫的最短路径问题
- 迷宫问题之最短路径bfs
- 迷宫最短路径问题-BFS
- BFS广度搜索(最短路径问题)
- 迷宫最短路径问题(BFS)
- bfs最短路径
- 搜索入门(BFS)迷宫的最短路径
- DP入门之最短路径问题
- 迷宫最短路径(bfs)
- 寻找最短路径BFS
- #bfs prime_path 最短路径
- BFS 迷宫最短路径
- hdu3790最短路径问题(BFS+优先队列)
- POJ 3984 迷宫问题【迷宫最短路径 bfs】
- BFS,Dijkstra,Floyd,DP求解最短路径问题
- BFS解迷宫问题,并打印最短路径
- 自旋锁(spinlock) 解释得经典,透彻
- js将时间格式Wed May 25 13:55:10 CST 2016转换为2016-5-25格式再得到7天后的时间
- 保存Session ID 的几种方式
- 文章标题
- 思维导图软件有哪些?为什么选择iMindMap?
- BFS最短路径问题新手快速入门
- VS报错:LNK2038 检测到“_MSC_VER”的不匹配项: 值“1800”不匹配值“1700”
- 华为和魅族真机调试打印不出Log
- 能让你少写1000行代码的20个正则表达式
- IOS管理文件和目录
- 6.OP-TEE+qemu的启动过程分析--加载bios.bin
- Jedis下的ShardedJedis(分布式)使用方法(一)
- UBUNTU中安装qq
- javascript获取div的width、height