-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
48 lines (41 loc) · 1.15 KB
/
Copy pathsolution.cpp
File metadata and controls
48 lines (41 loc) · 1.15 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
class Solution
{
public:
int n, m;
vector<vector<int>> dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool dfs(int i, int j, int idx, vector<vector<char>> &mat, string &word)
{
// If entire word is matched
if (idx == word.size())
return true;
// Boundary check and character match check
if (i < 0 || j < 0 || i >= n || j >= m || mat[i][j] != word[idx])
return false;
// Mark cell as visited
char temp = mat[i][j];
mat[i][j] = '#';
// Explore all 4 directions
for (auto &d : dir)
{
if (dfs(i + d[0], j + d[1], idx + 1, mat, word))
return true;
}
// Backtrack
mat[i][j] = temp;
return false;
}
bool isWordExist(vector<vector<char>> &mat, string &word)
{
n = mat.size();
m = mat[0].size();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (dfs(i, j, 0, mat, word))
return true;
}
}
return false;
}
};