Word Search——经典题(还没细看)
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——经典题(还没细看)的更多相关文章
- Valid Number——分情况讨论最经典的题(没细看)——这题必须静下心来好好看看
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
- ResultMap(还没细看)
前言 MyBatis是基于“数据库结构不可控”的思想建立的,也就是我们希望数据库遵循第三范式或BCNF,但实际事与愿违,那么结果集映射就是MyBatis为我们提供这种理想与现实间转换的手段了,而res ...
- U40620 还没想好名字的题
U40620 niiickの还没想好名字的题 给定一个长度为\(n\)的序列\(a_1,a_2...,a_n\) 要求将这\(n\)个数分为\(m\)组,每组可以有任意多个数,但同一组中的数必须是原序 ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- hdu 1251:统计难题(字典树,经典题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- Leetcode之回溯法专题-79. 单词搜索(Word Search)
Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
随机推荐
- bzoj1867: [Noi1999]钉子和小球(DP)
一眼题...输出分数格式才是这题的难点QAQ 学习了分数结构体... #include<iostream> #include<cstring> #include<cstd ...
- boost文件锁的使用
boost中可以用boost::interprocess::file_lock类对文件进行加锁和解锁操作. #include <fstream> #include <iostream ...
- HDU1394 逆序数
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 深入探析 Rational AppScan Standard Edition 多步骤操作
序言 IBM Rational AppScan Standard(下文简称 AppScan)作为面向 Web 应用安全黑盒检测的自动化工具,得到业界的广泛认可和应用.很多人使用 AppScan 时都采 ...
- echarts.js中的图表大小自适应
echarts的图表,如果父级容器的height/width属性设置为百分比的形式,那么echarts就会warning,且不能正常的生成图表.所以div容器的高度宽度必须指定为px,这设计不知道是为 ...
- [Leetcode] LRU 算法实现
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- centos设置tomcat开机启动
1.编辑开机启动脚本 vi /etc/init.d/tomcat8 #!/bin/bash # tomcat8:start|stop|restart # chkconfig: 345 90 10 # ...
- 【BZOJ4517】【SDOI2016】排列计数 [数论]
排列计数 Time Limit: 60 Sec Memory Limit: 128 MB[Submit][Status][Discuss] Description 求有多少种长度为 n 的序列 A, ...
- bzoj 3453 数论
首先我们知道对于f(x)来说,它是一个k次的多项式,那么f(x)的通项公式可以表示成一个k+1次的式子,且因为f(x)没有常数项,所以我们设这个式子为 f(x)=Σ(a[i]*x^i) (1<= ...
- Linux命令--hostname和uname
hostname命令 hostname命令用于显示和设置系统的主机名称.环境变量HOSTNAME也保存了当前的主机名.在使用hostname命令设置主机名后,系统并不会永久保存新的主机名,重新启动机器 ...