题目

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.

题解

这道题分析看,就是一个词,在一行出现也是true,一列出现也是true,一行往下拐弯也是true,一行往上拐弯也是true,一列往左拐弯也是true,一列往右拐弯也是true。所以是要考虑到所有可能性,基本思路是使用DFS来对一个起点字母上下左右搜索,看是不是含有给定的Word。还要维护一个visited数组,表示从当前这个元素是否已经被访问过了,过了这一轮visited要回false,因为对于下一个元素,当前这个元素也应该是可以被访问的。

代码如下:

 1     public boolean exist(char[][] board, String word) {
 2         int m = board.length;  
 3         int n = board[0].length;  
 4         boolean[][] visited = new boolean[m][n];  
 5         for (int i = 0; i < m; i++) {  
 6             for (int j = 0; j < n; j++) {  
 7                 if (dfs(board, word, 0, i, j, visited))  
 8                     return true;  
 9             }  
         }  
         return false;  
     }
     
     public boolean dfs(char[][] board, String word, int index, int rowindex, int colindex, boolean[][] visited) {  
         if (index == word.length())  
             return true;  
         if (rowindex < 0 || colindex < 0 || rowindex >=board.length || colindex >= board[0].length)  
             return false;  
         if (visited[rowindex][colindex])  
             return false;  
         if (board[rowindex][colindex] != word.charAt(index))  
             return false;  
         visited[rowindex][colindex] = true;  
         boolean res = dfs(board, word, index + 1, rowindex - 1, colindex,  
                 visited)  
                 || dfs(board, word, index + 1, rowindex + 1, colindex, visited)  
                 || dfs(board, word, index + 1, rowindex, colindex + 1, visited)  
                 || dfs(board, word, index + 1, rowindex, colindex - 1, visited);  
         visited[rowindex][colindex] = false;  
         return res;  
             }

Reference:http://blog.csdn.net/yiding_he/article/details/18893621

最新文章

  1. Cordova环境搭建 &amp; HelloWorld
  2. 用java PreparedStatement就不用担心sql注入了吗?
  3. Windows下安装Nginx+php+mysql环境
  4. Latex转换之PDF
  5. android开发系列之代码整洁之道
  6. DB2_001_MQT
  7. Pytorch 报错总结
  8. sqlAlchemy语法增删改查
  9. Mac OSX取消Apache(httpd)开机启动(转载)
  10. 产品经理面试题——浅谈O2O
  11. sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)
  12. MVC 实用构架实战(一)——项目结构搭建
  13. [UE4]Authority,网络控制权
  14. 如何把JavaScript数组中指定的一个元素移动到第一位
  15. C#全屏截图
  16. VB错误说明
  17. 如何隐藏js
  18. java泛型的实现原理
  19. hdu5696区间的价值 -- 2016&quot;百度之星&quot; - 初赛(Astar Round2B)
  20. 一个朋友 js图表开发问题 用 c和 js 解决

热门文章

  1. span 居中
  2. 【莫队算法】【权值分块】bzoj3920 Yuuna的礼物
  3. 吴恩达-coursera-机器学习-week8
  4. Slickflow.NET 开源工作流引擎基础介绍(九) -- .NET Core2.0 版本实现介绍
  5. Azure存储上传下载(断点续传)
  6. javascript区域打印代码
  7. .net core中的高效动态内存管理方案
  8. HDU 4739 Zhuge Liang&#39;s Mines (2013杭州网络赛1002题)
  9. Parallel Programming--perfbook
  10. jQuery $(&#39;div&gt;ul&#39;) $(&#39;div ul&#39;