-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdfs #.cpp
46 lines (42 loc) · 844 Bytes
/
dfs #.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
#include<bits/stdc++.h>
using namespace std;
const int N=1<<10;
long long int visit[N][N];
long long int flag,n,m,sx,sy,tx,ty;
char a[1000][1000];
void dfs(int x,int y){
if(x<0||x>n-1||y<0||y>n-1||a[x][y]=='#'||visit[x][y]==1)
return;
visit[x][y]=1;
if(x==tx&&y==ty)
{
flag=1;
return;
}
dfs(x+1,y);
dfs(x-1,y);
dfs(x,y+1);
dfs(x,y-1);
}
int main(){
cin>> n>> m;
for(long long int i=0;i<n;i++){
for(long long int j=0;j<m;j++){
cin>>a[i][j];
if(a[i][j]=='S')
{
sx=i;
sy=j;
}
if(a[i][j]=='T')
{
tx=i;
ty=j;
}
}
}
dfs(sx,sy);
if(flag==1)
cout<<"FOUND";
else cout<<"NOT FOUND";
return 0;}