0% found this document useful (0 votes)
31 views5 pages

Snakes and Ladders - The Quickest Way Up - Lab 7 FODSA Question - Contests - HackerRank

Uploaded by

amansnh1942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Snakes and Ladders - The Quickest Way Up - Lab 7 FODSA Question - Contests - HackerRank

Uploaded by

amansnh1942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6/30/24, 10:06 PM Snakes and Ladders: The Quickest Way Up | Lab 7 FODSA Question | Contests | HackerRank

HackerRank | Prepare Certify Compete Apply  Search   

All Contests  Lab 7 FODSA  Snakes and Ladders: The Quickest Way Up

Snakes and Ladders: The Quickest


Way Up
 locked

Problem Submissions Leaderboard

Markov takes out his Snakes and Ladders game, stares at the board and wonders: "If I can always roll the die to whatever number I
want, what would be the least number of rolls to reach the destination?"

Rules The game is played with a cubic die of faces numbered to .

1. Starting from square , land on square with the exact roll of the die. If moving the number rolled would place the player
beyond square , no move is made.

2. If a player lands at the base of a ladder, the player must climb the ladder. Ladders go up only.

3. If a player lands at the mouth of a snake, the player must go down the snake and come out through the tail. Snakes go down only.

Function Description

Complete the quickestWayUp function in the editor below. It should return an integer that represents the minimum number of
moves required.

quickestWayUp has the following parameter(s):

ladders: a 2D integer array where each contains the start and end cell numbers of a ladder

snakes: a 2D integer array where each contains the start and end cell numbers of a snake

Input Format

The first line contains the number of tests, .

For each testcase:


- The first line contains , the number of ladders.
- Each of the next lines contains two space-separated integers, the start and end of a ladder.
- The next line contains the integer , the number of snakes.
- Each of the next lines contains two space-separated integers, the start and end of a snake.

Constraints

The board is always with squares numbered to .


Neither square nor square will be the starting point of a ladder or snake.
A square will have at most one endpoint from either a snake or a ladder.

Output Format

For each of the t test cases, print the least number of rolls to move from start to finish on a separate line. If there is no solution,
print -1 .

Sample Input

https://www.hackerrank.com/contests/lab-7-fodsa/challenges/the-quickest-way-up/copy-from/1360618115 1/5
6/30/24, 10:06 PM Snakes and Ladders: The Quickest Way Up | Lab 7 FODSA Question | Contests | HackerRank

2
3
32 62
42 68
12 98
7
95 13
97 25
93 37
79 27
75 19
49 47
67 17
4
8 52
6 80
26 42
2 72
9
51 19
39 11
37 29
81 3
59 5
79 23
53 7
43 33
77 21

Sample Output

3
5

Explanation

For the first test:

The player can roll a and a to land at square . There is a ladder to square . A roll of ends the traverse in rolls.

For the second test:

The player first rolls and climbs the ladder to square . Three rolls of get to square . A final roll of lands on the target
square in total rolls.

  

Submissions: 44
Max Score: 100

Rate This Challenge:



More

C++20   ⚙
1 ▾#include <bits/stdc++.h>
2
3 using namespace std;
4
5 string ltrim(const string &);
6 string rtrim(const string &);
7 vector<string> split(const string &);
8
9 ▾/*
10 * Complete the 'quickestWayUp' function below.
11 *
12 * The function is expected to return an INTEGER.
13 * The function accepts following parameters:
14 * 1. 2D_INTEGER_ARRAY ladders
15 * 2. 2D_INTEGER_ARRAY snakes
16 */

