-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0079. Word Search.cpp
More file actions
53 lines (46 loc) · 1.53 KB
/
0079. Word Search.cpp
File metadata and controls
53 lines (46 loc) · 1.53 KB
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
// Task: https://leetcode.com/problems/word-search/?envType=daily-question&envId=2024-04-03
#include <iostream>
#include <vector>
#include <string>
class Solution {
public:
bool exist(std::vector<std::vector<char>>& board, std::string word) {
for (int i = 0; i < board.size(); i++)
for (int j = 0; j < board[0].size(); j++)
if (dfs(board, word, i, j, 0))
return true;
return false;
}
private:
bool dfs(std::vector<std::vector<char>>& board, const std::string& word, int i, int j, int s) {
if (i < 0 || i == board.size() || j < 0 || j == board[0].size())
return false;
if (board[i][j] != word[s] || board[i][j] == '*')
return false;
if (s == word.length() - 1)
return true;
const char cache = board[i][j];
board[i][j] = '*';
const bool exists = dfs(board, word, i + 1, j, s + 1) ||
dfs(board, word, i - 1, j, s + 1) ||
dfs(board, word, i, j + 1, s + 1) ||
dfs(board, word, i, j - 1, s + 1);
board[i][j] = cache;
return exists;
}
};
int main() {
// Example 1:
Solution s1;
std::vector<std::vector<char>> mat1 = { {'A','B','C','E'} ,{ 'S', 'F', 'C','S' }, {'A', 'D', 'E', 'E'} };
std::cout << s1.exist(mat1, "ABCCED") << "\n";
// Example 2:
Solution s2;
std::vector<std::vector<char>> mat2 = { {'A','B','C','E'} ,{ 'S', 'F', 'C','S' }, {'A', 'D', 'E', 'E'} };
std::cout << s2.exist(mat2, "SEE") << "\n";
// Example 3:
Solution s3;
std::vector<std::vector<char>> mat3 = { {'A','B','C','E'} ,{ 'S', 'F', 'C','S' }, {'A', 'D', 'E', 'E'} };
std::cout << s3.exist(mat3, "ABCD") << "\n";
return 0;
}