题目:

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. Spring MVC + Thymeleaf

    参考网址: https://www.cnblogs.com/litblank/p/7988689.html 一.简介 1.Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启动WEB应 ...

  2. mysql用户权限操作

    mysql用户权限操作1.创建用户mysql -urootcreate database zabbix default charset utf8;grant all on zabbix.* to za ...

  3. genlist -s 192.168.21.\*

    显示网段192.168.21中可用的主机.

  4. LeetCode Excel Sheet Column Number 表列数

    题意:天啊!我竟然看不懂题意,还去翻别人的代码才懂!给定一个字符串,求该字符串二十六进制的总值. 思路:'A'~'Z'就是1到26,"AA"=26+1=27,"BA&qu ...

  5. 动态规划专题(五)——斜率优化DP

    前言 斜率优化\(DP\)是难倒我很久的一个算法,我花了很长时间都难以理解.后来,经过无数次的研究加以对一些例题的理解,总算啃下了这根硬骨头. 基本式子 斜率优化\(DP\)的式子略有些复杂,大致可以 ...

  6. python基础一 day17 复习

    # 迭代器# 生成器进阶 # 内置函数 # 55个 # 带key的 max min filter map sorted # 思维导图上红色和黄色方法必须会用 # 匿名函数 # lambda 参数,参数 ...

  7. javaweb基础(31)_国际化(i18n)

    一.国际化开发概述 软件的国际化:软件开发时,要使它能同时应对世界不同地区和国家的访问,并针对不同地区和国家的访问,提供相应的.符合来访者阅读习惯的页面或数据. 国际化(internationaliz ...

  8. v4l2解析

    v4l2的学习建议和流程解析: http://www.cnblogs.com/silence-hust/p/4464291.html 补充: 枚举设备所支持的image format: VIDIOC_ ...

  9. BZOJ2118: 墨墨的等式(最短路 数论)

    题意 墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N.{an}.以及B的取值范围,求出有多少B可以使等式存在非负整数解. So ...

  10. 一、MySQL 安装

    MySQL 安装 所有平台的 MySQL 下载地址为: MySQL 下载 . 挑选你需要的 MySQL Community Server 版本及对应的平台. 注意:安装过程我们需要通过开启管理员权限来 ...