Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <windows.h>
  4. using namespace std;
  5. int lastmove = 0;
  6.  
  7. // Returns true if you can reach the bottom-right (otherwise returns false).
  8. // You can move up, down, left, or right. You cannot move diagonally.
  9. // 1 represents a wall. You cannot go through a wall.
  10. bool winnable(int maze[5][5], int m, int n) {
  11.  
  12.  
  13.  
  14.  
  15.  
  16. // Testing moves in right, down, left and up directions
  17. // If the previous move is the opposite vertically or horizontally
  18. // It is not valid
  19. cout << m << " " << n << endl;
  20. cout << maze[m][n] << endl;
  21. Sleep(1000);
  22.  
  23.  
  24.  
  25. if (n<5&& maze[m][n+1]!=1 && lastmove!=3){
  26.  
  27. if(m==5&&n==5)
  28. return true;
  29.  
  30. lastmove = 1;
  31. winnable(maze, m, ++n);
  32. }
  33. if (m<5&&maze[m+1][n]!=1 && lastmove!=4){
  34.  
  35. if(m==5&&n==5)
  36. return true;
  37. lastmove = 2;
  38. winnable(maze, m+1, n);
  39. }
  40. if (n>-1&& maze[m][n-1]!=1 && lastmove!=1){
  41.  
  42. if(m==5&&n==5)
  43. return true;
  44. lastmove = 3;
  45. winnable(maze, m, n-1);
  46. }
  47. if (m>-1&&maze[m-1][n]!=1 && lastmove!=2){
  48.  
  49. if(m==5&&n==5)
  50. return true;
  51. lastmove = 4;
  52. winnable( maze, m-1, n);
  53. }
  54. if (m==0&&n==0)
  55. return false;
  56. maze[m][n] =1;
  57. lastmove = lastmove + 2 % 4;
  58. winnable(maze,m,n);
  59.  
  60.  
  61.  
  62. }
  63.  
  64. int main() {
  65. int maze1[5][5] = {{0, 0, 0, 1, 1},
  66. {1, 1, 0, 1, 1},
  67. {0, 0, 0, 1, 1},
  68. {0, 1, 1, 1, 1},
  69. {0, 0, 0, 0, 0}};
  70.  
  71. int maze2[5][5] = {{0, 0, 0, 0, 1},
  72. {0, 1, 0, 1, 1},
  73. {0, 1, 1, 0, 0},
  74. {0, 0, 0, 1, 0},
  75. {1, 0, 1, 0, 0}};
  76.  
  77. int maze3[5][5] = {{0, 0, 0, 0, 1},
  78. {0, 1, 1, 0, 1},
  79. {0, 1, 1, 0, 1},
  80. {0, 0, 1, 1, 1},
  81. {1, 0, 0, 0, 0}};
  82.  
  83. int maze4[5][5] = {{0, 1, 0, 0, 0},
  84. {0, 1, 0, 1, 0},
  85. {0, 0, 1, 0, 0},
  86. {0, 1, 0, 1, 0},
  87. {0, 0, 0, 1, 0}};
  88.  
  89. cout << boolalpha; // to print bool values as true/false instead of 1/0
  90. cout << winnable(maze1, 0, 0) << endl;
  91. cout << winnable(maze2, 0, 0) << endl;
  92. cout << winnable(maze3, 0, 0) << endl;
  93. cout << winnable(maze4, 0, 0) << endl;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement