-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordSearch.cpp
127 lines (102 loc) · 3.76 KB
/
WordSearch.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Word Search
// https://leetcode.com/explore/interview/card/top-interview-questions-medium/109/backtracking/797/
#include <iostream>
#include <vector>
#include <string>
bool isInvalid(std::vector<std::vector<char>>& board, const std::string word, int r, int c,
const int m, const int n, std::vector<std::vector<bool>>& visited, int index)
{
return r < 0 || c < 0 || r >= m || c >= n ||
(board[r][c] != word[index]);
//&& (visited[r][c] == false);
}
bool dfs(std::vector<std::vector<char>>& board, const std::string word, int r, int c,
const int m, const int n, std::vector<std::vector<bool>>& visited, int index)
{
if (index == word.size()) {
return true;
}
if (r < 0 || c < 0 || r >= m || c >= n ||
(board[r][c] != word[index])) {
return false;
}
board[r][c] = '*';
bool result = dfs(board, word, r + 1, c, m, n, visited, index + 1) ||
dfs(board, word, r - 1, c, m, n, visited, index + 1) ||
dfs(board, word, r, c - 1, m, n, visited, index + 1) ||
dfs(board, word, r, c + 1, m, n, visited, index + 1);
std::cout << word[index] << " ";
board[r][c] = word[index];
return result;
}
bool recursive(std::vector<std::vector<char>>& board, const std::string word)
{
if (board.size() == 0 || board[0].size() == 0 || word.empty()) {
return false;
}
int m = board.size();
int n = board[0].size();
std::vector<std::vector<bool>> visited (m , std::vector<bool>(n, false));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
std::cout << "==== " << board[i][j] << "\n";
if (dfs(board, word, 0, 0, m, n, visited, 0)) {
return true;
}
}
}
return false;
}
bool iterative(std::vector<std::vector<char>>& board, const std::string word)
{
if (board.size() == 0 || board[0].size() == 0 || word.empty()) {
return false;
}
int m = board.size();
int n = board[0].size();
std::vector<std::vector<bool>> visited (m , std::vector<bool>(n, false));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
// Match forst char
if (board[i][j] == word[0]) {
// bfs
}
}
}
return false;
}
void printBoard(std::vector<std::vector<char>>& board)
{
for (auto row : board) {
for (auto cell : row) {
std::cout << cell << " ";
}
std::cout << "\n";
}
}
int main()
{
std::vector<std::vector<char>> board {{'A','B','C','E'},
{'S','F','C','S'},
{'A','D','E','E'}};
std::cout << "Board : \n";
printBoard(board);
std::cout << "=================================\n";
std::string word = "SEE";
std::cout << word << " present in board ?\n";
std::cout << "Recursive : " << std::boolalpha << recursive(board, word) << "\n";
std::cout << "Iterative : " << std::boolalpha << iterative(board, word) << "\n";
/*
std::cout << "=================================\n";
std::string word = "ABCCED";
std::cout << word << " present in board ?\n";
std::cout << "Recursive : " << std::boolalpha << recursive(board, word) << "\n";
std::cout << "Iterative : " << std::boolalpha << iterative(board, word) << "\n";
std::cout << "=================================\n";
word = "ABCB";
std::cout << word << " present in board ?\n";
std::cout << "Recursive : " << std::boolalpha << recursive(board, word) << "\n";
std::cout << "Iterative : " << std::boolalpha << iterative(board, word) << "\n";
*/
return 0;
}