Snake & Ladder Problem

Here is my approach: https://ide.codingblocks.com/#/s/24207
I’m trying to solve this question: https://hack.codingblocks.com/contests/c/511/766

The answer is 3 but I’m getting it as 7 and I cant figure out why. Please guide if I have made any mistakes.

I have updated your code. Check this out. This making edge part was creating an issue. BFS thing was perfect.
https://ide.codingblocks.com/#/s/24255

Thank you!
Could you please explain why we do not add the steps traversed by u + dice in calculating v? Which is: v = u (current position) + dice (move forward) + g->movement[u+dice] (jump)

    for(dice = 1; dice <= 6; dice++)
  	{
  	    if(g->movement[u+dice]==0)
  		    v = u + dice;
  		else
  		    v=g->movement[u+dice];
  		
  		g->addEdge(u, v);
  	}

g->movement[u+dice] is the final position of player itself, so no need to add u+dice.

I applied the approach that Prateek Sir used in the video:

Please see line 97. I totally understand your solution and it makes sense. When I was seeing this video, that made sense too. Why is the code not working when tried with Prateek Sir’s calculation?

in the video, board[34]=-22, board[18]=11.
board[i]=j means we move j steps from position i. In the question you used board[start]=end, thats why my logic will work. If you use board[start]=end-start, then you can use prateek sir’s code.

1 Like

Thank you for pointing out my mistake! Makes sense.

1 Like

how you are able to take screenshot , as i cant