-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinStepsToReachDestination
48 lines (45 loc) · 1009 Bytes
/
MinStepsToReachDestination
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
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
int main(){
std::vector<std::vector<int>>Nodes(3,std::vector<int>(4,0));
Nodes[0][0]=1;
Nodes[0][1]=1;
Nodes[0][2]=1;
Nodes[0][3]=1;
Nodes[1][3]=1;
// Nodes[2][2]=1;
Nodes[2][3]=1;
std::queue<std::pair<int,int>>q;
//std::vector<int>visited;
int row[]={1,-1,0,0};
int col[]={0,0,-1,1};
std::pair<int,int>source(0,0);
std::pair<int,int>dest(0,3);
int steps = 0;
q.push(source);
while(!q.empty()){
std::pair<int,int>u = q.front();
// std::cout<<"C: "<<u.first<<" "<<u.second<<"\n";
//mark as visited
Nodes[u.first][u.second]=-1;
for(int i=0;i<4;++i){
int x = row[i]+u.first;
int y = col[i]+u.second;
if(x<0 || y<0 || x>=3 || y>=4)
continue;
if(x==dest.first && y==dest.second){
steps++;
std::cout<<steps<<"\n";
return 0;
}
if(Nodes[x][y]!=-1 && Nodes[x][y]!=0){
q.push(std::pair<int,int>(x,y));
steps++;
}
}
q.pop();
}
std::cout<<steps<<"\n";
}