力扣算法题—079单词搜索【DFS】
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
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】的更多相关文章
- 力扣算法题—069x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- 力扣算法题—093复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- 力扣算法题—051N皇后问题
#include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...
- 力扣算法题—050计算pow(x, n)
#include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...
- 力扣算法题—147Insertion_Sort_List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 力扣算法题—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 ...
- 力扣算法题—052N皇后问题2
跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...
- 力扣算法题—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 万点暴击,二狗子被 DDoS 攻击的惨痛经历
二狗子的遭遇 “好消息,好消息,免费 DDoS 攻击软件上线了,性感黑客在线攻击,帮您攻克所有商业难题.”二狗子不知道在看着什么网站,新买的 Mac 中发出热闹的声音. 二狗子想知道“DDoS 是什么 ...
- 取代 FlashP2P,H5P2P 将成为 WebP2P 主流
5 月 19.20 日,行业精英齐聚的 WebRTCon 2018 在上海举办.又拍云 PrismCDN 项目负责人凌建发在大会做了<又拍云低延时的 WebP2P 直播实践>的精彩分享. ...
- qt之窗口换肤
1.相关文章 Qt 资源系统qt的moc,uic,rcc命令的使用 2.概要 毕业两年了,一直使用的是qt界面库来开发程序,使用过vs08.10.13等开发工具,并安装了qt的插件,最近在做客户 ...
- redis 系列27 Cluster高可用 (2)
一. ASK错误 集群上篇最后讲到,对于重新分片由redis-trib负责执行,关于该工具以后再介绍.在进行重新分片期间,源节点向目标节点迁移一个槽的过程中,可以会出现该槽中的一部分键值对保存在源节点 ...
- Leetcode:338. Bit位计数
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...
- Hibernate【inverse和cascade属性】知识要点
Inverse属性 Inverse属性:表示控制权是否转移.. true:控制权已转移[当前一方没有控制权] false:控制权没有转移[当前一方有控制权] Inverse属性,是在维护关联关系的时候 ...
- 10年架构师告诉你,他眼中的Spring容器是什么样子的
相关文章 如何慢慢地快速成长起来? 成长的故事之Spring Core系列 你是如何看待Spring容器的,是这样子吗? Spring的启动过程,你有认真思考过吗?(待写) 面向切面编程,你指的是Sp ...
- -1-2 java 面向对象基本概念 封装继承多态 变量 this super static 静态变量 匿名对象 值传递 初始化过程 代码块 final关键字 抽象类 接口 区别 多态 包 访问权限 内部类 匿名内部类 == 与 equal
java是纯粹的面向对象的语言 也就是万事万物皆是对象 程序是对象的集合,他们通过发送消息来相互通信 每个对象都有自己的由其他的对象所构建的存储,也就是对象可以包含对象 每个对象都有它的类型 也就是 ...
- Log4Net使用学习笔记
项目源文件下载https://files.cnblogs.com/files/ckym/Log4NetTestSourceCode.zip Log4net是一款非常好用的日志记录的框架,使用它可以实现 ...
- C# ListBox实现显示插入最新的数据的方法
在我们使用ListBox控件时,如果我们在里面不断的添加一条条数据,但是在我们添加的数据过多超过了ListBox显示的窗口时(此时会产生滑动条), 发现我们无法看到最新添加的数据.实现倒序显示此处有两 ...