-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0079.单词搜索.cpp
103 lines (99 loc) · 3.11 KB
/
0079.单词搜索.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* @lc app=leetcode.cn id=79 lang=cpp
*
* [79] 单词搜索
*
* https://leetcode-cn.com/problems/word-search/description/
*
* algorithms
* Medium (38.68%)
* Likes: 254
* Dislikes: 0
* Total Accepted: 28.1K
* Total Submissions: 71.2K
* Testcase Example: '[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]\n"ABCCED"'
*
* 给定一个二维网格和一个单词,找出该单词是否存在于网格中。
*
* 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
*
* 示例:
*
* board =
* [
* ['A','B','C','E'],
* ['S','F','C','S'],
* ['A','D','E','E']
* ]
*
* 给定 word = "ABCCED", 返回 true.
* 给定 word = "SEE", 返回 true.
* 给定 word = "ABCB", 返回 false.
*
*/
// @lc code=start
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
bool exist(vector<vector<char>>& board, string word)
{
bool ans = false;
vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(), false));
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[0].size(); ++j) {
if (word[0] == board[i][j]) {
visited[i][j] = true;
ans = aux(board, word, visited, 1, i, j);
visited[i][j] = false;
if (ans) {
return ans;
}
}
}
}
return ans;
}
bool aux(const vector<vector<char>>& board, const string& word, vector<vector<bool>>& visited, int count, int x, int y)
{
if (count == word.size()) {
return true;
}
bool up = false, down = false, right = false, left = false;
if (x > 0 && !visited[x - 1][y] && board[x - 1][y] == word[count]) {
visited[x - 1][y] = true;
up = aux(board, word, visited, count + 1, x - 1, y);
visited[x - 1][y] = false;
if (up) {
return true;
}
}
if (x < (board.size() - 1) && !visited[x + 1][y] && board[x + 1][y] == word[count]) {
visited[x + 1][y] = true;
down = aux(board, word, visited, count + 1, x + 1, y);
visited[x + 1][y] = false;
if (down) {
return true;
}
}
if (y < (board[0].size() - 1) && !visited[x][y + 1] && board[x][y + 1] == word[count]) {
visited[x][y + 1] = true;
right = aux(board, word, visited, count + 1, x, y + 1);
visited[x][y + 1] = false;
if (right) {
return true;
}
}
if (y > 0 && !visited[x][y - 1] && board[x][y - 1] == word[count]) {
visited[x][y - 1] = true;
left = aux(board, word, visited, count + 1, x, y - 1);
visited[x][y - 1] = false;
if (left) {
return true;
}
}
return false;
}
};
// @lc code=end