forked from shivammaniharsahu/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfs in grid.cpp
More file actions
63 lines (63 loc) · 1.05 KB
/
bfs in grid.cpp
File metadata and controls
63 lines (63 loc) · 1.05 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
#include<bits/stdc++.h>
using namespace std;
char ar[1001][1001];
bool vis[1001][1001];
int n,m;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
char direction[4]={'U','R','D','L'};
int path[1001][2];
bool isValid(int x,int y)
{
if(x<1 || x>n || y<1 || y>m)
{
return false;
}
if(vis[x][y] || ar[x][y]=='#')
{
return false;
}
return true;
}
void bfs(int srcx,int srcy)
{
queue<pair<int,int> >q;
q.push(make_pair(srcx,srcy));
vis[srcx][srcy]=true;
while(!q.empty())
{
int currx=q.front().first;
int curry=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
int newx=currx+dx[i];
int newy=curry+dy[i];
if(isValid(newx,newy))
{
path.push_back(direction[i]);
vis[newx][newy]=true;
q.push(make_pair(newx,newy));
}
}
}
cout<<path;
}
int main()
{
cin>>n>>m;
int srcx,srcy;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>ar[i][j];
if(ar[i][j]=='A')
{
srcx=i; srcy=j;
}
}
}
bfs(srcx,srcy);
return 0;
}