题目:

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的更多相关文章

  1. 【word ladder】cpp

    题目: Given two words (beginWord and endWord), and a dictionary, find the length of shortest transform ...

  2. 【Word Break】cpp

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  3. 【word xml】将word转化为xml格式后,如何在xml中卫word添加分页符

    1.首先在xml中找到我们需要添加分页符的位置 例如:我需要在这个第一部分上面添加一个分页符 2.找到这个[第一部分]这个位置之后,开始往上找,找到对应的位置 3.在</w:pPr>下方添 ...

  4. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  5. 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~ ...

  6. 【Text Justification】cpp

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  7. 【Merge Intervals】cpp

    题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...

  8. 【Insert Interval】cpp

    题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...

  9. 【Edit Distance】cpp

    题目: Given two words word1 and word2, find the minimum number of steps required to convert word1 to w ...

随机推荐

  1. Zabbix邮件报警设置方法

    实现目的: 在Zabbix服务端设置邮件报警,当被监控主机宕机或者达到触发器预设值时,会自动发送报警邮件到指定邮箱. 具体操作: 以下操作在Zabbix监控服务端进行 备注:Zabbix监控服务端 操 ...

  2. F5-WAF-12.0

    平台: CentOS 类型: 虚拟机镜像 软件包: f5bigip basic software security waf 服务优惠价: 按服务商许可协议 云服务器费用:查看费用 立即部署 产品详情 ...

  3. Openfire+spark在linux上搭建内部聊天系统

    一.    实验环境 Ubuntu server14.04 openfire:http://www.igniterealtime.org/downloads/index.jsp spark:http: ...

  4. Android商城开发系列(十三)—— 首页热卖商品布局实现

    热卖商品布局效果如下图: 这个布局跟我们上节做的推荐是一样的,也是用LinearLayout和GridView去实现的,新建一个hot_item.xml,代码如下所示: <?xml versio ...

  5. PHP的模板引擎smarty原理浅谈

    mvc是开发中的一个伟大的思想,使得开发代码有了更加清晰的层次,让代码分为了三层各施其职.无论是对代码的编写以及后期的阅读和维护,都提供了很大的便利. 我们在php开发中,视图层view是不允许有ph ...

  6. StackOverflow之旅<2>------{HashMap和Hashtable的区别}

    问题 在Java中HashMap和Hashtable的区别? 哪一个对于多线程应用程序更好? 回答 Hashtable是同步的,加了synchronized锁,而HashMap不是.没有加synchr ...

  7. World Wind Java开发之十三——加载Geoserver发布的WMS服务(转)

    这篇是转载的平常心博客,原地址见:http://www.v5cn.cn/?p=171 1.WMSTiledImageLayer类说明 一个WMSTiledImageLayer类对象只能对应一个WMS发 ...

  8. go语言,第三方包相对路径导入包引起的问题及解决方案(goquery)

    对go语言而言,跟踪init很显然包有且仅有一次被导入的可能. 但是重复引用了goquery包,后编译出现问题 项目涉及相关目录 ├── main.go└── parse    └── parse.g ...

  9. RAC基本使用

    @interface ViewController () @property (weak, nonatomic) IBOutlet lwRedView *redView; @property (wea ...

  10. Softmax回归(Softmax Regression

    多分类问题 在一个多分类问题中,因变量y有k个取值,即.例如在邮件分类问题中,我们要把邮件分为垃圾邮件.个人邮件.工作邮件3类,目标值y是一个有3个取值的离散值.这是一个多分类问题,二分类模型在这里不 ...