-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexist.cpp
More file actions
26 lines (25 loc) · 825 Bytes
/
exist.cpp
File metadata and controls
26 lines (25 loc) · 825 Bytes
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
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
m = board.size();
n = board[0].size();
for (int i = 0 ; i < m;i++){
for (int j = 0; j < n; j++){
if (search(board,word,0,i,j)) return true;
}
}
return false;
}
private:
int m;
int n;
bool search(vector<vector<char>>& board,string word, int k,int x,int y){
if (k == word.size()) return true;
if (x < 0 || y < 0 || x >=m || y>=n || board[x][y]!=word[k])return false;
char tmp = board[x][y];
board[x][y] = '\0';
if (search(board,word,k+1,x+1,y)||search(board,word,k+1,x-1,y)||search(board,word,k+1,x,y+1)||search(board,word,k+1,x,y-1)) return true;
board[x][y] = tmp;
return false;
}
};