-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindtheword.cpp
64 lines (62 loc) · 1.1 KB
/
findtheword.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
#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
char matrix[76][76];
int slen = -1,sr = -1,sc = -1,er = -1,ec = -1,r = -1,c = -1;
string goal;
vector<pair<int,int>> arr;
void find_pos()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(matrix[i][j] == goal[0])
arr.push_back(make_pair(i,j));
}
}
}
void dfs(int nr,int nc,int cnt)
{
if(goal[cnt] != matrix[nr][nc]) return;
if(nr<0 || nr>=r || nc<0 || nc >= c) return;
if(cnt == slen-1)
{
if(er!=-1)
er = min(er,nr);
else
er = nr;
if(ec!=-1)
ec = min(ec,nc);
else
ec = nc;
return;
}
dfs(nr-1,nc,cnt+1);
dfs(nr+1,nc,cnt+1);
dfs(nr,nc-1,cnt+1);
dfs(nr,nc+1,cnt+1);
}
int main()
{
fastio
cin>>r>>c;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>matrix[i][j];
cin>>goal;
slen = goal.size();
find_pos();
int x;
for(x=0;x<arr.size();x++)
{
dfs(arr[x].first,arr[x].second,0);
if(ec != -1 && er != -1)
{
cout<<arr[x].first+1<<" "<<arr[x].second+1<<"\n"<<er+1<<" "<<ec+1;
break;
}
}
if(x == arr.size()) cout<<"NO\n";
return 0;
}