https://www.hackerrank.com/contests/lab-7-fodsa/challenges/the-quickest-way-up/copy-from/1360618115 2/5
6/30/24, 10:06 PM Snakes and Ladders: The Quickest Way Up | Lab 7 FODSA Question | Contests | HackerRank
17
18 ▾int quickestWayUp(vector<vector<int>> ladders, vector<vector<int>> snakes) {
19 queue<int> q;
20 vector<bool> visit(120,false);
21 vector<int> dist(120,-1);
22 q.push(1);
23 ▾ dist[1] = 0;
24 ▾ visit[1] = true;
25 vector<int> last;
26 ▾ for(int i=0;i<=120;i++){
27 last.push_back(i);
28 }
29 ▾ for(auto i : snakes){
30 ▾ last[i[0]] = i[1];
31 }
32 unordered_map<int,int> l;
33 ▾ for(auto i : ladders){
34 ▾ last[i[0]] = i[1];
35 }
36 ▾ while(!q.empty()){
37 int temp = q.front();
38 q.pop();
39
40 ▾ for(int i=1;i<7;i++){
41 int off = temp + i;
42 ▾ if(off > 0 && off <= 100 && visit[off]==false){
43 ▾ visit[off] = true;
44 ▾ dist[off] = dist[temp] + 1;
45 ▾ off = last[off];
46 ▾ visit[off] = true;
47 ▾ dist[off] = dist[temp] + 1;
48 q.push(off);
49 if(off == 100) break;
50 }
51 }
52 }
53 ▾ return dist[100];
54 }
55
56 int main()
57 ▾{
58 ofstream fout(getenv("OUTPUT_PATH"));
59
60 string t_temp;
61 getline(cin, t_temp);
62
63 int t = stoi(ltrim(rtrim(t_temp)));
64
65 ▾ for (int t_itr = 0; t_itr < t; t_itr++) {
66 string n_temp;
67 getline(cin, n_temp);
68
69 int n = stoi(ltrim(rtrim(n_temp)));
70
71 vector<vector<int>> ladders(n);
72
73 ▾ for (int i = 0; i < n; i++) {
74 ▾ ladders[i].resize(2);
75
76 string ladders_row_temp_temp;
77 getline(cin, ladders_row_temp_temp);
78
79 vector<string> ladders_row_temp = split(rtrim(ladders_row_temp_temp));
80
81 ▾ for (int j = 0; j < 2; j++) {
82 ▾ int ladders_row_item = stoi(ladders_row_temp[j]);
83
84 ▾ ladders[i][j] = ladders_row_item;
85 }
86 }
87
88 string m_temp;
89 getline(cin, m_temp);

https://www.hackerrank.com/contests/lab-7-fodsa/challenges/the-quickest-way-up/copy-from/1360618115 3/5
6/30/24, 10:06 PM Snakes and Ladders: The Quickest Way Up | Lab 7 FODSA Question | Contests | HackerRank
90
91 int m = stoi(ltrim(rtrim(m_temp)));
92
93 vector<vector<int>> snakes(m);
94
95 ▾ for (int i = 0; i < m; i++) {
96 ▾ snakes[i].resize(2);
97
98 string snakes_row_temp_temp;
99 getline(cin, snakes_row_temp_temp);
100
101 vector<string> snakes_row_temp = split(rtrim(snakes_row_temp_temp));
102
103 ▾ for (int j = 0; j < 2; j++) {
104 ▾ int snakes_row_item = stoi(snakes_row_temp[j]);
105
106 ▾ snakes[i][j] = snakes_row_item;
107 }
108 }
109
110 int result = quickestWayUp(ladders, snakes);
111
112 fout << result << "\n";
113 }
114
115 fout.close();
116
117 return 0;
118 }
119
120 ▾string ltrim(const string &str) {
121 string s(str);
122
123 s.erase(
124 s.begin(),
125 find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
126 );
127
128 return s;
129 }
130
131 ▾string rtrim(const string &str) {
132 string s(str);
133
134 s.erase(
135 find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
136 s.end()
137 );
138
139 return s;
140 }
141
142 ▾vector<string> split(const string &str) {
143 vector<string> tokens;
144
145 string::size_type start = 0;
146 string::size_type end = 0;
147
148 ▾ while ((end = str.find(" ", start)) != string::npos) {
149 tokens.push_back(str.substr(start, end - start));
150
151 start = end + 1;
152 }
153
154 tokens.push_back(str.substr(start));
155
156 return tokens;
157 }
158

Line: 1 Col: 1

https://www.hackerrank.com/contests/lab-7-fodsa/challenges/the-quickest-way-up/copy-from/1360618115 4/5
6/30/24, 10:06 PM Snakes and Ladders: The Quickest Way Up | Lab 7 FODSA Question | Contests | HackerRank

 Upload Code as File Test against custom input Run Code Submit Code

Interview Prep | Blog | Scoring | Environment | FAQ | About Us | Support | Careers | Terms Of Service | Privacy Policy |

https://www.hackerrank.com/contests/lab-7-fodsa/challenges/the-quickest-way-up/copy-from/1360618115 5/5

You might also like