-
Notifications
You must be signed in to change notification settings - Fork 0
/
79_Word Search.cpp
85 lines (81 loc) · 2 KB
/
79_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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
if (board.size() <= 0 || word.size() <= 0) return false;
int row = board.size(), col = board[0].size();
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (board[i][j] == word[0]) {
vector<vector<bool>> vis(row, vector<bool>(col, false));
vis[i][j] = true;
if (searchWord(i, j, 0, row, col, board, word, vis)) {
return true;
}
}
}
}
return false;
}
bool searchWord(int x, int y, int step, int row, int col, vector<vector<char>>& board,
const string& word, vector<vector<bool>>& vis) {
if (board[x][y] != word[step]) {
return false;
}
if (step >= word.size()-1) {
return true;
}
bool res = false;
for (int i = 0; i < 4; ++i) {
int nextX = x+dx[i];
int nextY = y+dy[i];
if (nextX >= 0 && nextX < row && nextY >= 0 && nextY < col && !vis[nextX][nextY]) {
vis[nextX][nextY] = true;
res |= searchWord(nextX, nextY, step+1, row, col, board, word, vis);
if (res) return true;
vis[nextX][nextY] = false;
}
}
return res;
}
private:
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
};
int main() {
Solution s;
int n, m;
string word;
while (cin >> n >> m) {
cin >> word;
vector<vector<char>> board(n, vector<char>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> board[i][j];
}
}
cout << s.exist(board, word) << endl;
}
return 0;
}
/**
3 4
ABCCED
A B C E
S F C S
A D E E
1
3 4
SEE
A B C E
S F C S
A D E E
1
3 4
ABCB
A B C E
S F C S
A D E E
0
*/