-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnakesAndLadders.cpp
91 lines (86 loc) · 2.31 KB
/
SnakesAndLadders.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
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100005];
bool visited[100005]={false};
int dist[101];
void BFS_SnL(int source)
{
queue<int> q;
q.push(source);
visited[source] = true;
dist[source]=0;
while(!q.empty())
{
int parent = q.front();
q.pop();
for(int i = 0; i < adj[parent].size(); i++)
{
if(!visited[adj[parent][i]])
{
q.push(adj[parent][i]);
visited[adj[parent][i]] = true;
// As distance of any child/neighbour would be one more than the parent.
dist[adj[parent][i]]=dist[parent]+1;
}
if(adj[parent][i]==100) return;
}
}
}
void solveSnL()
{
int n,m,start,end;
cin>>n;
for(int i=0; i<n; i++)
{
cin >> start >> end;
// Making edges for ladders.
adj[start].push_back(end);
}
cin>>m;
for(int i=0; i<m; i++)
{
cin >> start >> end;
// Making edges for snakes.
adj[start].push_back(end);
}
// For making connection between a point and the next 6 points.
for(int i=1; i<=100; i++)
{
// If there's a ladder or snake on any of the points then it would directly take us to other end.
// So we don't need to connect this point to the next 6 points.
// Just need to connect it to the other end of snake or ladder.
if(adj[i].size()==1) continue;
for(int j=i+1; j<=i+6 && j<=100; j++)
{
// If there's a ladder or snake at a point then it directly takes us to the other end.
// So instead of connecting point i to j we connect point i to the other end.
if(adj[j].size()==1) // We can tell that there's a snake or ladder at a point if there's only one connection.
adj[i].push_back(adj[j][0]); // adj[3]=[22]
else
adj[i].push_back(j); // adj[2]=[]
}
}
// BFS to calculate distance.
BFS_SnL(1);
cout<<dist[100]<<endl;
}
// To initialize everything for performing BFS.
void init_SnL()
{
for(int i=1; i<=100; i++)
{
adj[i].clear();
dist[i]=-1;
visited[i]=false;
}
}
int main() {
int t=1;
cin>>t;
while(t--)
{
init_SnL();
solveSnL();
}
return 0;
}