【Word Search】cpp
题目:
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 exist(vector<vector<char> >& board, string word)
{
vector<vector<bool> > visited;
for ( size_t i = ; i < board.size(); ++i )
{
vector<bool> tmp(board[i].size(),false);
visited.push_back(tmp);
}
for ( int i = ; i < board.size(); ++i )
{
for ( int j = ; j < board[i].size(); ++j )
{
if (Solution::dfs(board, visited, i, j, word, )) return true;
}
}
return Solution::dfs(board, visited, , , word, );
}
static bool dfs(
vector<vector<char> >& board,
vector<vector<bool> >& visited,
int i,
int j,
string& word,
int curr )
{
if ( curr==word.size() ) return true;
if ( i< || i==board.size() || j< || j==board[i].size() ) return false;
if ( visited[i][j] ) return false;
if ( board[i][j]!=word[curr] ) return false;
if ( board[i][j]==word[curr] )
{
visited[i][j] = true;
if ( Solution::dfs(board, visited, i, j+, word, curr+)) return true;
if ( Solution::dfs(board, visited, i+, j, word, curr+)) return true;
if ( Solution::dfs(board, visited, i, j-, word, curr+)) return true;
if ( Solution::dfs(board, visited, i-, j, word, curr+)) return true;
}
visited[i][j] = false;
return false;
}
};
tips:
好好领悟一下dfs吧。。。
1. 这道题在主程序中有一个循环,如果一旦发现word的起点,就从这个位置开始dfs,看能否返回结果。
2. dfs的过程跟模板一样。
shit
============================================
第二次过这道题,逻辑清晰了很多,修正了两个笔误bug,AC了。
class Solution {
public:
bool exist(vector<vector<char> >& board, string word)
{
for ( int i=; i<board.size(); ++i )
{
for ( int j=; j<board[i].size(); ++j )
{
if ( board[i][j]==word[] )
{
board[i][j] = '#';
if ( Solution::dfs(board, i, j, word.substr(,word.size()-)) ) return true;
board[i][j] = word[];
}
}
}
return false;
}
static bool dfs(
vector<vector<char> >& board,
int i, int j,
string word)
{
if ( word.size()== ) return true;
// left
if ( j->= && board[i][j-]==word[] )
{
board[i][j-] = '#';
if (Solution::dfs(board, i, j-, word.substr(,word.size()-)) ) return true;
board[i][j-] = word[];
}
// right
if ( j+<board[].size() && board[i][j+]==word[] )
{
board[i][j+] = '#';
if (Solution::dfs(board, i, j+, word.substr(,word.size()-)) ) return true;
board[i][j+] = word[];
}
// up
if ( i->= && board[i-][j]==word[] )
{
board[i-][j] = '#';
if (Solution::dfs(board, i-, j, word.substr(,word.size()-)) ) return true;
board[i-][j] = word[];
}
// down
if ( i+<board.size() && board[i+][j]==word[] )
{
board[i+][j] = '#';
if (Solution::dfs(board, i+, j, word.substr(,word.size()-)) ) return true;
board[i+][j] = word[];
}
return false;
}
};
第二次的代码,比第一次过的代码还优化了额外空间结构。O(1)额外空间。
【Word Search】cpp的更多相关文章
- 【word ladder】cpp
题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...
- 【Word Break】cpp
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符
1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Text Justification】cpp
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- 【Merge Intervals】cpp
题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...
- 【Insert Interval】cpp
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- 【Edit Distance】cpp
题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...
随机推荐
- 【C++】【MFC】定义全局变量的方法
在stafx.h 里面加extern CString place在stafx.app 里面加CString place
- 服务器 PyTorch 报错 重装 PyTorch
两个代码,pix2pix + CycleGan , wgan-gp 都是 pytorch 写的, 在服务器端运行,均存在下列问题,故判定是 pytorch 的安装问题. Traceback (mos ...
- Centos7_Minimal-1611 版安装python3.5.3
前提 最近在学习python3,看到好多教程都是要求在Windows或者Ubuntu 平台上使用,安装比较方便.由于不在想Winddows上安装也没有Ubutnu系统 ,所以在自己的CentOS7上面 ...
- leetcode--3
1. 题目: Longest Substring Without Repeating Characters Given a string, find the length of the longest ...
- Sonar服务器搭建
Sonar服务器搭建 Sonar概述 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson ...
- IOS 获取文本焦点 主动召唤出键盘(becomeFirstResponder) and 失去焦点(退下键盘)
主动召唤出键盘 - (void)viewDidAppear:(BOOL)animated { // 3.主动召唤出键盘 [self.nameField becomeFirstResponder]; / ...
- EF写统计
EF的特性是,你from的第一个表为主表,接下来的所有表以左联或者内联或者交叉连接的方式去显示,不会出现右联, 在编写的时候,可以先确定个数据源,然后对这个数据源进行数据的统计, 例如SQL: -- ...
- iOS支付宝 9.x 版本首页效果
http://www.jianshu.com/p/7516eb852cca 支付宝 9.x 版本首页效果 对于新版支付宝首页的产品功能这里就不说什么了,一大堆人吐槽,我们只想要一个好好的支付工具,阿里 ...
- jquery iCheck 插件
1 官网:http://www.bootcss.com/p/icheck/#download 2 博客:https://www.cnblogs.com/xcsn/p/6307610.html http ...
- CUDA常见问题与解答
源 1.在SDK自带的例子程序中,发现SRC文件珜下有.cpp文件和.cu文件.这两种文件的关系和各自的作用是什么呀? 答:SDK自带例子中的.cpp文件主要是一些CPU端处理,或者是使用CPU计算对 ...