-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find the string in grid.cpp
46 lines (43 loc) · 1.15 KB
/
Find the string in grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
GFG
Find the string in grid
class Solution {
public:
const int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
void dfs(vector<vector<char>>& grid, string &word, int &index, int row, int col, int q, bool &find){
int m=grid.size();
int n=grid[0].size();
if(row>=m||row<0||col<0||col>=n)
return;
if(grid[row][col]==word[index])
index++;
else return;
if(index==word.length()){
find=true;
return;
}
dfs(grid,word,index,row+dx[q],col+dy[q],q,find);
}
vector<vector<int>> searchWord(vector<vector<char>> grid, string word) {
vector<vector<int>> ans;
int m=grid.size();
int n=grid[0].size();
for(int i=0; i<m; ++i){
for(int j=0; j<n; ++j){
int index=0;
if(grid[i][j]==word[index]){
for(int k=0; k<8; ++k){
bool find=false;
index=0;
dfs(grid,word,index,i,j,k,find);
if(find){
ans.push_back({i,j});
break;
}
}
}
}
}
return ans;
}
};