当前位置:网站首页>剑指 Offer 12. 矩阵中的路径
剑指 Offer 12. 矩阵中的路径
2022-07-03 12:10:00 【嗝~~~~】
剑指 Offer 12. 矩阵中的路径
难度中等601收藏分享切换为英文接收动态反馈
给定一个 m x n 二维字符网格 board 和一个字符串单词 word 。如果 word 存在于网格中,返回 true ;否则,返回 false 。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
例如,在下面的 3×4 的矩阵中包含单词 “ABCCED”(单词中的字母已标出)。
示例 1:
输入:board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"输出:true
示例 2:
输入:board = [["a","b"],["c","d"]], word = "abcd"输出:false
提示:
- 1 <= board.length <= 200
- 1 <= board[i].length <= 200
- board 和 word 仅由大小写英文字母组成
*思路:
模仿下述思路,每次理清楚。
思路

代码
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;
//向四个方向
board[i][j]='\0';
//剪枝,不用+而是||
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;
}
};
边栏推荐
- [exercise 5] [Database Principle]
- [judgment question] [short answer question] [Database Principle]
- Day 1 of kotlin learning: simple built-in types of kotlin
- 社交社区论坛APP超高颜值UI界面
- 十条职场规则
- [exercice 7] [principe de la base de données]
- Create a dojo progress bar programmatically: Dojo ProgressBar
- 剑指 Offer 11. 旋转数组的最小数字
- Oh my Zsh + TMUX installation
- Kotlin notes - popular knowledge points asterisk (*)
猜你喜欢

Quick learning 1.8 front and rear interfaces

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

Node.js: express + MySQL的使用

4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment

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

Xctf mobile--app2 problem solving

Application of ncnn neural network computing framework in orange school orangepi 3 lts development board
![[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)](/img/45/c2d7934b886d8090373ca9e6e23c97.gif)
[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)

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

低代码平台国际化多语言(i18n)技术方案
随机推荐
Xctf mobile--app2 problem solving
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
【習題五】【數據庫原理】
Four problems and isolation level of MySQL concurrency
Pytext training times error: typeerror:__ init__ () got an unexpected keyword argument 'serialized_ options'
Node.js: express + MySQL的使用
Keep learning swift
【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
How to convert a decimal number to binary in swift
Swift return type is a function of function
对业务的一些思考
最新版抽奖盲盒运营版
Loan calculator my pressure is high
Openstack node address change
Export the entire Oracle Database
Solve the problem of VI opening files with ^m at the end
Attack and defense world mobile--ph0en1x-100
Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
Brief introduction to mvcc
启用MemCached的SASL认证