Advertisement
AsimKPrasad

Rat_in_a_Maze

Nov 22nd, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 KB | None | 0 0
  1. //Author : Asim K Prasad
  2. //Program to solve Rat-In-A-Maze problem and then print a valid path too
  3. //The program tracks for a path from top-left corner to the bottom-right corner ONLY
  4. //So the (0,0) && (n-1,m-1) cells should be 1 for a valid path to exist
  5.  
  6. #include<stdio.h>
  7.  
  8. #define wl(n) while(n--)
  9. #define fl(i,a,b) for(i=a; i<b; i++)
  10. #define rev(i,a,b) for(i=a; i>=b; i--)
  11. #define scan(n) scanf("%d", &n)
  12. #define scans(s) scanf("%s", s)
  13. #define scanc(c) scanf("%c", &c)
  14. #define scanp(f) scanf("%f", &f)
  15. #define print(n) printf("%d\n", n)
  16. #define prints(s) printf("%s\n", s)
  17. #define printc(c) printf("%c\n", c)
  18. #define printp(f) printf("%f\n", f)
  19. #define nline printf("\n")
  20. #define mclr(strn) strn.clear()
  21. #define ignr cin.ignore()
  22. #define MOD 1000000007
  23. #define ll long long int
  24. #define u64 unsigned long long int
  25. #define true 1
  26. #define false 0
  27. #define PB push_back
  28. #define SZ size
  29.  
  30. int n, m;
  31. int sx, sy;
  32. int tx, ty;
  33.  
  34. void ans(int ans_mat[100][100])
  35. {
  36.     int i, j;
  37.     fl(i,0,n)
  38.     {
  39.         fl(j,0,m)
  40.              printf(" %d ", ans_mat[i][j]);
  41.         nline;
  42.     }
  43. }
  44.  
  45. int isOk(int maze[100][100], int x, int y)
  46. {
  47.     if(x>=0 && x<n && y>=0 && y<m && maze[x][y]==1)
  48.         return true;
  49.     return false;
  50. }
  51. int Magic(int maze[100][100], int x, int y, int ans_mat[100][100])
  52. {
  53.      if(x==n-1 && y==m-1)
  54.      {
  55.          ans_mat[x][y] = 1;
  56.         printf("The solution path : ");
  57.         nline;
  58.         ans(ans_mat);
  59.          return 1;
  60.      }
  61.  
  62.      if(isOk(maze, x, y)==1)
  63.      {
  64.          ans_mat[x][y] = 1;
  65.          //Checking the adjacent blocks for a valid path and then
  66.          //recursively checking adjacent
  67.          //blocks for their valid paths
  68.          if(isOk(maze,x+1,y+1) && ans_mat[x+1][y+1]==0 && Magic(maze,x+1,y+1,ans_mat)==1 )
  69.                 return 1;
  70.          if(isOk(maze,x+1,y) && ans_mat[x+1][y]==0 && Magic(maze,x+1,y,ans_mat)==1)
  71.                 return 1;
  72.          if(isOk(maze,x,y+1) && ans_mat[x][y+1]==0 && Magic(maze,x,y+1,ans_mat)==1)
  73.                 return 1;
  74.          if(isOk(maze,x-1,y) && ans_mat[x-1][y]==0 && Magic(maze,x-1,y,ans_mat)==1  )
  75.                 return 1;
  76.          if(isOk(maze,x-1,y+1) && ans_mat[x-1][y+1]==0 && Magic(maze,x-1,y+1,ans_mat)==1 )
  77.                 return 1;
  78.          if(isOk(maze,x+1,y-1) && ans_mat[x+1][y-1]==0 && Magic(maze,x+1,y-1,ans_mat)==1)
  79.                  return 1;
  80.          if(isOk(maze,x,y-1) && ans_mat[x][y-1]==0 && Magic(maze,x,y-1,ans_mat)==1)
  81.                 return 1;
  82.          if(isOk(maze,x-1,y-1) && ans_mat[x-1][y-1]==0 && Magic(maze,x-1,y-1,ans_mat)==1)
  83.                  return 1;
  84.          ans_mat[x][y] = 0;  //Setting the block to 0 if there is no
  85.                                 //valid path through it
  86.          return 0;
  87.      }
  88.      return 0;
  89. }
  90.  
  91. int arr[1000006];
  92. int reveal(int maze[100][100])
  93. {
  94.      int ans_mat[100][100];
  95.      int i, j;
  96.      int k=0;
  97.      if(Magic(maze,0,0,ans_mat)==false)
  98.      {
  99.            printf("Blocked :( ");   //Blocked condition
  100.            nline;
  101.            return false;
  102.      }
  103.      //printf("valid");
  104.      nline;
  105.      return true;
  106. }
  107.  
  108. int maze[100][100];
  109.  
  110. int main()
  111. {
  112.     printf("Enter the number of rows and columns : ");
  113.         scan(n); scan(m);
  114.         int i ,j;
  115.         printf("Enter the matrix ('0' for blocked path, '1' for allowed path) : \n");
  116.         fl(i,0,n)
  117.             fl(j,0,m)
  118.                 scan(maze[i][j]);
  119.         reveal(maze);
  120.         return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement