-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphGrid.cpp
56 lines (47 loc) · 1.07 KB
/
GraphGrid.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
//Basic dfs on grid
#include<iostream>
#include<vector>
using namespace std;
bool visited[1001][1001];
int N;
char arr[100][100];
bool isvalid(int x,int y)
{
if(x<0 || y<0 ||x>=N||y>=N)
return false;
if(visited[x][y]==true)
return false;
if(arr[x][y]=='#')
return false;
return true;
}
void dfs(int x,int y) // dfs on grid function
{
visited[x][y]=true; // totaly ridiculus, can use 2 array of possible x and y direction moves
cout<<x<<" "<<y<<endl;
if(isvalid(x-1,y))
dfs(x-1,y);
if(isvalid(x,y-1))
dfs(x,y-1);
if(isvalid(x+1,y))
dfs(x+1,y);
if(isvalid(x,y+1))
dfs(x,y+1);
}
int main()
{
cin>>N;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
cin>>arr[i][j];
dfs(0,0);
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
cout<<visited[i][j]<<" ";
cout<<endl;
}
if(visited[N-1][N-1]==true)
cout<<"can reach" ;
return 0;
}