-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsnakes_and_ladders.cpp
123 lines (104 loc) · 3.39 KB
/
snakes_and_ladders.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// #include <bits/stdc++.h>
// using namespace std;
// // board {{-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},{-1,35,-1,-1,13,-1},{-1,-1,-1,-1,-1,-1},{-1,15,-1,-1,-1,-1}}
// class Snake {
// public:
// int vertex, moves;
// Snake(){}
// Snake(int v, int m) : vertex(v), moves(m) {}
// };
// auto convertBoardTo1D(vector<vector<int>> &board) {
// int r = board.size(), c = board[0].size(), k = 1;
// vector<int> boardOneD(r * c, 0);
// bool leftToRight = true;
// for(int i=r-1; i>=0; i--) {
// if(leftToRight)
// for(int j=0; j<c; j++) //leftToRight
// boardOneD[k++] = board[i][j];
// else
// for(int j=c-1; j>=0; j--) //rightToLeft
// boardOneD[k++] = board[i][j];
// leftToRight = !leftToRight;
// }
// return boardOneD;
// }
// int snakesAndLadders(vector<vector<int>>& board) {
// if(board.size() == 0) return 0;
// auto boardOneD = convertBoardTo1D(board);
// auto boardSize = boardOneD.size();
// // for(auto ele : boardOneD) cout<<ele<<" ";
// vector<bool> visited(boardSize + 1, false);
// queue<Snake> Q;
// Snake s(1, 0); //start state
// Q.push(s);
// Snake element;
// while(!Q.empty()) {
// // auto &[vertex, moves] = Q.front();
// element = Q.front();
// cout<<element.vertex<<" "<<element.moves<<"\n";
// Q.pop();
// // visited[element.vertex] = true;
// if(element.vertex == boardSize) break;
// for(int j=element.vertex+1; j<=element.vertex+6 and j<=boardSize; ++j) {
// if(!visited[j]) {
// visited[j] = true;
// // int cvertex = j, cmoves = 0;
// Snake cele;
// cele.moves = element.moves + 1;
// if(boardOneD[j] != -1)
// cele.vertex = boardOneD[j];
// else
// cele.vertex = j;
// Q.push(cele);
// }
// }
// }
// if(element.vertex != boardSize) return -1;
// else return element.moves;
// }
// int main() {
// vector<vector<int>> board = {{-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},{-1,35,-1,-1,13,-1},{-1,-1,-1,-1,-1,-1},{-1,15,-1,-1,-1,-1}};
// cout<<snakesAndLadders(board);
// }
class Solution {
public:
int snakesAndLadders(vector<vector<int>>& board) {
int n = board.size(), d = n*n, moves = 0;
queue<int> q;
q.push(1);
vector<bool> vis(d+1,0);
vis[1]=1;
while(q.size()!=0)
{
int z = q.size();
for(int k=0; k<z; k++)
{
int u = q.front();
q.pop();
if(u == d)
return moves;
for(int i=1; i<=6; i++)
{
if(u+i < d+1 && !vis[u+i])
{
//mark it visited here so that if tail lies here don't take that snake in future
vis[u+i] = 1;
//extract row number and column number from current position
int x = (u+i-1) / n ;
int y = (u+i-1) % n;
//adjust according to given format i.e. boustrophedonically
x = n-x-1;
if(x%2==n%2)
y = n-y-1;
if(board[x][y]!=-1)
q.push(board[x][y]);
else
q.push(u+i);
}
}
}
moves++;
}
return -1;
}
};