当前位置:网站首页>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 20 asynchronous programming: examples - cases without asynchronous
- 剑指 Offer 14- II. 剪绳子 II
- Low code platform international multilingual (I18N) technical solution
- 【习题五】【数据库原理】
- ncnn神經網絡計算框架在香柳丁派OrangePi 3 LTS開發板中的使用介紹
- [review questions of database principles]
- Nodejs+express+mysql realizes login function (including verification code)
- 如何在微信小程序中获取用户位置?
- 【数据库原理复习题】
- luoguP3694邦邦的大合唱站队
猜你喜欢

Integer case study of packaging

Brief introduction to mvcc
![[comprehensive question] [Database Principle]](/img/d7/8c51306bb390e0383a017d9097e1e5.png)
[comprehensive question] [Database Principle]

【Colab】【使用外部数据的7种方法】

Low code platform international multilingual (I18N) technical solution

GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈

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

【R】【密度聚类、层次聚类、期望最大化聚类】

【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素

Xctf mobile--rememberother problem solving
随机推荐
Redhat5 installing socket5 proxy server
sitesCMS v3.0.2发布,升级JFinal等依赖
(最新版) Wifi分销多开版+安装框架
Swift bit operation exercise
社交社区论坛APP超高颜值UI界面
【习题六】【数据库原理】
GaN图腾柱无桥 Boost PFC(单相)七-PFC占空比前馈
Attack and defense world mobile--ph0en1x-100
Four problems and isolation level of MySQL concurrency
低代码平台国际化多语言(i18n)技术方案
Everything comes to him who waits
Sword finger offer14 the easiest way to cut rope
Cache penetration and bloom filter
node的ORM使用-Sequelize
[exercice 7] [principe de la base de données]
2022-01-27 use liquibase to manage MySQL execution version
Analysis of a music player Login Protocol
Two solutions of leetcode101 symmetric binary tree (recursion and iteration)
Exploration of sqoop1.4.4 native incremental import feature
【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】