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

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

示例:

board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
] 给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.
 #include "_000库函数.h"

 //此问题是一个DFS问题,每个单词可以像上下左右四个方向进行下一步行走

 class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if (board.empty() || board[].empty())return false;
int m = board.size(), n = board[].size();
vector<vector<bool>>visted(m, vector<bool>(n, false));//记录是否被访问过
for (int i = ; i < m; ++i) {
for (int j = ; j < n; ++j) {
if (search(board, visted, , i, j, word))return true;//每个单词单元都进行上下左右进行搜索!
}
}
} bool search(vector<vector<char>>& board, vector<vector<bool>>& visted, int idx, int i, int j, string word) {
if (idx == word.size())return true;
int m = board.size(), n = board[].size();
if (i < || j < || i >= m || j >= n || visted[i][j] || board[i][j] != word[idx])return false;
visted[i][j] = true;
bool res = (search(board, visted, idx + , i - , j, word) ||
search(board, visted, idx + , i + , j, word) ||
search(board, visted, idx + , i, j - , word) ||
search(board, visted, idx + , i, j + , word));
visted[i][j] = false;//回溯
return res;
}
}; void T079() {
Solution s;
vector<vector<char>> board;
string word;
board = { {'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'} };
word = "ABCCED";
cout << s.exist(board, word) << endl; }

力扣算法题—079单词搜索【DFS】的更多相关文章

  1. 力扣算法题—069x的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

  2. 力扣算法题—093复原IP地址

    给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...

  3. 力扣算法题—060第K个排列

    给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...

  4. 力扣算法题—051N皇后问题

    #include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...

  5. 力扣算法题—050计算pow(x, n)

    #include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...

  6. 力扣算法题—147Insertion_Sort_List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  7. 力扣算法题—111.Minimum Depth of Binary Tree

      Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the sh ...

  8. 力扣算法题—052N皇后问题2

    跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...

  9. 力扣算法题—143ReorderList

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...

随机推荐

  1. 微信扫码登录(3)---授权码code获取用户基本信息

    授权码code获取用户基本信息 上一遍已经获得微信回调的code,网址:回调获取code     那这篇通过code和其它参数去获得用户基本信息. 1.UserServiceImpl关键代码 @Ove ...

  2. JavaScript面向对象--多态

    一.多态的概念 相同的函数作用于不同的对象,会得到不同的结果,这就是多态. 二.如果不用多态,会怎么样? 这里有个浅显易懂的例子,定义一个函数叫makeSound,传入不同的对象,函数体里要写不同的情 ...

  3. LogDashboard 1.0.4 版本发布

    LogDashboard 1.0.4 版本 有关LogDashboard的介绍请看这里.logDashboard已经发布了1.0.4版本 有关这个版本的详细变化可以在Github上的里程碑上查看 支持 ...

  4. PE知识复习之PE的导入表

    PE知识复习之PE的导入表 一丶简介 上一讲讲解了导出表. 也就是一个PE文件给别人使用的时候.导出的函数  函数的地址 函数名称 序号 等等. 一个进程是一组PE文件构成的.  PE文件需要依赖那些 ...

  5. 解决Mysql错误:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (111)

    需要重启服务器:sudo /etc/init.d/mysql restart

  6. SmartSql 类型处理器

    Nuget 安装 Install-Package SmartSql.TypeHandler -Version 3.0.1 SmartSql.TypeHandler 包括了俩种类型的类型处理程序: Js ...

  7. C#线程安全使用(三)

    在讲CancellationTokenSource之前我决定先讲一下lock和Interlocked,如果能很好的理解这两个,再去理解CancellationTokenSource就会方便很多,由于我 ...

  8. SQL优化总结之二

    1.列优先 如图有表A和表B,对其查询时,会有如下语句: select a.*,b.* from a,b where a.id = b.a_id; 注意from 后边的表名, a.如果多表查询是完全无 ...

  9. 第一册:lesson 105.

    原文: Full of mistakes. Where's Sandra,Bob? I want her. Do you want to speak to her? Yes I do. I want ...

  10. C# IQueryable和IEnumerable的区别

    在使用EF查询数据的时候,我们常用的查询数据方式有linq to sql,linq to object, 查询返回的结果有两种类型:IQueryable.IEnumerable,两者内部的处理机制是完 ...