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.

Hide Tags

Array Backtracking

 

 
    这事一道回溯题,写的有点重复,因为没有将多个if 合在一起。
 
 
 
#include <iostream>
#include <vector>
#include <string>
using namespace std; class Solution {
public:
bool exist(vector<vector<char> > &board, string word) {
if(word.length()<) return true;
if(board.size()==||board[].size()==) return false;
for(int i =;i<board.size();i++){
for(int j =;j<board[].size();j++){
if(board[i][j]==word[]&&helpFun(board,word,,i,j))
return true;
}
}
return false;
} bool helpFun(vector<vector<char> >&board,string & word,int idx,int beg_i,int beg_j)
{
if(idx == word.size()) return true;
char tmp = board[beg_i][beg_j];
board[beg_i][beg_j] = '*';
if(beg_i>&&board[beg_i-][beg_j]==word[idx]&&helpFun(board,word,idx+,beg_i-,beg_j)){
board[beg_i][beg_j] = tmp;
return true;
}
if(beg_i<board.size()-&&board[beg_i+][beg_j]==word[idx]&&helpFun(board,word,idx+,beg_i+,beg_j)){
board[beg_i][beg_j] = tmp;
return true;
}
if(beg_j>&&board[beg_i][beg_j-]==word[idx]&&helpFun(board,word,idx+,beg_i,beg_j-)){
board[beg_i][beg_j] = tmp;
return true;
}
if(beg_j<board[].size()-&&board[beg_i][beg_j+]==word[idx]&&helpFun(board,word,idx+,beg_i,beg_j+)){
board[beg_i][beg_j] = tmp;
return true;
}
board[beg_i][beg_j] = tmp;
return false;
}
}; int main()
{
vector<vector< char> > board{{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
Solution sol;
cout<<sol.exist(board,"ABCB")<<endl;
return ;
}

[LeetCode]Word Search 回溯的更多相关文章

  1. [LeetCode] Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  2. [LeetCode] Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  3. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  4. Leetcode: word search

    July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...

  5. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...

  6. [leetcode]Word Search @ Python

    原题地址:https://oj.leetcode.com/problems/word-search/ 题意: Given a 2D board and a word, find if the word ...

  7. [Leetcode] word search 单词查询

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. [LeetCode] Word Search [37]

    题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...

  9. leetcode Word Search 待解决?

    终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞 ...

随机推荐

  1. Python全栈day 03

    Python全栈day 03 一.运算符补充 in ,逻辑运算符,判断某字符或某字符串是否在一个大的字符串中,输出得到bool型数据. value = '我是中国人' v = '我' if v in ...

  2. POJ:3268-Silver Cow Party

    Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26184 Accepted: 11963 De ...

  3. [CodeForces940E]Cashback(set+DP)

    Description Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is ...

  4. IIS发布错误记录

    1.HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效. 详细错误信息模块 IIS Web Core 通知 BeginRequ ...

  5. Hadoop环境搭建 (伪分布式搭建)

    一,Hadoop版本下载 建议下载:Hadoop2.5.0 (虽然是老版本,但是在企业级别中运用非常稳定,新版本虽然添加了些小功能但是版本稳定性有带与考核) 1.下载地址: hadoop.apache ...

  6. 12,nginx+uWSGI+django+virtualenv+supervisor发布web服务器

    导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的框架 ...

  7. yum常规操作的基本用法

    比如说,我想用nano编辑器的,发现没有安装, 这个时候你就可以用$ yum search nano 查询nano属于哪个安装包下. 发现就有nano这个安装包, 这个时候安装一下这个安装包,$ su ...

  8. [译]13-spring 内部bean

    spring基于xml配置元数据的方式下,位于property元素或者contructor-arg元素内的bean元素被称为内部bean,如下: <?xml version="1.0& ...

  9. SPOJ 362 Ignore the Garbage 转7进制+简单大数除法

    显然正着倒着看仍然是数字的只有7个数:0,1,2,5,6,8,9 所以就是用这7个数组合成不同的数. 将n转换成7进制,对应位输出即可. #include <cstdio> #includ ...

  10. JavaWeb笔记(五)JSP

    JSP 指令 格式: <%@ 指令名称 属性名1=属性值1 属性名2=属性值2 ... %> 分类: page 配置JSP页面 contentType:等同于response.setCon ...