-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path00079-word_search.cpp
59 lines (44 loc) · 1.32 KB
/
00079-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
// 79: Word Search
// https://leetcode.com/problems/word-search
#include <iostream>
#include <vector>
using namespace std;
class Solution {
private:
bool backtrack(vector<vector<char>>& board, int i, int j, string& word) {
if (!word.size()) return true;
if (i<0 || i>=board.size() || j<0 || j>=board[0].size() || board[i][j]!=word[0]) return false;
char c = board[i][j];
board[i][j] = '*';
string s = word.substr(1);
bool result = backtrack(board, i-1, j, s)
|| backtrack(board, i+1, j, s)
|| backtrack(board, i, j-1, s)
|| backtrack(board, i, j+1, s);
board[i][j] = c;
return result;
}
public:
// SOLUTOIN
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 (backtrack(board, i, j, word))
return true;
return false;
}
};
int main() {
Solution o;
// INPUT
vector<vector<char>> board = {
{'A','B','C','E'},
{'S','F','C','S'},
{'A','D','E','E'}
};
string word = "ABCCED";
// OUTPUT
auto result = o.exist(board, word);
cout << boolalpha << result << endl;
return 0;
}