前言
上篇有写到DFS算法的大致思路和一个应用案例。本篇主要介绍BFS算法(广度优先搜索)的思路和两个案例。
一、BFS算法的大致思路
BFS算法利用了数据结构中的队列,他的算法思想大体如下:
(1)A为最高层作为队列的队头,将其移出队列,并将相邻元素移入队列。
B C D F作为A的相邻队列进入队列
(1)将B移出队列,如果B有邻接点且未被访问过,将其依次压入队列
(2)将C移出队列,如果C有邻接点且未被访问过,将其依次压入队列
(3)将D移出队列,将其邻接点E压入队列
将D移出队列,将其邻接点H,G依次压入队列
对队列中剩余元素重复相同操作,直至所有元素都被访问过。
由此可见,DFS通俗来讲为“追查到底”,而BFS可以称为“逐层搜索”。
二、两个案例
1.走迷宫
1.1 问题描述
给定一个 n × m n×m n×m 的二维整数数组,用来表示一个迷宫,数组中只包含 0 0 0 或 1 1 1,其中 0 0 0 表示可以走的路, 1 1 1 表示不可通过的墙壁。
最初,有一个人位于左上角 ( 1 , 1 ) (1,1) (1,1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。
请问,该人从左上角移动至右下角 ( n , m ) (n,m) (n,m) 处,至少需要移动多少次。
数据保证 ( 1 , 1 ) (1,1) (1,1) 处和 ( n , m ) (n,m) (n,m) 处的数字为 0,且一定至少存在一条通路。
1.2 实现代码
import java.io.*;
import java.util.*;
class Main {
private static int N = 110;
private static int[][] q = new int[N][N];
private static int[][] d = new int[N][N];
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String[] str1 = bufferedReader.readLine().split(" ");
int n = Integer.parseInt(str1[0]);
int m = Integer.parseInt(str1[1]);
for (int i = 0; i < n; i ++) {
String[] str2 = bufferedReader.readLine().split(" ");
for (int j = 0; j < m; j ++) {
q[i][j] = Integer.parseInt(str2[j]);
}
}
System.out.println(bfs(n, m));
bufferedReader.close();
}
public static int bfs(int n, int m) {
Queue<int[]> queue = new LinkedList<>();
d[0][0] = 0;
///dx 和 dy表示上下左右四个方向
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
queue.offer(new int[]{0, 0});
while (!queue.isEmpty()) {
int[] a = queue.poll();
for (int i = 0; i < 4; i ++) {
int x = a[0] + dx[i];
int y = a[1] + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && q[x][y] == 0 && d[x][y] == 0) {
d[x][y] = d[a[0]][a[1]] + 1;
queue.offer(new int[]{x, y});
}
}
}
return d[n - 1][m - 1];
}
}
2.八数码
2.1 问题描述
在一个 3 × 3 3×3 3×3 的网格中, 1 ∼ 8 1∼8 1∼8 这 8 8 8 个数字和一个 x 恰好不重不漏地分布在这 3 × 3 3×3 3×3 的网格中。
例如:
1 2 3
x 4 6
7 5 8
在游戏过程中,可以把 x 与其上、下、左、右四个方向之一的数字交换(如果存在)。
我们的目的是通过交换,使得网格变为如下排列(称为正确排列):
1 2 3
4 5 6
7 8 x
例如,示例中图形就可以通过让 x 先后与右、下、右三个方向的数字交换成功得到正确排列。
交换过程如下:
1 2 3 1 2 3 1 2 3 1 2 3
x 4 6 4 x 6 4 5 6 4 5 6
7 5 8 7 5 8 7 x 8 7 8 x
现在,给你一个初始网格,请你求出得到正确排列至少需要进行多少次交换。
2.2 实现代码
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String[] str = bufferedReader.readLine().split(" ");
String start = "";
for (int i = 0; i < str.length; i ++) {
start += str[i];
}
String end = "12345678x";
System.out.println(bfs(start, end));
bufferedReader.close();
}
public static void swap(char[] a, int i, int j) {
char tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
public static int bfs(String start, String end) {
Map<String, Integer> map = new HashMap<>();
Queue<String> queue = new LinkedList<>();
queue.offer(start);
map.put(start, 0);
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
while (!queue.isEmpty()) {
String s = queue.poll();
if (s.equals(end)) {
return map.get(s);
}
// k的位置
int k = s.indexOf('x');
int x = k / 3;
int y = k % 3;
for (int i = 0; i < 4; i ++) {
int a = x + dx[i];
int b = y + dy[i];
if (a < 3 && a >= 0 && b < 3 && b >= 0) {
char[] arr = s.toCharArray();
swap(arr, k, a * 3 + b);
String str = new String(arr);
if (map.get(str) == null) {
queue.offer(str);
map.put(str, map.get(s) + 1);
}
}
}
}
return -1;
}
}