当前位置:网站首页>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;
}
};
边栏推荐
- Clickhouse notes 02 -- installation test clickvisual
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- Recommended books | essentials of industrial digital transformation: methods and Practice
- 预处理指令
- leetcode-79:单词搜索
- [advanced mathematics] [3] Application of differential mean value theorem and derivative
- Difference Between Accuracy and Precision
- MPI学习笔记(二):矩阵相乘的两种实现方法
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
- leetcode-6129:全 0 子数组的数目
猜你喜欢

Why did I choose to become a network engineer after graduating from weak current for 3 months
![[advanced mathematics] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions

预处理指令

【高等数学】【1】函数、极限、连续

Illustration leetcode - 3. longest substring without repeated characters (difficulty: medium)

Difference Between Accuracy and Precision

毕业从事弱电3个月,我为什么会选择转行网络工程师

Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important

【TensorRT】动态batch进行推理

KEGG通路的从属/注释信息如何获取
随机推荐
Leetcode-146: LRU cache
Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
【高等数学】【8】微分方程
增加 swap 空间
Chapter VI modified specification (SPEC) class
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
Fanoutexchange switch code tutorial
JS scope and scope chain
Struct, enum type and union
Remote - actual combat
leetcode-146:LRU 缓存
移动web布局方法
SecureCRT garbled code solution [easy to understand]
Aircraft PID control (rotor flight control)
[advanced mathematics] [5] definite integral and its application
Follow up of Arlo's thinking
How much memory does bitmap occupy in the development of IM instant messaging?
Character function and string function (2)