当前位置:网站首页>Sword finger offer 12 Path in matrix
Sword finger offer 12 Path in matrix
2022-06-28 23:47:00 【anieoo】
Original link : The finger of the sword Offer 12. Path in matrix
solution:
dfs
class Solution {
public:
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
vector<vector<bool>> vis; // Save whether to traverse the point
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;
}
};边栏推荐
- Stm32f407 ------ serial (serial port) communication
- What pitfalls should be avoided in the job interview for the operation post in 2022?
- Class extension and optional type extension of dart
- [machine learning] numerical analysis 02 -- finding roots of arbitrary equations
- 在线买股票开户安全嘛?
- Insomnia last night
- Finally, someone explained the cloud native architecture
- Is it safe to open an account for buying stocks online?
- Use conditional breakpoints in vscode (based on GDB)
- Chapter II Classic synchronous exercises
猜你喜欢

The second session of question swiping and clock out activity -- solving the switching problem with recursion as the background (2)

ctfshow XSS

Stm32f407 ------ running lamp and buzzer

Behaviortree in ros2

随笔记:模拟类数组(array-like)的方法
![[stm32 Hal library] RTC and BKP drives](/img/72/c2c46377d0a2a5a032802640ca0201.png)
[stm32 Hal library] RTC and BKP drives

Mobile heterogeneous computing technology - GPU OpenCL programming (basic)

TypeScript -- 第二节:变量声明

TypeScript--第四节:函数

TypeScript -- 第一节:基础类型
随机推荐
[stm32 Hal library] serial port communication
随笔记:定义setter和getter的三种方式
Yyds dry inventory solution sword finger offer: maximum sum of continuous subarrays (II)
Machine learning 4-dimension reduction technology
Is it safe to open a stock account on the Internet?
What will be done after digital IC Verification?
Auto encoder
Insomnia last night
Would like to ask, how to open a stock account? Is it safe to open an account online?
Mysql-5.7.30-winx64 installation free download and installation tutorial
PHP 使用endroid/qrcode 二维码生成, GD库生成分享海报
Stm32f407-------- NVIC interrupt priority management
Windows10 phpstudy installing redis extension
百度知道爬虫,根据问题id,线索id,评论id获取评论下面的对话
[API packet capturing in selenium automation] installation and configuration of browsermobproxy
MySQL connection query is easy to understand
【C Primer Plus第二章课后编程题】
window10 phpstudy 安装redis扩展
关联线探究,如何连接流程图的两个节点
Basic operation of MySQL database: import hellodb SQL and query as required; Create account and authorize