-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathword-search.cc
More file actions
33 lines (31 loc) · 809 Bytes
/
word-search.cc
File metadata and controls
33 lines (31 loc) · 809 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
27
28
29
30
31
32
// Word Search
#define REP(i, n) for (int i = 0; i < (n); i++)
class Solution {
private:
bool f(vector<vector<char> > &a, int x, int y, const char *p) {
if (a[x][y] != *p++) return false;
a[x][y] = ~a[x][y];
bool flag = false;
if (!*p)
flag = true;
else
REP(i, 4) {
int xx = x+(int[]){-1,0,1,0}[i], yy = y+(int[]){0,1,0,-1}[i];
if (unsigned(xx) < a.size() && unsigned(yy) < a[0].size() && f(a, xx, yy, p)) {
flag = true;
break;
}
}
a[x][y] = ~a[x][y];
return flag;
}
public:
bool exist(vector<vector<char> > &board, string word) {
if (word.empty()) return true;
REP(i, board.size())
REP(j, board[0].size())
if (f(board, i, j, word.c_str()))
return true;
return false;
}
};