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.

解题思路:

使用深度优先搜索的方法,从二维数组中的每个点出发,向上下左右进行延伸,根据判定条件及时判断;

解题步骤:

1、写一个主函数:

  (1)获得二维数组的行列大小;

  (2)遍历所有二维数组元素,对每个元素作为搜索起点,调用递归函数;

2、写一个递归函数,参数为:二维数组(引用)、待匹配的字符串(c字符串形式方便)、当前运动到的坐标值x和y、二维数组大小m和n;

  (1)终止判定条件:x和y无效、当前运动到的字符不是字符串下一个字符;

  (2)如果已经到达匹配字符串末尾,返回true

  (3)暂时将当前运动到的坐标字符置换为'\0',因为要做出标记,以免重复折返运动;

  (4)调用4个递归函数,分别运动到当前运动字符的上下左右四周,并判定;

  (5)如果4个递归函数都false,则换回被置换的'\0',返回false;

代码:

 class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
m = board.size();
n = board[].size();
for(int x = ; x < m; x++) {
for(int y = ; y < n; y++) {
if(isFound(board, word.c_str(), x, y))
return true;
}
}
return false;
} private:
int m;
int n;
bool isFound(vector<vector<char> > &board, const char* w, int x, int y) {
if(x < || y < || x >= m || y >= n || *w != board[x][y])
return false;
if(*(w + ) == '\0')
return true;
char t = board[x][y];
board[x][y] = '\0';
if(isFound(board, w + , x - , y) ||
isFound(board, w + , x + , y) ||
isFound(board, w + , x, y - ) ||
isFound(board, w + , x, y + ))
return true;
board[x][y]=t;
return false;
}
};

【Leetcode】【Medium】word search的更多相关文章

  1. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【leetcode刷题笔记】Word Search

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

  6. 【LeetCode每天一题】Word Search(搜索单词)

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

  7. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  8. 【LeetCode算法-58/66】Length of Last Word/Plus One

    LeetCode第58题: Given a string s consists of upper/lower-case alphabets and empty space characters ' ' ...

  9. 【LeetCode题意分析&解答】33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  10. 【LeetCode每天一题】Length of Last Word(字符串中最后一个单词的长度)

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

随机推荐

  1. LeftoverDataException.

    在用java poi 3.8操作excel的时候,在打开一个已有excel文件时,有时候会报错: org.apache.poi.hssf.record.RecordInputStream$Leftov ...

  2. 关于Function.prototype.bind

    bind()方法会创建一个新函数,称为绑定函数.当调用这个绑定函数时,绑定函数会以创建它时传入bind()方法的第一个参数作为 this,传入 bind() 方法的第二个以及以后的参数加上绑定函数运行 ...

  3. 使用linux的nc来进行文件的传输

    NAME nc - arbitrary TCP and UDP connections and listensSYNOPSIS     nc [-46DdhklnrStUuvz] [-i interv ...

  4. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍

    一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指定导出的数据项,也可以根据指定的条件导 ...

  5. javascript组件化

    http://purplebamboo.github.io/2015/03/16/javascript-component/#%E5%BC%95%E5%85%A5%E4%BA%8B%E4%BB%B6% ...

  6. B-F 字符串匹配算法

    Brute-Froce 算法是串的匹配模式算法中的一种其匹配方式如下: 1.设有字符串 a ,b;a为主串,在 a 中查找 b 串的位置 2.匹配方式如下: 2.1: 分别从 a,b串的第一个元素开始 ...

  7. smarty基本语法

    smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...

  8. 《Cracking the Coding Interview 》之 二叉树的创建 与 遍历(非递归+递归version)

    #include <iostream> #include <cstdio> #include <vector> #include <stack> #de ...

  9. JS打印页面

         打印 整个html页面(PS:样式要写在页面里面才能打印(就是用内部样式))             <a id="dayi" runat="server ...

  10. POJ 1236-Network of Schools (图论-有向图强联通tarjan)

    题目链接:http://poj.org/problem?id=1236 题目大意:N(2<N<100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输.问题 ...