-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path909. Snakes and Ladders.cpp
66 lines (64 loc) · 1.61 KB
/
909. 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
class Solution {
public:
int snakesAndLadders(vector<vector<int>>& board) {
int n=board.size();
int size=n*n;
cout<<size<<endl;
int arr[size+1];
memset(arr,0,sizeof(arr));
int k=1;
bool flag=false;
bool vis[size+1];
memset(vis,false,sizeof(vis));
for(int i=n-1;i>=0;i--)
{
if(!flag)
{
for(int j=0;j<n;j++)
{
arr[k]=board[i][j];
k++;
}
}
else
{
for(int j=n-1;j>=0;j--)
{
arr[k]=board[i][j];
k++;
}
}
flag=!flag;
}
cout<<arr[size]<<endl;
if(arr[size]!=-1)
return -1;
queue<pair<int,int>>q;
q.push({1,0});
while(!q.empty())
{
int x=q.front().first;
int cost=q.front().second;
q.pop();
if(x==size)
return cost;
for(int i=1;i<=6;i++)
{
int y=x+i;
if(y<=size )
{
if(arr[y]!=-1)
{
y=arr[y];
}
if(vis[y]==false)
{
q.push({y,cost+1});
vis[y]=true;
}
}
}
}
return -1;
}
};