当前位置:网站首页>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;
}
};
边栏推荐
- Tensorflow binary installation & Failure
- The latest version of blind box mall thinkphp+uniapp
- Kung Fu pays off, and learning is done
- [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)
- SSH登录服务器发送提醒
- [exercise 5] [Database Principle]
- SLF4J 日志门面
- node的ORM使用-Sequelize
- Tianyi ty1208-z brush machine detailed tutorial (free to remove)
- Kotlin - 改良装饰者模式
猜你喜欢

Two solutions of leetcode101 symmetric binary tree (recursion and iteration)

Social community forum app ultra-high appearance UI interface

Dojo tutorials:getting started with deferrals source code and example execution summary

【数据库原理及应用教程(第4版|微课版)陈志泊】【SQLServer2012综合练习】

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

Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board

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

Analysis of the influence of voltage loop on PFC system performance

The latest version of blind box mall thinkphp+uniapp

Xctf mobile--app2 problem solving
随机推荐
The latest version of blind box mall thinkphp+uniapp
【数据库原理及应用教程(第4版|微课版)陈志泊】【第五章习题】
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
初入职场,如何快速脱颖而出?
Social community forum app ultra-high appearance UI interface
并网-低电压穿越与孤岛并存分析
Dix règles de travail
sitesCMS v3.1.0发布,上线微信小程序
ImportError: No module named examples. tutorials. mnist
Deeply understand the mvcc mechanism of MySQL
Using swift language features, write a pseudo-random number generator casually
【R】【密度聚类、层次聚类、期望最大化聚类】
Swift5.7 extend some to generic parameters
Leetcode234 palindrome linked list
【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
I'm too lazy to write more than one character
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Openstack node address change
Glide question you cannot start a load for a destroyed activity
studio All flavors must now belong to a named flavor dimension. Learn more