当前位置:网站首页>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;
}
};
边栏推荐
- Swift5.7 extend some to generic parameters
- Everything comes to him who waits
- Application of ncnn Neural Network Computing Framework in Orange Pi 3 Lts Development Board
- alright alright alright
- Loan calculator my pressure is high
- Leetcode234 palindrome linked list
- [combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number
- Bert running error: attributeerror: module'tensorflow contrib. tpu' has no attribute 'InputPipelineConfig'
- 高效能人士的七个习惯
- Analysis of the influence of voltage loop on PFC system performance
猜你喜欢
![[problem exploration and solution of one or more filters or listeners failing to start]](/img/82/e7730d289c4c1c4800b520c58d975a.jpg)
[problem exploration and solution of one or more filters or listeners failing to start]

Brief introduction to mvcc

并网-低电压穿越与孤岛并存分析

最新版盲盒商城thinkphp+uniapp

Grid connection - Analysis of low voltage ride through and island coexistence

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

initial、inherit、unset、revert和all的区别

(latest version) WiFi distribution multi format + installation framework

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

Four problems and isolation level of MySQL concurrency
随机推荐
【数据库原理复习题】
[comprehensive question] [Database Principle]
对业务的一些思考
[review questions of database principles]
Node. Js: use of express + MySQL
[judgment question] [short answer question] [Database Principle]
Do you feel like you've learned something and forgotten it?
Solve the problem of VI opening files with ^m at the end
Xctf mobile--app1 problem solving
剑指 Offer 15. 二进制中1的个数
Tensorflow binary installation & Failure
[exercise 6] [Database Principle]
ImportError: No module named examples. tutorials. mnist
[ArcGIS user defined script tool] vector file generates expanded rectangular face elements
剑指 Offer 17. 打印从1到最大的n位数
【数据库原理及应用教程(第4版|微课版)陈志泊】【第五章习题】
(latest version) WiFi distribution multi format + installation framework
解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
sitesCMS v3.0.2发布,升级JFinal等依赖
Cache penetration and bloom filter