Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. /* Question 4: Maze Problem (Bonus)
  2. * Starting point is m[0][0], need to find a path go to m[9][9]. 0 means OK,
  3. * 1 means cannot go there, boundary is 0 and 9, cannot go beyond boundary.
  4. * Each step can be made horizontally or vertically for one more grid (diagonal
  5. * jump is not allowed).
  6. * Your program should print a series of grid coordinates that start from m[0][0]
  7. * and go to m[9][9]
  8. * Hint: No need to find the shortest path, only need to find one path that gets
  9. * you to desitination.
  10. */
  11.  
  12. const int DOWN = 0;
  13. const int UP = 1;
  14. const int LEFT = 2;
  15. const int RIGHT = 3;
  16.  
  17. // Prints a path to the bottom right corner of the map described by m.
  18. void maze() {
  19. int x = 0;
  20. int y = 0;
  21. int last_move = RIGHT;
  22. int m[10][10]
  23. { 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,
  24. 0, 1, 0, 0, 0, 0, 1, 1, 1, 1,
  25. 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
  26. 0, 0, 0, 1, 1, 0, 1, 1, 1, 1,
  27. 0, 1, 1, 1, 0, 0, 1, 0, 0, 1,
  28. 1, 1, 1, 1, 0, 1, 1, 0, 0, 1,
  29. 1, 1, 0, 1, 0, 0, 0, 0, 0, 1,
  30. 1, 1, 0, 0, 1, 1, 1, 1, 0, 1,
  31. 0, 1, 1, 0, 1, 1, 1, 1, 0, 1,
  32. 0, 1, 1, 1, 1, 1, 1, 1, 0, 0
  33. };
  34. std::cout << "(row: " << 0 << ", column: " << 0 << ")\n";
  35.  
  36. while ((x != 9) && (y != 9)) {
  37.  
  38. // Check move right.
  39. if ((m[y][x+1] == 0) && (last_move != LEFT)) {
  40. x++;
  41. last_move = RIGHT;
  42. } // Check move down.
  43. else if ((m[y+1][x] == 0) && (last_move != UP)) {
  44. y++;
  45. last_move = DOWN;
  46. } // Check move up.
  47. else if ((m[y-1][x] == 0) && (last_move != DOWN)) {
  48. y--;
  49. last_move = UP;
  50. } else { // Move left as last resort.
  51. x--;
  52. last_move = LEFT;
  53. }
  54. std::cout << "(row: " << y << ", column: " << x << ")\n";
  55. }
  56. std::cout << "(row: " << 9 << ", column: " << 9 << ")\n";
  57.  
  58. }
  59.  
  60. int main() {
  61. maze();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement