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. VIM安装YCM插件

    折腾了两天,终于好了 1.配置VIM (1)下载相关插件 sudo apt-get install git sudo apt-get install build-essential cmake sud ...

  2. 用python实现【五猴分桃】问题

    转载链接:https://blog.csdn.net/cy309173854/article/details/78296839 据说“五猴分桃”问题最先是由大物理学家狄拉克提出来的,这一貌似简单的问题 ...

  3. [BZOJ1927]星际竞速(费用流)

    Description 题意:给定n个点m条边的无向图,只能从编号小的到编号大的,且要求经过所有点刚好一次,而且可以从任意点瞬移到i号点并花费代价Ai,求最小代价. n<=800,m<=1 ...

  4. 笔记-cookie参数

    笔记-cookie参数 cookie各个参数详解 定义和用法 setcookie()函数向客户端发送一个 HTTP cookie. cookie是由服务器发送到浏览器的变量.cookie 通常是服务器 ...

  5. python文件,字符串,二进制的读写

    读文件: f = open('/Users/michael/test.txt', 'r') #一次读取文件的全部内容 f.read() #文件使用完毕后必须关闭,因为文件对象会占用操作系统的资源,并且 ...

  6. python 函数function

    函数 当代码出现有规律的重复的时候,只写一次函数实现多次使用(调用) 可使用的函数: 自定义函数 内置函数:文档  https://docs.python.org/3/library/function ...

  7. 5,pandas高级数据处理

    1.删除重复元素 使用duplicated()函数检测重复的行,返回元素为布尔类型的Series对象,每个元素对应一行,如果该行不是第一次出现,则元素为True - keep参数:指定保留哪一重复的行 ...

  8. form表单提交和重置小结

    1. input标签 1.1>input[type=submit] <form name="form" method="post" action=& ...

  9. ceph 存储系统

    http://w w w.c s d n 123.com/html/topnews201408/2/4702.htm

  10. .net 下word 中的图片与文字分离

    最近在做一个项目要求word 中的图片与文字分离 ,找了好久终于找到一个完美的方法 c#实现word中的图文分离   part 1: class define Code highlighting pr ...