Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #define n 4
  4. using namespace std;
  5.  
  6. bool mazeUtil(int maze[n][n], int x, int y, int sol[n][n]);
  7. bool isValid(int maze[n][n], int x, int y);
  8. bool mazeWorks(int maze[n][n]);
  9.  
  10. void printSol(int sol[n][n])
  11. {
  12. for (int i = 0; i < n; i++)
  13. {
  14. for(int j = 0; j < n; j++)
  15. {
  16. cout << sol[i][j] + "\n";
  17. }
  18. }
  19. }
  20.  
  21.  
  22. bool isValid(int maze[n][n], int x, int y)
  23. {
  24. if(x >= 0 && x < n && y >= 0 && y < n && maze[n][n] == 1)
  25. {
  26. return true;
  27. }
  28. else{
  29. return false;
  30. }
  31. }
  32.  
  33. bool mazeWorks(int maze[n][n])
  34. {
  35. int sol[n][n]={{0,0,0,0},
  36. {0,0,0,0},
  37. {0,0,0,0},
  38. {0,0,0,0}};
  39. };
  40. if(mazeUtil(maze, 0, 0, sol) == false)
  41. {
  42. cout << "no solution";
  43. return false;
  44. }
  45. printSol(sol);
  46. return true;
  47. }
  48.  
  49. bool mazeUtil(int maze[n][n], int x, int y, int sol[n][n])
  50. {
  51. if (x== n-1 && y == n-1)
  52. {
  53. sol[x][y]=1;
  54. return true;
  55. }
  56. if(isValid(maze, x, y) == true)
  57. {
  58. sol[x][y]=1;
  59. if(mazeUtil(maze, x+1, y, sol)== true)
  60. {
  61. return true;
  62. }
  63. if(mazeUtil(maze, x, y+1, sol) == true)
  64. {
  65. return true;
  66. }
  67. else
  68. {
  69. sol[x][y]=0;
  70. return false;
  71. }
  72. }
  73. return false;
  74. }
  75. }
  76.  
  77. int main()
  78. {
  79. int maze[n][n] ={ { 1,0,0,0},
  80. {1,1,0,1},
  81. {0,1,0,0},
  82. {1,1,1,1}
  83. };
  84. mazeWorks(maze);
  85. return 0;
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement