广搜(BFS):广搜一般是用于求图的最短路径,比如迷宫中走到某一点的最短距离,或者某个字符串交换达到目标字符串的最少次数,解的个数一般是为单一的,可以把搜索的整个过程想象成一棵树,要求的解就是其中的某一个节点,且距离根节点是最近的。
实现方式:用队列存储每一个子节点,由于队列的存取方式是先进先出,所以很符合广搜的要求,每求出一种情况,将其推入队列中,若当前节点的子节点都已求出,直接将队列的头取出继续求就行。
下面是具体的实现代码:
import java.util.LinkedList; import java.util.Queue; class Main { static int[][] edges = { { 0, 1, 0, 0, 0, 1, 1, 0, 0 }, { 1, 0, 1, 0, 0, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 0, 0, 0, 1 }, { 0, 0, 1, 0, 1, 0, 1, 1, 1 }, { 0, 0, 0, 1, 0, 1, 0, 1, 0 }, { 1, 0, 0, 0, 1, 0, 1, 0, 0 }, { 0, 1, 0, 1, 0, 1, 0, 1, 0 }, { 0, 0, 0, 1, 1, 0, 1, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 0, 0 } }; static String res []={ "A", "B", "C", "D", "E", "F", "G", "H", "I" }; public static void main(String[] args) { bfs(); } static void bfs() { boolean check []=new boolean [9]; Queue<Integer> queue=new LinkedList<Integer>(); for(int i=0;i<9;i++) { if(!check[i]) { queue.add(i); check[i]=true; System.out.print(res[i]+" "); } while(!queue.isEmpty()) { int k=queue.poll(); for(int j=0;j<9;j++) { if(!check[j] && edges[k][j]==1) { queue.add(j); check[j]=true; System.out.print(res[j]+" "); } } } } } }
深搜(DFS):深搜一般用于整个图的遍历,它一般用于求多个解的情况,例如八皇后问题就是一个很典型的深搜问题,它要求的是所有符合要求的棋子摆法,也就是要将整个棋盘都遍历一遍。
实现方式:递归和非递归(用栈实现)。栈的存取方式为先进后出,即每次选择栈顶元素往下搜索。
下面是具体代码:
import java.util.Stack; class Main { static int[][] edges = { { 0, 1, 0, 0, 0, 1, 1, 0, 0 }, { 1, 0, 1, 0, 0, 0, 1, 0, 1 }, { 0, 1, 0, 1, 0, 0, 0, 0, 1 }, { 0, 0, 1, 0, 1, 0, 1, 1, 1 }, { 0, 0, 0, 1, 0, 1, 0, 1, 0 }, { 1, 0, 0, 0, 1, 0, 1, 0, 0 }, { 0, 1, 0, 1, 0, 1, 0, 1, 0 }, { 0, 0, 0, 1, 1, 0, 1, 0, 0 }, { 0, 1, 1, 1, 0, 0, 0, 0, 0 } }; static String res []={ "A", "B", "C", "D", "E", "F", "G", "H", "I" }; static boolean check []=new boolean [9]; public static void main(String[] args) { dfs_stack(); dfs_back(); } static void dfs_stack() {//非递归实现 Stack<Integer> stack=new Stack<>(); boolean check []=new boolean [9]; for(int i=0;i<9;i++) { if(!check[i]) { stack.push(i); check[i]=true; System.out.print(res[i]+" "); } while(!stack.isEmpty()) { int k=stack.pop(); for(int j=0;j<9;j++) { if(!check[j] && edges[k][j]==1) { stack.push(j); check[j]=true; System.out.print(res[j]+" "); break;//这里是重点,必须跳出当前循环,以取当前节点的下一个节点 } } } } System.out.println(); } static void dfs_back() {//递归实现 for(int i=0;i<9;i++) { if(!check[i]) {//check判断是否已经取过,静态变量 dfs(i); } } } static void dfs(int i) { check[i]=true; System.out.print(res[i]+" "); for(int j=0;j<9;j++) { if(!check[j] && edges[i][j]==1) { dfs(j); } } } }