-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path79.单词搜索.cpp
48 lines (45 loc) · 1.18 KB
/
79.单词搜索.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*
* @lc app=leetcode.cn id=79 lang=cpp
*
* [79] 单词搜索
*/
// @lc code=start
class Solution {
private:
bool dfs(int level, const string& word, int row, int col,
vector<vector<char> >& board) {
// terminator
if (row < 0 || col < 0 || row > board.size() - 1 ||
col > board[row].size() - 1)
return false;
if (board[row][col] != word[level]) return false;
if (level == word.size() - 1) return true;
// process current logic
char ch = board[row][col];
board[row][col] = 0;
// drill down
if (dfs(level + 1, word, row + 1, col, board) ||
dfs(level + 1, word, row - 1, col, board) ||
dfs(level + 1, word, row, col + 1, board) ||
dfs(level + 1, word, row, col - 1, board)) {
return true;
}
// restore status
board[row][col] = ch;
return false;
}
public:
bool exist(vector<vector<char> >& board, string word) {
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board[i].size(); j++) {
if (dfs(0, word, i, j, board)) return true;
}
}
return false;
}
};
// @lc code=end