-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgridBFS.cpp
More file actions
57 lines (44 loc) · 750 Bytes
/
gridBFS.cpp
File metadata and controls
57 lines (44 loc) · 750 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int N,M;
bool isValid(int x , int y)
{
if(x < 1 || x > N || y < 1 || y > M)
return false;
if(vis[x][y] == true || ar[x][y] == 0)
return false;
return true;
}
void BFS(int x,int y,vector<vector> &grid)
{
visited[x][y]=1;
queue<pair<int,int>> q;
dist[x][y]=0;
q.push({x,y});
while(!q.empty())
{
int curX=q.front().first;
int curY=q.front().second;
q.pop();
for(int k=0;k<4;k++)
{
int nX=curX+dx[k];
int nY=curY+dy[k];
if(isValid(nX,nY))
{
if(!visited[nX][nY])
{
dist[nX][nY]=dist[curX][curY]+1;
q.push({nX,nY});
visited[nX][nY]=1;
}
}
}
}
}
int main()
{
return 0;
}