dfs递归过程代码优化
2015-10-05 12:12
381 查看
在一个图或者矩阵中,一个点往往有很多邻接点,在dfs搜索的过程中,如果有一个邻接点搜索成功,剩下的邻接定点就不需要搜索了。
对矩阵中的DFS来说,还需要处理边界问题,保持搜素的点在矩阵内部,不会出现访问越界的情况,对此优化有两点:
1、在搜索多个邻接定点时,使用“||或”运算,有个搜索成功即返回成功。
2、将边界检查放在DFS函数的头部,如果越界,返回搜索失败,不需要进行递归。
以leetcode题目wordsearch为例,原题为:
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
word =
word =
word =
思路:
深度遍历,设置visit数组来标记已经访问过的位置,在访问失败回溯时,重新将访问过的位置设置未访问,访问失败了,就当没放问
代码如下:
对矩阵中的DFS来说,还需要处理边界问题,保持搜素的点在矩阵内部,不会出现访问越界的情况,对此优化有两点:
1、在搜索多个邻接定点时,使用“||或”运算,有个搜索成功即返回成功。
2、将边界检查放在DFS函数的头部,如果越界,返回搜索失败,不需要进行递归。
以leetcode题目wordsearch为例,原题为:
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[ ["ABCE"], ["SFCS"], ["ADEE"] ]
word =
"ABCCED", -> returns
true,
word =
"SEE", -> returns
true,
word =
"ABCB", -> returns
false.
思路:
深度遍历,设置visit数组来标记已经访问过的位置,在访问失败回溯时,重新将访问过的位置设置未访问,访问失败了,就当没放问
代码如下:
public class Solution { //k表示要找的word中的第k个字符,从i,j的周边开始找,i,j是第k个字符 public boolean dfs(char[][] board,String word,int i,int j,int k,boolean[][] visit){ if(k==word.length()){ //成功搜索 return true; } /**优化1*/ if(i<0 || i>=board.length || j<0 || j>=board[0].length //越界, 搜索失败 || board[i][j] != word.charAt(k) //第i,j个字符不等于word的第k个字符, 搜索失败 || visit[i][j]){ //第i,j个字符已经被访问过了, 搜索失败 return false; } visit[i][j] = true; //将当前节点记为已经访问 /**优化2*/ if(dfs(board,word,i-1,j,k+1,visit) //从上下左右四个方向遍历,只要有一个成功便返回 || dfs(board,word,i+1,j,k+1,visit) || dfs(board,word,i,j-1,k+1,visit) || dfs(board,word,i,j+1,k+1,visit)){ return true; } visit[i][j] = false; //这个很重要,在回溯时,把访问标记记为false return false; } public boolean exist(char[][] board, String word) { if(board.length==0 || board[0].length==0 || word.length()==0){ return false; } int row = board.length; int col = board[0].length; boolean[][] visit = new boolean[row][col]; //访问标记数组 for(int i=0;i<row;i++){ for(int j=0;j<col;j++){ if(board[i][j]==word.charAt(0) && dfs(board,word,i,j,0,visit)){ return true; } } } return false; } public static void main(String[] args) { Solution s = new Solution(); char[][] a = {{'A','B','C','E'}, {'S','F','C','S'}, {'A','D','E','E'}}; System.out.println(s.exist(a, "ABCCED")); System.out.println(s.exist(a, "SEE")); System.out.println(s.exist(a, "ABCB")); } }
相关文章推荐
- java Exception RuntimeException
- C# 中栈和队的区别
- android eclipse ndk使用记录
- Spring @Cacheable 的key生成
- C#--静态构造函数
- NumPy常见函数和使用示例
- kmp算法实现(C++版)
- 详细记录python的range()函数用法
- Python面向对象编程 - 一个记事本程序范例(二)
- C/C++中的const--常量指针与指针常量
- C#(ASP.Net)获取当前路径的方法集合
- C/C++:用数组构造队列。
- 1.Python补充_序列的方法
- ASP.NET页面内传递参数方法
- GOF 23 设计模式之 状态模式
- Eclipse--Team--SVN--URL修改
- C#--readlyonly关键字
- 学习工具篇 - eclipse 中使用autotools plugins
- java基础——break灵活应用
- python中return与pass的区别