Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. //rat in a maze
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define N 6
  5.  
  6. void printMaze(int maze[N][N]){
  7. for(int i=0; i<N; i++){
  8. for(int j=0; j<N; j++){
  9. printf("%3d", maze[i][j]);
  10. }
  11. printf("\n");
  12. }
  13. printf("\n");
  14. }
  15.  
  16. int isSafe(int maze[N][N], int x, int y){
  17. return (x>=0 && x < N && y>=0 && y < N && maze[x][y]==1);
  18. }
  19.  
  20. int mazeSolver(int maze[N][N], int x, int y){
  21. if(x==N-1 && y==N-1){
  22. maze[x][y]=2;
  23. return 1;
  24. }
  25.  
  26. if(isSafe(maze, x, y)){
  27. maze[x][y]=2;
  28.  
  29. if(mazeSolver(maze, x+1, y)){
  30. return 1;
  31. }
  32.  
  33. if(mazeSolver(maze, x, y+1)){
  34. return 1;
  35. }
  36. maze[x][y]=0;
  37. return 0;
  38.  
  39. }
  40. return 0;
  41. }
  42.  
  43. void mazeSolve(){
  44. int maze[N][N] = {
  45. {1, 1, 1, 1, 0, 0},
  46. {0, 0, 1, 0, 0, 0},
  47. {1, 1, 1, 1, 1, 1},
  48. {1, 0, 0, 1, 0, 0},
  49. {1, 1, 1, 1, 1, 1},
  50. {0, 0, 0, 0, 0, 1}
  51. };
  52. if(mazeSolver(maze, 0, 0)==0){
  53. printf("No solution\n");
  54. return;
  55. }else{
  56. printMaze(maze);
  57. }
  58. return;
  59. }
  60.  
  61. int main(){
  62. mazeSolve();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement