-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathFebruary-01.cpp
57 lines (49 loc) · 1.39 KB
/
February-01.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
bool dfs(vector<vector<char>> &b, string &w, int i, int j, int k) {
if(k == w.size()) return true;
if(i < 0 || j < 0 || i >= b.size() || j >= b[0].size() || b[i][j] != w[k]) return false;
char t = b[i][j];
b[i][j] = '#';
bool f = dfs(b, w, i-1, j, k+1) || dfs(b, w, i+1, j, k+1) ||
dfs(b, w, i, j-1, k+1) || dfs(b, w, i, j+1, k+1);
b[i][j] = t;
return f;
}
bool isWordExist(vector<vector<char>> &b, string w) {
for(int i = 0; i < b.size(); i++)
for(int j = 0; j < b[0].size(); j++)
if(b[i][j] == w[0] && dfs(b, w, i, j, 0))
return true;
return false;
}
};
//{ Driver Code Starts.
int main() {
int tc;
cin >> tc;
while (tc--) {
int n, m;
cin >> n >> m;
vector<vector<char>> mat(n, vector<char>(m, '*'));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> mat[i][j];
string word;
cin >> word;
Solution obj;
bool ans = obj.isWordExist(mat, word);
if (ans)
cout << "true\n";
else
cout << "false\n";
cout << "~"
<< "\n";
}
return 0;
}
// } Driver Code Ends