-
Notifications
You must be signed in to change notification settings - Fork 1
/
BFS in Grid.cpp
100 lines (73 loc) · 1.85 KB
/
BFS in Grid.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<stdio.h>
#include<queue>
using namespace std;
#define MAX 500
int row,col;
char box[MAX][MAX];
int visit[MAX][MAX],dis[MAX][MAX];
int rrr[]={1,0,-1,0};
int ccc[]={0,1,0,-1}; //4 Direction
void bfs(int start_x,int start_y)
{
queue<int>Qx;
queue<int>Qy;
visit[start_x][start_y] = 1;
dis[start_x][start_y] = 0;
Qx.push(start_x);
Qy.push(start_y);
while(!Qx.empty())
{
int x = Qx.front();
int y = Qy.front();
Qx.pop();
Qy.pop();
for(int i=0;i<4;i++) // for four direction move
{
int u = x+rrr[i];
int v = y+ccc[i];
if(0<=u && u<row && 0<=v && v<col)
{
if(visit[u][v]==0)
{
if(box[u][v]=='.')
{
visit[u][v]=1;
dis[u][v] = dis[x][y]+1;
Qx.push(u);
Qy.push(v);
}
}
}
}
}
return ;
}
int main()
{
int i;
scanf("%d%d",&row,&col);
for(int i=0;i<row;i++) scanf(" %s",box[i]);
int start_x = 0;
int start_y = 0;
bfs(start_x,start_y);
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++) printf("%d ",dis[i][j]);
printf("\n");
}
}
/*
As it is a two dimensional grid we need to keep x and y for one position. So we use 2 Queues.
Here only . is visitable and x is not visitable.
Suppose we are in 1,0 position; If we want to make 4 moves up,down,right,left then our coordinate will be
0,0 if we go up
2,0 if we go down
1,-1 if we go left
1,1 if we go right
With rrr and ccc we are finding that thing and checking whether it is in the grid.
Input::
3 3
..x
x.x
...
*/