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

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

示例:

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. cmd命令窗口的快速选中复制黏贴

    右击"窗口标题栏",选择属性,进入属性面板: 在"选项"面板勾选编辑选项下的"快速编辑模式"; 点击确认. 这时鼠标左键就有了选中功能,可以 ...

  2. http缓存与离线缓存

    一.http协议实现缓存 1. 缓存头部 通用缓存.条件缓存.缓存控制三大类 头部名称 说明 请求/响应 通用缓存头部 控制客户端是否向服务器发送请求或者是服务端响应请求   cache-contro ...

  3. Quartz.NET学习笔记(三) 简单触发器

    触发器是Quartz.NET的另外第一个核心元素,他有2种类型,简单触发器(Simple Trigger)和计划任务触发器(Cron  Trigger), 一个触发器可以绑定一个任务. 通用触发器属性 ...

  4. MongoDB exception:connection failed

    根据http://www.runoob.com/mongodb/mongodb-window-install.html的教程配置了MongoDB,Mongod.exe配置为 --port 指令表明mo ...

  5. Node.js 中的 stream

    什么是 stream Stream 借鉴自 Unix 编程哲学中的 pipe. Unix shell 命令中,管道式的操作 | 将上一个命令的输出作为下一个命令的输入.Node.js stream 中 ...

  6. Chapter 5 Blood Type——20

    "Just let me sit for a minute, please?" I begged. “就让我坐一会可以吗?” 我乞求道. He helped me sit on t ...

  7. golang - gob与rpc

    今天和大家聊聊golang中怎么使用rpc,rpc数据传输会涉及到gob编码,所以先讲讲gob,别担心,就算你完全没有接触过gob与rpc,只要知道rpc的中文是远程过程调用,剩下的我都能给你讲明白( ...

  8. 说说不知道的Golang中参数传递

    本文由云+社区发表 导言 几乎每一个C++开发人员,都被面试过有关于函数参数是值传递还是引用传递的问题,其实不止于C++,任何一个语言中,我们都需要关心函数在参数传递时的行为.在golang中存在着m ...

  9. [十一]基础数据类型之Character

    Character与Unicode Character 基本数据类型char  的包装类 Character 类型的对象包含一个 char 类型的字段   该类提供了几种方法来确定字符的类别(小写字母 ...

  10. .Net语言 APP开发平台——Smobiler学习日志:如何快速实现手机上的资源上传功能

    最前面的话:Smobiler是一个在VS环境中使用.Net语言来开发APP的开发平台,也许比Xamarin更方便 一.目标样式 我们要实现上图中的效果,需要如下的操作: 1.从工具栏上的“Smobil ...