题目:

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.

Example:

board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
] Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false.

分析:

给定一个二维网格和一个单词,找出该单词是否存在于网格中。

单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。

一道搜索的题目,有点类似走迷宫,只不过按照给定单词字母顺序来寻找,遍历board中每一个元素,判断与word中的第一个字母是否相同,如果相同则在当前位置上去搜索上下左右相邻的单元格的元素是否和当前字母的下一个字母相同,不在搜索范围内或者字母不同就返回false,当搜索的字母数等于word的长度时,也就表明在board找到了这个word。注意每次判断一个字母要标记当前位置以搜索过,以防止字母重复利用。我选择直接更改board元素,以便在后续的判断中不会重复判断此位置,在搜索结束后改回来就可以了。

程序:

class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
h = board.size();
w = board[].size();
for(int i = ; i < h; i++){
for(int j = ; j < w; ++j){
if(searchexist(board, word, , i, j)) return true;
}
}
return false;
}
int searchexist(vector<vector<char>>& board, string &word, int n, int x, int y){
if(x < || x > h- || y < || y > w- || word[n] != board[x][y])
return ;
if(n == word.length()-)
return ;
char temp = board[x][y];
board[x][y] = ;
int flag = searchexist(board, word, n+, x+, y)
||searchexist(board, word, n+, x-, y)
||searchexist(board, word, n+, x, y+)
||searchexist(board, word, n+, x, y-);
board[x][y] = temp;
return flag;
}
private:
int h, w;
};

LeetCode 79. Word Search单词搜索 (C++)的更多相关文章

  1. [LeetCode] 79. Word Search 单词搜索

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

  2. [LeetCode] 79. 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 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  4. LeetCode 79. Word Search(单词搜索)

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

  5. LeetCode 79 Word Search(单词查找)

    题目链接:https://leetcode.com/problems/word-search/#/description 给出一个二维字符表,并给出一个String类型的单词,查找该单词是否出现在该二 ...

  6. [LeetCode OJ] Word Search 深度优先搜索DFS

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

  7. 079 Word Search 单词搜索

    给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...

  8. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  9. Leetcode#79 Word Search

    原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...

随机推荐

  1. 题解 P2719 【搞笑世界杯】

    其实懂了之后很简单,但是刚开始真的很难想.. d[a][b]表示剩a张A类票和b张B类票时,最后两张票相同的概率 那么此时的排队的第一个人只有两种选择 拿A类票或者B类票 抛硬币得到的可能性当然是二分 ...

  2. Paper | A novel deep learning-based method of improving coding efficiency from the decoder-end for HEVC

    目录 精彩叙述 细节 发表在2017年DCC. 这篇文章立意很简单,方法也很简单,但是做得早.效果好.引用量也不错(40+). 指标:在HEVC的intra.LDP.LDB和RA模式下,BDBR平均可 ...

  3. 1+x 证书 Web 前端开发 MySQL 知识点梳理

    官方QQ群 1+x 证书 Web 前端开发 MySQL 知识点梳理 http://blog.zh66.club/index.php/archives/199/

  4. AngleSharp 实战(05)之遍历内部子元素(x)元素,尝试着获取元素的 Attr 和 InnerText

    直接贴代码了: using System; using System.Linq; using System.Threading.Tasks; using AngleSharp; using Angle ...

  5. Kubernetes service 使用定义

    Kubernetes service 使用定义 介绍说明 • 防止Pod失联• 定义一组Pod的访问策略• 支持ClusterIP,NodePort以及LoadBalancer三种类型• Servic ...

  6. C++ 在线编译器(支持 C++11)

    C++11 的 Inheriting constructors 特性在 GCC 4.8 以前的版本及 VS2013 中都没有支持,测试起来比较麻烦,所以搜集到了几个支持 GCC 4.8 及更高版本的在 ...

  7. dedecms5.7的获取本文章的TAG

    tag调用标签如下: {dede:tag row='10' getall='1' sort='month'} <li><a href='[field:link/]'>[fiel ...

  8. ASP.NET Core Web 项目文件

    在本节中,我们将探索并了解 asp.net core 项目文件. 我们使用 C#作为编程语言,因此项目文件具有.csproj 扩展名. 如果您使用过以前版本的 ASP.NET,那么您可能对此文件非常熟 ...

  9. 关于 L3 缓存行 cacheLIne 的研究!还是对程序有举足轻重的作用!

    https://www.cnblogs.com/PurpleTide/archive/2010/11/25/1887506.html CLR via C# 读书笔记 2-3 Cache Lines a ...

  10. Python - 正则表达式 - 第二十二天

    正则表达式 - 教程 正则表达式(Regular Expression)是一种文本模式,包括普通字符(例如,a 到 z 之间的字母)和特殊字符(称为"元字符"). 正则表达式使用单 ...