当前位置:网站首页>剑指 Offer 12. 矩阵中的路径
剑指 Offer 12. 矩阵中的路径
2022-06-28 23:43:00 【anieoo】
原题链接:剑指 Offer 12. 矩阵中的路径
solution:
dfs
class Solution {
public:
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
vector<vector<bool>> vis; //保存是否遍历过该点
bool exist(vector<vector<char>>& board, string word) {
vis = vector<vector<bool>> (board.size(), vector<bool> (board[0].size(), false));
for(int i = 0;i < board.size();i++)
for(int j = 0;j < board[0].size();j++) {
if(dfs(board, word, 0, i, j)) return true;
}
return false;
}
bool dfs(vector<vector<char>>& board, string &word, int u,int x,int y) {
if(board[x][y] != word[u]) return false;
if(u == word.size() - 1) return true;
vis[x][y] = true;
for(int i = 0;i < 4;i++) {
int a = x + dx[i],b = y + dy[i];
if(a < 0 || a == board.size() || b < 0 || b == board[0].size() || vis[a][b]) continue;
if(dfs(board, word, u + 1, a, b)) return true;
}
vis[x][y] = false;
return false;
}
};边栏推荐
- Class extension and optional type extension of dart
- How to make two objects or arrays equal
- 华为22级专家十年心血终成云原生服务网格进阶实战文档,是真的6
- 百度知道爬虫,根据问题id,线索id,评论id获取评论下面的对话
- Stm32f407----- capacitive touch button
- Is it safe to open a stock account on the Internet?
- I can't sleep
- Don't ask me how to do UI automation test again
- [word Tutorial Series Part 1] how to remove arrows in word tables
- 这样学习二叉树
猜你喜欢

stm32F407-------RTC实时时钟

lock4j--分布式锁中间件--使用/实例

ERROR 1067 (42000): Invalid default value for ‘end_time‘ Mysql

MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数

Finally, someone explained the cloud native architecture

Be on the list again! Know that Chuangyu was selected as one of the top 50 competitive enterprises in China's network security industry in 2022

从SQL注入绕过最新安全狗WAF中学习fuzz

【软件分析】软件分析、设计与建模迭代式详解
![[API packet capturing in selenium automation] installation and configuration of browsermobproxy](/img/67/3e15b2191ee23a8c4453aad007651d.png)
[API packet capturing in selenium automation] installation and configuration of browsermobproxy
![Edge extraction based on Halcon learning [2] circles Hdev routine](/img/e4/e3738d71c2ff5a239a12f67d06e2c9.jpg)
Edge extraction based on Halcon learning [2] circles Hdev routine
随机推荐
Form verification problem - El select (solution to automatically trigger verification on initialization page)
[stm32 Hal library] serial port communication
note
Chapter II Classic synchronous exercises
【SSM】报错 Access denied for user ‘WYF‘@‘localhost‘ (using password: YES) 数据的用户名变成了电脑的用户名
[stm32 HAL库] RTC和BKP驱动
urllib.parse 解析url连接中的参数
入行数字IC验证后会做些什么?
Stm32f407 ------ running lamp and buzzer
[stm32 HAL库] 串口通信
10. Standard i/o redirection and pipeline
Flutter obtains the coordinate size of any element in the interface through globalkey
【状态机设计】Moore、Mealy状态机、三段式、二段式、一段式状态机书写规范
stm32F407-------电容触摸按键
Save data in Excel: use openpyxl to create multiple tables and set excel row limit
IO playback function of FIO
MATLAB 学习笔记(6)MATLAB 的 upsample 函数和 downsample 函数
再次上榜!知道创宇入选2022中国网安产业竞争力50强
Is it safe to open a stock account on the Internet?
Implementation of dynamic timer for quartz