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的运行过程剖析·编程语言分类

    总结: 编程语言的分类 编译型: 说明:与汇编语言类似,都有一个编译程序将源代码编译成硬件可执行的二进制代码 特点:执行速度快.同等情况下对系统要求低,适合于开发大型应用程序.数据库系统.操作系统等 ...

  2. linux无名管道

    特点 无名管道是半双工的,也就是说,一个管道要么只能读,要么只能写 只能在有共同祖先的进程间使用(父子进程.兄弟进程.子孙进程等) fork或者execve调用创建的子进程,继承了父进程的文件描述符 ...

  3. Multiplication Puzzle ZOJ - 1602

    Multiplication Puzzle ZOJ - 1602 传送门 The multiplication puzzle is played with a row of cards, each c ...

  4. POJ 2441 状压DP

    Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 5289   Accepted: 2033 ...

  5. activity堆栈式管理

    package com.chinaCEB.cebActivity.utils; import java.util.Stack; import android.app.Activity; import ...

  6. DNSSec

    Domain Name System Security Extensions (DNSSEC)DNS安全扩展,是由IETF提供的一系列DNS安全认证的机制(可参考RFC2535).它提供了一种来源鉴定 ...

  7. 成员变量和属性区别(@property那点事儿)

    历史由来: 接触iOS的人都知道,@property声明的属性默认会生成一个_类型的成员变量,同时也会生成setter/getter方法. 但这只是在iOS5之后,苹果推出的一个新机制.看老代码时,经 ...

  8. Pascal编写的蠕虫病毒,凌盟提供,Chaobs转载

    { Happy Birthday (c) 1998 WoRmI don't take responsibility for any damage caused by this virus.It was ...

  9. JMeter学习笔记(十一) 关于 CSV Data Set Config 的 Sharing mode 对取值的影响

    关于 CSV Data Set Config 的一些介绍之前已经梳理过了,可以参考: https://www.cnblogs.com/xiaoyu2018/p/10184127.html . 今天主要 ...

  10. ansible自动安装jdk

    脚本功能:安装jdk 测试环境:CentOS6.7 说明: 1.卸载系统自带的openjdk,重新安装Oracle jdk,支持Hotspot,性能更好,更稳定. 2.jdk软件包按文档说明进行定制( ...