当前位置:网站首页>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;
}
};
边栏推荐
- [network counting] Chapter 3 data link layer (2) flow control and reliable transmission, stop waiting protocol, backward n frame protocol (GBN), selective retransmission protocol (SR)
- Keep learning swift
- Enable SASL authentication for memcached
- Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
- 剑指 Offer 14- I. 剪绳子
- Low code platform international multilingual (I18N) technical solution
- [comprehensive question] [Database Principle]
- 自抗扰控制器七-二阶 LADRC-PLL 结构设计
- [exercise 5] [Database Principle]
- Nodejs+express+mysql realizes login function (including verification code)
猜你喜欢
剑指 Offer 12. 矩阵中的路径
我的创作纪念日:五周年
Node. Js: use of express + MySQL
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】
自抗扰控制器七-二阶 LADRC-PLL 结构设计
The latest version of lottery blind box operation version
Seven second order ladrc-pll structure design of active disturbance rejection controller
How to convert a decimal number to binary in swift
随机推荐
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
Exploration of sqoop1.4.4 native incremental import feature
[combinatorics] permutation and combination (multiple set permutation | multiple set full permutation | multiple set incomplete permutation all elements have a repetition greater than the permutation
Node.js: express + MySQL的使用
剑指 Offer 14- I. 剪绳子
Swift Error Handling
基于同步坐标变换的谐波电流检测
【数据挖掘复习题】
Airflow installation jump pit
Powerful avatar making artifact wechat applet
Harmonic current detection based on synchronous coordinate transformation
Enter the length of three sides of the triangle through the user, and calculate the area of the triangle, where the length is a real number
【习题七】【数据库原理】
Kung Fu pays off, and learning is done
剑指 Offer 14- II. 剪绳子 II
CVPR 2022 图像恢复论文
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
Swift bit operation exercise
如何在微信小程序中获取用户位置?
Ali & ant self developed IDE