-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeAndLadder
50 lines (42 loc) · 964 Bytes
/
snakeAndLadder
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
#include<iostream>
#include<queue>
int BFS(int g[]){
bool visited[30]={false};
std::queue<int>q;
int dist[30]={0};
q.push(0);
visited[0]=true;
while(!q.empty()){
int x = q.front();
if(x==29)
return dist[x];
for(int i=1;i<=6;++i){
if(!visited[x+i] && (x+i)<=30){
if(g[x+i]==0)
g[x+i] =(x+i);
visited[x+i]=true;
dist[g[x+i]] =dist[i]+1;
std::cout<<x<<" "<<g[x+i]<<" "<<dist[g[x+i]]<<"\n";
q.push(g[x+i]);
}
}
q.pop();
}
return -1;
}
int main()
{
int T;
std::cin>>T;
while(T--){
int n;
std::cin>>n;
int game[30]={0};
for(int i=0;i<n;++i){
int a,b;
std::cin>>a>>b;
game[a-1]=b-1;
}
std::cout<<BFS(game)<<"\n";
}
}