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.

这也算是设置标志位的经典例子了吧。(回溯)

class Solution {
public:
bool isExist(vector<vector<char>>& board,string wordsub,int h,int w, vector<vector<bool>>&flag)
{
int direct[][]={{,},{,-},{,},{-,}};
for(int d=;d<;d++)
{
int jj=h+direct[d][];
int ii=w+direct[d][];
if(jj>=&&jj<board.size()&&ii>=&&ii<board[].size())
{
if(wordsub[]==board[jj][ii]&&flag[jj][ii]==)
{
flag[jj][ii]=;
if(wordsub.size()==||isExist(board,wordsub.substr(),jj,ii,flag))
return true;
flag[jj][ii]=;
} } }
return false;
} bool exist(vector<vector<char>>& board, string word) {
int length=word.size();
int Height=board.size();
int Width=board[].size();
vector<vector<bool>> flag(Height,vector<bool>(Width,));
for(int h=;h<Height;h++)
for(int w=;w<Width;w++)
{
if(board[h][w]==word[])
{
flag[h][w]=;
if( word.size()==||isExist(board,word.substr(),h,w,flag))
return true;
flag[h][w]=;
}
}
return false;
}
};

Word Search——经典题(还没细看)的更多相关文章

  1. Valid Number——分情况讨论最经典的题(没细看)——这题必须静下心来好好看看

    Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...

  2. ResultMap(还没细看)

    前言 MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBatis为我们提供这种理想与现实间转换的手段了,而res ...

  3. U40620 还没想好名字的题

    U40620 niiickの还没想好名字的题 给定一个长度为\(n\)的序列\(a_1,a_2...,a_n\) 要求将这\(n\)个数分为\(m\)组,每组可以有任意多个数,但同一组中的数必须是原序 ...

  4. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  5. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  6. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  7. hdu 1251:统计难题(字典树,经典题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  8. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  9. Leetcode之回溯法专题-79. 单词搜索(Word Search)

    Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...

随机推荐

  1. X day3

    题目 官方题解 T1: 一道水题 #include<iostream> #include<cstring> #include<cstdio> #include< ...

  2. python高效学习路线图

  3. 怎样去面试JavaScript开发者

    面试 Javascript 工程师难吗?Javascript 工程师的水平参差不齐,如何评定他们技术水平的高低?如何确定 Javascript 工程师适合承担哪方面的任务?我在腾讯时的面试经验是,通过 ...

  4. setTimeout()的应用

    错误写法:setTimeout(window.close(),5000); 正确写法:setTimeout(window.close,5000); 或者 setTimeout(function(){ ...

  5. 如何使用Navicat连接Oracle

    1.Navicat连接Oracle,需要使用OCI库.因此先要安装Oracle提供的客户端instantclient-basic, 请注意,32位的Navicat需要下载配置32位的instantcl ...

  6. [mysql]tpcc相关及画图

    参考:http://blog.chinaunix.net/uid-26896862-id-3563600.html 参考:http://blog.chinaunix.net/uid-25266990- ...

  7. hbase的Region分裂代码分析

    region分裂有2种触发情景:1是用户手动触发(参见HRegionServer的splitRegion方法),2是后台flush线程flush完一个region的memstore时,会去检查这个re ...

  8. UVALive-4670 Dominating Patterns / 洛谷 3796 【模板】AC自动机

    https://vjudge.net/problem/UVALive-4670 中文题面:https://www.luogu.org/problem/show?pid=3796 AC自动机模板 注意如 ...

  9. 51Nod 1094 和为k的连续区间 | 水

    Input示例 6 10 1 2 3 4 5 6 Output示例 1 4 #include "cstdio" #include "algorithm" #in ...

  10. (转)matlab练习程序(HOG方向梯度直方图)

    matlab练习程序(HOG方向梯度直方图)http://www.cnblogs.com/tiandsp/archive/2013/05/24/3097503.html HOG(Histogram o ...