fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // No input is there, source 0, cells are 1-30
  6. int targets [32];
  7. for ( int i = 0; i <= 30; i++ )targets [ i ] = i;
  8.  
  9. // targets[2] = 21;
  10. // targets[4] = 7;
  11. // targets[10] = 25;
  12. // targets[19] = 28;
  13. // Snakes
  14. // targets[26] = 0;
  15. // targets[20] = 8;
  16. // targets[16] = 3;
  17. // targets[18] = 6;
  18.  
  19.  
  20. //Use BFS to find the minimum moves
  21. //While traversing go for moves and skip the snakes cells.
  22. vector < bool > vis (32, false);
  23. queue < pair < int, int > > qu;
  24. qu.push({0, 0});
  25. vis [ 0 ] = true;
  26.  
  27. while(!qu.empty()){
  28. auto cur = qu.front();
  29. qu.pop();
  30.  
  31. for ( int move = 1; move <= 6; move++ ){
  32. int nextPos = cur.first + move;
  33. if (nextPos > 30 || vis [ nextPos] )continue;
  34. if (targets [ nextPos] > nextPos ) {
  35. nextPos = targets [ nextPos ];
  36. }
  37. if (nextPos == 30 ){
  38. cout<<cur.second+1<<"\n";
  39. return 0;
  40. }
  41. vis [ nextPos ] = true;
  42. qu.push({nextPos, cur.second + 1});
  43. }
  44. }
  45.  
  46. cout<<"No path found!";
  47. return 0;
  48. }
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
5