题目:

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. mail客户端POP和IMAP协议

    POP-邮局协议 mail客户端如果使用POP协议,那么线上服务器的邮件将会自动下载到客户端. IMAP-因特网消息访问协议 mail客户端如果使用IMAP协议,邮件服务器上的邮件将不会自动下载到客户 ...

  2. Winform调整DEV控件高度

  3. xtrabackup 安装

    xtrabackup 安装   yum install -y perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL  perl-Dige ...

  4. python_43_移动文件指针补充

    #移动文件指针补充 ''' 文件对象.seek((offset,where)) offset:移动的偏移量,单位为字节.等于正数时向文件尾方向移动,等于负数时向文件头方向移动文件指针 where:指针 ...

  5. C# FileInfo 类

    FileInfo类不像File类,它没有静态方法,仅可用于实例化的对像.FileInfo对像表示在磁盘或网络位置的文件,注意它不是流,为了读写文件,必须创建Stream对像. fileInfo类提供了 ...

  6. node第一天

    一.主要执行的文件命名一般为main.js var aModule =require('./a.js');//相对路径 var aModule =require('a.js');//专门从node_m ...

  7. STL笔记(に)--vector容器

    Vector 1.可变长的动态数组 2.需包含头文件#include<vector> (当然,如果用了万能头文件#include<bits/stdc++.h>则可忽略) 3.支 ...

  8. C/C++程序基础 (二)常用知识点

    使用宏实现max 注意括号在宏内的使用 #define MAX(x, y) ( ( (x) > (y) ) ? (x) : (y) ) 宏参数连接 a##e##b 转化为字符串 #a const ...

  9. mysql 存储过程 例子

    DROP PROCEDURE IF EXISTS variable_demo; delimiter // CREATE PROCEDURE variable_demo() BEGIN select ' ...

  10. Date.prototype.Format---对Date的扩展

    // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...