深度优先搜索

一、算法介绍

深度优先搜索是一种图的遍历算法,思想是从一个顶点开始,沿着一条路一直走到底,如果发现不能到达目标解,那就返回到上一个节点,然后从另一条路开始走到底,这种尽量往深处走的概念即是深度优先的概念。

二、应用

1.迷宫问题

问题描述:迷宫中的入口到迷宫中某一目标点的最短步数,移动方向只能是相邻的上下左右,而且不能越过障碍物。

解题思路:递归、深度优先搜索

java代码:

 1 public class DFSMaze {
 2 
 3     //迷宫目标点的x坐标
 4     public static int endX = 3;
 5     
 6     //迷宫目标点的y坐标
 7     public static int endY = 2;
 8     
 9     //到达迷宫目标点的最小步数
10     public static int minStep = 9999;
11     
12     //下一步的方向:右、下、左、上
13     public static int[][] direct = {{0,1},{1,0},{0,-1},{-1,0}};
14     
15     //迷宫数组的边界(n行*m列)
16     public static int n = 5;
17     public static int m = 4;
18     
19     //标记位置是否走过,走过标1,没走0
20     public static int[][] book = {
21             {0,0,0,0},
22             {0,0,0,0},
23             {0,0,0,0},
24             {0,0,0,0},
25             {0,0,0,0}};
26     
27     //迷宫数组:0表示通道,1表示障碍物
28     public static int[][] maze = {
29             {0,0,1,0},
30             {0,0,0,0},
31             {0,0,1,0},
32             {0,1,0,0},
33             {0,0,0,1}};
34     
35     public static void main(String[] args){
36         
37         dfs(0,0,0);
38         System.out.println(minStep);
39     }
40     
41     //递归实现,深度优先搜索
42     public static void dfs(int x,int y,int stepNum){
43         
44         //判断当前点是否是目标点
45         if(x==endX && y==endY){
46             if(stepNum < minStep){
47                 minStep = stepNum;
48                 return;
49             }
50         }
51         
52         //枚举4种走法
53         int nextX;
54         int nextY;
55         for(int i=0;i<4;i++){
56             nextX = x+direct[i][0];
57             nextY = y+direct[i][1];
58             //判断是否越界
59             if(nextX<0||nextX>=n||nextY<0||nextY>=m){
60                 continue;
61             }
62             //判断是否障碍物,是否已经走过
63             if(maze[nextX][nextY]==0 && book[nextX][nextY]==0){
64                 book[nextX][nextY] = 1;
65                 dfs(nextX,nextY,stepNum+1);
66                 book[nextX][nextY] = 0;
67             }
68         }
69         return;
70         
71     }
72 }

2.全排列

问题描述:给定一个数字列表,返回其所有可能的排列。(你可以假设没有重复数字。)

样例:给出一个列表[1,2,3],其全排列为:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

java代码:(lintcode)
 1 class Solution {
 2    
 3    int[] copyNums;
 4     /**
 5      * @param nums: A list of integers.
 6      * @return: A list of permutations.
 7      */
 8     public List<List<Integer>> permute(int[] nums) {
 9         // write your code here
10         int numLen = nums.length;
11         int book[] = new int[numLen];
12         copyNums = new int[numLen];
13         for(int i = 0 ; i <numLen ; i++){
14             book[i] = 0;
15         }
16         for(int i = 0 ; i <numLen ; i++){
17             copyNums[i] = nums[i];
18         }
19         
20         List<List<Integer>> result = new ArrayList<>();
21         dfs(0,nums,book,result);
22         return result;
23     }
24     
25     public void dfs(int step,int[] nums,int[] book,List<List<Integer>> result){
26         
27         if(step == nums.length){
28             List<Integer> pl = new ArrayList<>();
29             for(int i=0;i<nums.length;i++){
30                 pl.add(copyNums[i]);
31             }
32             result.add(pl);
33             return;
34         }
35         for(int i=0;i<nums.length;i++){
36             if(book[i]==0){
37                 copyNums[step] = nums[i];
38                 book[i] = 1;
39                 dfs(step+1,nums,book,result);
40                 book[i] = 0;
41             }
42             
43         }
44         return;
45     }
46 }
原文地址:https://www.cnblogs.com/ouym/p/6103028.html