-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSnakeLadder.cpp
More file actions
66 lines (66 loc) · 976 Bytes
/
SnakeLadder.cpp
File metadata and controls
66 lines (66 loc) · 976 Bytes
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
#include<bits\stdc++.h>
#define ll long long
using namespace std;
struct queueEntry
{
int v;
int d;
};
int minmove(int N,int moves[])
{
bool *visited=new bool[N];
for(int i=0;i<N;i++)
{
visited[i]=false;
}
queue<queueEntry> q;
visited[0]=true;
queueEntry s={0,0};
q.push(s);
queueEntry qe;
while(!q.empty())
{
qe=q.front();
int v=qe.v;
if(v==N-1)
{
break;
}
q.pop();
for(int j=v+1;j<=(v+6)&& j<N;j++)
{
if(visited[j]==false)
{
queueEntry a;
a.d=(qe.d+1);
visited[j]=true;
if(moves[j]!=-1)
{
a.v=moves[j];
}
else
a.v=j;
q.push(a);
}
}
}
return qe.d;
}
int main()
{
int n=30;
int moves[30];
memset(moves,-1,sizeof(moves));
//LADDERS
moves[2]=21;
moves[4]=7;
moves[10]=25;
moves[19]=28;
//SNAKES
moves[26]=0;
moves[20]=8;
moves[16]=3;
moves[18]=6;
int x=minmove(n,moves);
cout<<"minmoves of dice="<<x<<endl;
}