当前位置:网站首页>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;
}
};
边栏推荐
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
- 【R】【密度聚类、层次聚类、期望最大化聚类】
- 最新版抽奖盲盒运营版
- sitesCMS v3.1.0发布,上线微信小程序
- Social community forum app ultra-high appearance UI interface
- Node. Js: use of express + MySQL
- Nodejs+express+mysql realizes login function (including verification code)
- Enable SASL authentication for memcached
- 剑指 Offer 12. 矩阵中的路径
- Glide 4.6.1 API initial
猜你喜欢
【Colab】【使用外部数据的7种方法】
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
[data mining review questions]
Leetcode234 palindrome linked list
Seven second order ladrc-pll structure design of active disturbance rejection controller
【R】【密度聚类、层次聚类、期望最大化聚类】
电压环对 PFC 系统性能影响分析
Brief introduction to mvcc
Attack and defense world mobile--ph0en1x-100
随机推荐
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
Grid connection - Analysis of low voltage ride through and island coexistence
Xctf mobile--app2 problem solving
sitesCMS v3.0.2发布,升级JFinal等依赖
Quickly learn member inner classes and local inner classes
C graphical tutorial (Fourth Edition)_ Chapter 17 generic: genericsamplep315
Kung Fu pays off, and learning is done
When the R language output rmarkdown is in other formats (such as PDF), an error is reported, latex failed to compile stocks Tex. solution
Ali & ant self developed IDE
The foreground uses RSA asymmetric security to encrypt user information
Kotlin notes - popular knowledge points asterisk (*)
sitesCMS v3.1.0发布,上线微信小程序
Huffman coding experiment report
alright alright alright
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
I'm too lazy to write more than one character
Simple use and precautions of kotlin's array array and set list
关于CPU缓冲行的理解
The latest version of blind box mall thinkphp+uniapp
Harmonic current detection based on synchronous coordinate transformation