Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have submitted my code #185

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions Snake Ladder Problem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <iostream>
#include <queue>

using namespace std;



struct queueEntry {

int v;

int dist;
};



int getMinDiceThrows(int move[], int N)
{



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]) {



queueEntry a;

a.dist = (qe.dist + 1);

visited[j] = true;




if (move[j] != -1)

a.v = move[j];

else

a.v = j;

q.push(a);

}

}

}




return qe.dist;
}


int main()
{


int N = 30;

int moves[N];

for (int i = 0; i < N; i++)

moves[i] = -1;


// 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;


cout << "Min Dice throws required is "

<< getMinDiceThrows(moves, N);

return 0;
}