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) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
随机推荐
- X day2
题目 官方题解 T1: 我们可以把问题化简为$a\times b \times c \leq n $中的有序$(a,b,c)$有多少组.分三种情况考虑 当$a=b=c$时,答案十分好统计 当$a< ...
- windows内存映射文件
http://shushanyegui.duapp.com/?p=731 在描述内存映射文件之前 我们先来写一个系统通过I/O来读写磁盘文件的小程序 #include "stdafx.h&q ...
- ios轮播
// // ViewController.m // Ocproject // // Created by wenzhe yi on 2018/2/28. // Copyright © 2018年 we ...
- How Many Nines ZOJ - 3950 打表大法好
If we represent a date in the format YYYY-MM-DD (for example, 2017-04-09), do you know how many 9s w ...
- 清除windows系统垃圾文件简易脚本(bat)
@echo off echo 正在清除系统垃圾文件,请稍等...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %sy ...
- [技巧篇]11.JavaScript原生态如何获取浏览器请求地址中的参数
var getAccessParams = function(){ var i,ilen,strs,keyName,keyValue, params={}, path = window.locatio ...
- Mybatis(3) 映射文件-增删改查
映射文件: 映射文件是根据数据库模型生成的编写sql脚本xml文件, mapper标签中namespace属性值为对应模型实体类的全类名. <?xml version="1.0&quo ...
- 【C++ STL】Deques
1.结构 容器deque和vector非常相似,也是采用动态数组来管理元素,提供随机存取,有着和vector几乎一样的接口,不同的是deque的动态数组头尾都开放,因此可以在头尾都可以进行快速的安插和 ...
- bzoj2516 电梯
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2516 [题解] 状压dp. $f_{sta,i}$表示状态为sta,当前在第i层的最小花费时 ...
- Zabbix 通过 JMX 监控 java 进程
参考: [ JMX monitoring ] [ Zabbix Java gateway ] [ JMX Monitoring (Java Gateway) not Working ] [ Monit ...