-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind the string in grid.cpp
More file actions
64 lines (56 loc) · 1.41 KB
/
Copy pathFind the string in grid.cpp
File metadata and controls
64 lines (56 loc) · 1.41 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
54
55
56
57
58
59
60
61
62
63
64
#include<bits/stdc++.h>
using namespace std;
void findWord(int, int);
bool _findWord(char**, int, int, int, int, string);
int main(){
int t, n, m;
cin >> t;
while(t--){
cin >> n >> m;
findWord(n, m);
}
return 0;
}
void findWord(int n, int m){
char **arr = new char*[n];
string x;
bool res = false;
for(int i = 0; i < n; i++)
arr[i] = new char[m];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> arr[i][j];
cin >> x;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
if(x[0] == arr[i][j]){
if(_findWord(arr, i, j, n, m, x)){
cout << i << " " << j << ", ";
res = true;
}
}
if(!res)
cout << -1 << endl;
else
cout << endl;
}
bool _findWord(char **arr, int x, int y, int n, int m, string str){
int xMove[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int yMove[] = {-1, 0, 1, 1, 1, 0, -1, -1};
for(int i = 0; i < 8; i++){
int _x = x, _y = y;
int len = str.length(), temp = 1;
while(temp < len){
_x = _x + xMove[i];
_y = _y + yMove[i];
if(!(0 <= _x && _x < n && 0 <= _y && _y < m))
break;
if(arr[_x][_y] != str[temp])
break;
temp++;
}
if(temp == len)
return true;
}
return false;
}