当前位置:网站首页>Sword finger offer 12 Path in matrix
Sword finger offer 12 Path in matrix
2022-07-03 12:59:00 【Hiccup~~~~】
The finger of the sword Offer 12. Path in matrix
Medium difficulty 601 Switch to English to receive dynamic feedback
Given a m x n Two dimensional character grid board And a string word word . If word Exists in the grid , return true ; otherwise , return false .
The words must be in alphabetical order , It's made up of letters in adjacent cells , among “ adjacent ” Cells are those adjacent horizontally or vertically . Letters in the same cell are not allowed to be reused .
for example , In the following 3×4 The matrix of contains the word “ABCCED”( The letters in the word are marked ).
Example 1:
Input :board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" Output :true
Example 2:
Input :board = [["a","b"],["c","d"]], word = "abcd" Output :false
Tips :
- 1 <= board.length <= 200
- 1 <= board[i].length <= 200
- board and word It consists of upper and lower case English letters only
* Ideas :
Imitate the following ideas , Clear up every time .
Ideas

Code
class Solution {
public:
bool flag=0;
bool dfs(vector<vector<char> >& board, string word,int i,int j,int k){
if(i<0||j<0||i>=board.size()||j>=board[0].size()||board[i][j]!=word[k])
return 0;
if(k==word.size()-1)
return 1;
// In four directions
board[i][j]='\0';
// prune , no need + It is ||
bool res=dfs(board,word,i-1,j,k+1)|| dfs(board,word,i+1,j,k+1) || dfs(board,word,i,j-1,k+1) || dfs(board,word,i,j+1,k+1);
board[i][j]=word[k];
return res;
}
bool exist(vector<vector<char> >& board, string word) {
if(word=="") return true;
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
if(dfs(board,word,i,j,0))
return 1;
}
}
return 0;
}
};
边栏推荐
- Deeply understand the mvcc mechanism of MySQL
- 社交社区论坛APP超高颜值UI界面
- 剑指 Offer 16. 数值的整数次方
- 基于同步坐标变换的谐波电流检测
- [exercise 7] [Database Principle]
- SQL learning notes (I)
- 自抗扰控制器七-二阶 LADRC-PLL 结构设计
- The best shortcut is no shortcut
- Seven second order ladrc-pll structure design of active disturbance rejection controller
- Everything comes to him who waits
猜你喜欢

强大的头像制作神器微信小程序

The latest version of blind box mall thinkphp+uniapp

4. 无线体内纳米网:电磁传播模型和传感器部署要点

Swift bit operation exercise

高效能人士的七个习惯

【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】

Detailed explanation of the most complete constraintlayout in history

Harmonic current detection based on synchronous coordinate transformation

如何在微信小程序中获取用户位置?

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
随机推荐
Detailed explanation of the most complete constraintlayout in history
2022-01-27 use liquibase to manage MySQL execution version
Glide question you cannot start a load for a destroyed activity
T430 toss and install OS majave 10.14
剑指 Offer 16. 数值的整数次方
A large select drop-down box, village in Chaoyang District
[review questions of database principles]
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
Enable SASL authentication for memcached
[judgment question] [short answer question] [Database Principle]
How to get user location in wechat applet?
最新版抽奖盲盒运营版
Attack and defense world mobile--ph0en1x-100
【数据挖掘复习题】
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
【数据库原理复习题】
如何在微信小程序中获取用户位置?
[exercice 7] [principe de la base de données]
Loan calculator my pressure is high
sitesCMS v3.0.2发布,升级JFinal等依赖