当前位置:网站首页>Leetcode-79: word search
Leetcode-79: word search
2022-07-25 20:38:00 【Chrysanthemum headed bat】
leetcode-79: Word search
subject
Topic linking
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 .
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","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
Output :true
Example 3:
Input :board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output :false

Problem solving
Method 1 : to flash back
class Solution {
public:
vector<vector<int>> dirs={
{
1,0},{
-1,0},{
0,1},{
0,-1}};
vector<vector<bool>> visit;
bool dfs(vector<vector<char>>& board,string& word,int index,int x,int y){
if(index==word.size()) return true;
if(x<0||x>=board.size()||y<0||y>=board[0].size()) return false;
if(visit[x][y]) return false;
char c=word[index];
if(board[x][y]!=c) return false;
for(vector<int>& dir:dirs){
int nx=x+dir[0];
int ny=y+dir[1];
visit[x][y]=true;
if(dfs(board,word,index+1,nx,ny)) return true;
visit[x][y]=false;
}
return false;
}
bool exist(vector<vector<char>>& board, string word) {
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
visit=vector<vector<bool>>(board.size(),vector<bool>(board[0].size(),false));
if(dfs(board,word,0,i,j)) return true;
}
}
return false;
}
};
边栏推荐
猜你喜欢

LeetCode通关:哈希表六连,这个还真有点简单

leetcode-919:完全二叉树插入器
![[tensorrt] dynamic batch reasoning](/img/59/42ed0074de7162887bfe2c81891e20.png)
[tensorrt] dynamic batch reasoning

Working principle of radar water level gauge and precautions for installation and maintenance

preprocessor directives

Unity VS—— VS中默认调试为启动而不是附加到Unity调试

Clickhouse notes 02 -- installation test clickvisual
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/60/09c5f44d64b96c6e4d57e5f426e4ed.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results

FanoutExchange交换机代码教程

【高等数学】【1】函数、极限、连续
随机推荐
LeetCode通关:哈希表六连,这个还真有点简单
How to obtain the subordinate / annotation information of KEGG channel
How to choose a microservice registration center?
Key network protocols in tcp/ip four layer model
Behind every piece of information you collect, you can't live without TA
4everland storage node portal network design
Proxy implements MySQL read / write separation
Embedded development: embedded foundation -- threads and tasks
Link list of sword finger offer question bank summary (III) (C language version)
Docker 搭建 Redis Cluster集群
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
Interpretation of filter execution sequence source code in sprigboot
文件操作详解
[MCU] 51 MCU burning those things
preprocessor directives
Dataframe first performs grouping operation and then combines output
Step num problem
[advanced mathematics] [5] definite integral and its application
【高等数学】【3】微分中值定理与导数的应用
Difference Between Accuracy and Precision