-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS on grid-CSES-labyrinth
More file actions
76 lines (47 loc) · 1.16 KB
/
BFS on grid-CSES-labyrinth
File metadata and controls
76 lines (47 loc) · 1.16 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
64
65
66
67
68
69
70
71
72
73
You are given a map of a labyrinth, and your task is to find a path from start to end. You can walk left, right, up and down.
Input
The first input line has two integers n and m: the height and width of the map.
First print "YES", if there is a path, and "NO" otherwise.
If there is a path, print the length of the SHORTESST path and its description as a
string consisting of characters L (left), R (right), U (up), and D (down).
You can print any valid solution.
Input:
5 8
########
#.A#...#
#.##.#B#
#......#
########
Output:
YES
9
LDDRRRRRU
//bfs on grid
bool isValid(int x,int y){
if(x<0 || x>N || y<0 || y>M )return false;
if(grid[x][y]=='#' || vis[x][y]==true) return false;
return true;
}
void bfs(int strtX,int strtY,int destX,int destY){
queue<pair<int,int>> q;
q.push({strtX,strtY});
vis[strtX][strtY]=true;
dis[strtX][strtY]=0;
int dX={-1,1,0,0};
int dY={0,0,-1,1};
while(!q.empty()){
int currX=q.front().first;
int currY=q.front().second;
q.pop();
for(int i=0;i<4;i++){
if(isValid(currX+dX[i],currY+dY[i]))
{
int newX=currX+dX[i];
int newY=currY+dY[i];
vis[newX][newY]=true
q.push({newX,newY});
dis[newX][newY]=dis[currX][currY]+1;
}
}
}
}