-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT079. Word Search.cpp
30 lines (30 loc) · 1009 Bytes
/
T079. Word Search.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
class Solution {
public:
bool search(vector<vector<char>>&board,string word,int x,int y){
if(word.length() == 1)
return board[x][y] == word[0];
if(board[x][y] != word[0])
return false;
char temp = board[x][y];
board[x][y] = '#';
int offset[][2] = {{1,0},{0,1},{-1,0},{0,-1}};
for(int i = 0;i < 4;i++){
int x0 = x + offset[i][0];
int y0 = y + offset[i][1];
if(x0 >= 0 && x0 < board.size() && y0 >= 0 && y0 < board[0].size() && board[x0][y0] != '#'){
if(search(board,word.substr(1, word.size()-1),x0,y0))return true;
}
}
board[x][y] = temp;
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++){
if(search(board,word,i,j))
return true;
}
}
return false;
}
};