https://oj.leetcode.com/problems/word-search/

类似于在棋盘上走步,走过的地方不能再走,每次都可以走当前位置的上、下、左、右,问能不能走出要求的形状来。

深搜:

依次搜它的上

在深搜中,容易超时,所以如果有复杂类型的数据传值,一般都用引用。当然,为了恢复每次引用的现场,需要把本次深搜中改变的值,再改回来。

  1. class Solution {
  2. public:
  3.  
  4. bool exist(vector<vector<char> > &board, string word) {
  5. if(board.size() == || word.size() == )
  6. return false;
  7.  
  8. int row = board.size();
  9. int col = board[].size();
  10. vector<vector<bool> > flag;
  11. //initialize
  12. flag.resize(row);
  13. for(int i = ; i < row; i++)
  14. {
  15. flag[i].resize(col);
  16. for(int j = ; j < col; j++)
  17. flag[i][j] = false;
  18. }
  19.  
  20. bool ans = false;
  21. for(int i = ; i < row; i++)
  22. {
  23. for(int j = ; j < col; j++)
  24. {
  25. if(board[i][j] == word[])
  26. {
  27. ans = find(board,word,i,j,flag,);
  28. if(ans)
  29. return true;
  30. }
  31. }
  32. }
  33. return false;
  34. }
  35.  
  36. bool find(vector<vector<char> > &board, string &word, int i, int j,vector<vector<bool> > &flag, int match_index)
  37. {
  38. if(match_index == word.size())
  39. return true;
  40.  
  41. //true means used
  42. flag[i][j] = true;
  43.  
  44. bool ans;
  45. //up
  46. if(i!= && board[i-][j] == word[match_index] && flag[i-][j] == false)
  47. {
  48. ans = find(board,word,i-,j,flag,match_index + );
  49. if(ans)
  50. return true;
  51. }
  52.  
  53. //right
  54. if(j!= board[].size() - && board[i][j+] == word[match_index] && flag[i][j+] == false)
  55. {
  56. ans = find(board,word,i,j+,flag,match_index + );
  57. if(ans)
  58. return true;
  59. }
  60.  
  61. //down
  62. if(i!= board.size() - && board[i+][j] == word[match_index] && flag[i+][j] == false)
  63. {
  64. ans = find(board,word,i+,j,flag,match_index + );
  65. if(ans)
  66. return true;
  67. }
  68. //left
  69. if(j!= && board[i][j-] == word[match_index] && flag[i][j-] == false)
  70. {
  71. ans = find(board,word,i,j-,flag,match_index + );
  72. if(ans)
  73. return true;
  74. }
  75. flag[i][j] = false;
  76. return false;
  77. }
  78. };

LeetCode OJ--Word Search **的更多相关文章

  1. [LeetCode OJ] Word Search 深度优先搜索DFS

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  2. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  3. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  4. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. [LeetCode] 212. Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  7. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  8. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  9. LeetCode OJ——Word Ladder

    http://oj.leetcode.com/problems/word-ladder/ 图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜. 深搜版本: 第一次写的时候,把sum和visi ...

  10. [LeetCode#212]Word Search II

    Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...

随机推荐

  1. Python装饰器使用规范案例详解

    由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25') ... >> ...

  2. Green Space【绿色空间】

    Green Space Living in an urban area with green spaces has a long-lasting positive impact on people's ...

  3. The 2018 ACM-ICPC Asia Qingdao Regional Contest(青岛网络赛)

    A Live Love 水 #include <algorithm> #include<cstdio> #include<cstring> using namesp ...

  4. HDU 6153 KMP

    最终刷KMP目标就是为了挑战这道题!现在成功了恩... 首先,题目大意是:给出一个字符串str1,之后给出另一个字符串str2,问,str2的后缀在str1匹配的次数*后缀当前长度是多少 首先考虑正统 ...

  5. 扩展程序 - Google Chrome

    Adblock Plus 3.0.3 Adblock Plus 已被超过 1 亿台设备使用,是世界上最受欢迎的广告拦截软件. ID:cfhdojbkjhnklbpkdaibdccddilifddb 查 ...

  6. 手机注册过哪些网站37kfenxi.com,查询注册过哪些网站

    注册过哪些网站?发现这么一个网站,https://www.37kfenxi.com?_=cnblogs 可以根据手机号码查询注册过哪些网站,然后通过大数据分析出机主的性格,爱好等. 据说还可以查老板, ...

  7. 《鸟哥的Linux私房菜》学习笔记(5)——权限管理

    一.权限的基本概念                                                   权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...

  8. service-worker实践

    service-worker虽然已列入标准,但是支持的浏览器还是有限制,还有比较多的问题. 1. 生命周期 注册成功-------installing--------------> 安装成功(i ...

  9. 设计模式之第17章-备忘录模式(Java实现)

    设计模式之第17章-备忘录模式(Java实现) 好男人就是我,我就是曾小贤.最近陈赫和张子萱事件闹得那是一个沸沸扬扬.想想曾经每年都有爱情公寓陪伴的我现如今过年没有了爱情公寓总是感觉缺少点什么.不知道 ...

  10. cookie注意事项

    cookie是在 HTTP 协议下,服务器或脚本可以维护客户工作站上信息的一种方式. 四种会话跟踪技术(URL重写.隐藏表单域.Cookie.Session) 服务端技术:URL重写,Session, ...