Advertisement
juanjo12x

Rat_Maze_1_Solution

May 4th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.36 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. // Maze size
  4. #define N 4
  5.  
  6. int solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);
  7.  
  8. /* A utility function to print solution matrix sol[N][N] */
  9. void printSolution(int sol[N][N])
  10. {
  11. int i,j;
  12.     for ( i = 0; i < N; i++)
  13.     {
  14.         for (j = 0; j < N; j++)
  15.             printf(" %d ", sol[i][j]);
  16.         printf("\n");
  17.     }
  18. }
  19.  
  20. /* A utility function to check if x,y is valid index for N*N maze */
  21. int isSafe(int maze[N][N], int x, int y)
  22. {
  23.     // if (x,y outside maze) return false
  24.     if(x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)
  25.         return 1;
  26.  
  27.     return 0;
  28. }
  29.  
  30. /* This function solves the Maze problem using Backtracking.  It mainly uses
  31. solveMazeUtil() to solve the problem. It returns false if no path is possible,
  32. otherwise return true and prints the path in the form of 1s. Please note that
  33. there may be more than one solutions, this function prints one of the feasible
  34. solutions.*/
  35. int solveMaze(int maze[N][N])
  36. {
  37.     int sol[N][N] = { {0, 0, 0, 0},
  38.         {0, 0, 0, 0},
  39.         {0, 0, 0, 0},
  40.         {0, 0, 0, 0}
  41.     };
  42.  
  43.     if(solveMazeUtil(maze, 0, 0, sol) == 0)
  44.     {
  45.         printf("Solution doesn't exist");
  46.         return 0;
  47.     }
  48.  
  49.     printSolution(sol);
  50.     return 1;
  51. }
  52.  
  53. /* A recursive utility function to solve Maze problem */
  54. int solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N])
  55. {
  56.     // if (x,y is goal) return true
  57.     if(x == N-1 && y == N-1)
  58.     {
  59.         sol[x][y] = 1;
  60.         return 1;
  61.     }
  62.  
  63.     // Check if maze[x][y] is valid
  64.     if(isSafe(maze, x, y) == 1)
  65.     {
  66.         // mark x,y as part of solution path
  67.         sol[x][y] = 1;
  68.  
  69.         /* Move forward in x direction */
  70.         if (solveMazeUtil(maze, x+1, y, sol) == 1)
  71.             return 1;
  72.  
  73.         /* If moving in x direction doesn't give solution then
  74.            Move down in y direction  */
  75.         if (solveMazeUtil(maze, x, y+1, sol) == 1)
  76.             return 1;
  77.  
  78.         /* If none of the above movements work then BACKTRACK:
  79.             unmark x,y as part of solution path */
  80.         sol[x][y] = 0;
  81.         return 0;
  82.     }  
  83.  
  84.     return 0;
  85. }
  86.  
  87. // driver program to test above function
  88. int main()
  89. {
  90.     int maze[N][N]  =  { {1, 0, 0, 0},
  91.         {1, 1, 0, 1},
  92.         {0, 1, 0, 0},
  93.         {1, 1, 1, 1}
  94.     };
  95.  
  96.     solveMaze(maze);
  97.     getchar();
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement