当前位置:网站首页>剑指 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;
}
};
边栏推荐
- Xctf mobile--app3 problem solving
- 如何在微信小程序中获取用户位置?
- Glide 4.6.1 API initial
- Oh my Zsh + TMUX installation
- [exercise 6] [Database Principle]
- C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
- How to convert a decimal number to binary in swift
- 十条职场规则
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
- 4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
猜你喜欢
【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
Analysis of the influence of voltage loop on PFC system performance
Social community forum app ultra-high appearance UI interface
Differences between initial, inherit, unset, revert and all
基于同步坐标变换的谐波电流检测
Day 1 of kotlin learning: simple built-in types of kotlin
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
Four problems and isolation level of MySQL concurrency
【R】【密度聚类、层次聚类、期望最大化聚类】
随机推荐
Swift5.7 extend some to generic parameters
context. Getexternalfilesdir() is compared with the returned path
阿里 & 蚂蚁自研 IDE
Simple use and precautions of kotlin's array array and set list
【習題七】【數據庫原理】
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
电压环对 PFC 系统性能影响分析
Kotlin - 改良装饰者模式
Brief introduction to mvcc
Xctf mobile--app3 problem solving
【数据库原理及应用教程(第4版|微课版)陈志泊】【第四章习题】
Detailed explanation of the most complete constraintlayout in history
With pictures and texts, summarize the basic review of C language in detail, so that all kinds of knowledge points are clear at a glance?
Tianyi ty1208-z brush machine detailed tutorial (free to remove)
The latest version of lottery blind box operation version
Sqoop1.4.4原生增量导入特性探秘
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
Ten workplace rules
Low code platform international multilingual (I18N) technical solution
Exploration of sqoop1.4.4 native incremental import feature