-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathladder.cpp
82 lines (60 loc) · 1.41 KB
/
ladder.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
#include<bits/stdc++.h>
using namespace std ;
class Ladder{
unordered_map<int,int> ladderMapping;
protected:
bool isValidBlock(int blockNumber){
return blockNumber>=1 && blockNumber<=100 ;
}
public:
Ladder(){
int defaultLadder[9][2] ={
{4,14},
{9,31},
{21,42},
{28,84},
{36,44},
{51,67},
{71,91},
{80,100},
};
for(int i=0 ;i<9 ;i++){
int from = defaultLadder[i][0] ;
int to = defaultLadder[i][1] ;
ladderMapping[from] = to ;
}
}
Ladder(int customLadder[][2] , int noOfLadders){
for(int i =0 ;i<noOfLadders ; i++){
int from = customLadder[i][0] ;
int to = customLadder[i][1] ;
ladderMapping[from] = to ;
}
}
// functions:
bool isLadder(int blockNumber){
return isValidBlock(blockNumber) &&
ladderMapping[blockNumber] ;
}
int rideLadder(int blockNumber){
if( isValidBlock(blockNumber) && isLadder(blockNumber)){
return ladderMapping[blockNumber];
}
return 0 ;
}
void addLadder(int from , int to){
// check if they are in same line
if( isValidBlock(from) && isValidBlock(to) && from/10 == to/10){
return ;
}
ladderMapping[from] = to ;
return ;
}
void removeLadder(int blockNumber){
// if ladder is not present then ignore
if( isValidBlock(blockNumber) && isLadder(blockNumber) ){
ladderMapping[blockNumber] = 0 ;
return ;
}
}
} ;