Compatibility Turbo C
   1: #include<conio.h>   2: #include<stdio.h>3: #define SIZE 15
   4: #include<stdlib.h>5: void main()
   6: {7: int maze[SIZE][SIZE],mark[SIZE][SIZE],stack[SIZE][3];
8: static int
   9: move[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};  10:  11: int i,j,m,n,top,mov,g,h;
  12: clrscr();13: printf("enter size");
14: scanf("%d%d",&m,&n);
15: for(i=1;i<=m;i++)
  16: {17: for(j=1;j<=n;j++)
  18: {19: scanf("%d",&maze[i][j]);
  20: }  21: }22: for(i=0;i<=n+1;i++)
  23: maze[0][i]=1;24: for(i=0;i<=n+1;i++)
  25: maze[m+1][i]=1;26: for(i=0;i<=m+1;i++)
  27: maze[i][0]=1;28: for(i=0;i<=m+1;i++)
  29: maze[i][n+1]=1;30: for(i=1;i<=m;i++)
  31: {32: for(j=1;j<=n;j++)
  33: {  34: mark[i][j]=0;  35: }  36: }  37: mark[1][1]=1;  38: stack[0][0]=1;  39: stack[0][1]=1;  40: stack[0][2]=2;  41: top=1;42: while(top!=0)
  43: {  44: i=stack[0][0];  45: j=stack[0][1];  46: mov=stack[0][2];  47: top=top-1;48: while(mov<=7)
  49: {  50: g=i+move[mov][0];  51: h=j+move[mov][1];  52:  53: if(mark[g][h]==0&&maze[g][h]==0)
  54: {  55: mark[g][h]=1;  56: top++;  57: stack[top][0]=i;  58: stack[top][1]=j;  59: mov=-1;  60: i=g;j=h;  61: }  62: mov=mov+1;63: if(g==m&&h==n)
  64: {65: printf("
  66: path made by the rat is");67: for(i=1;i<=top;i++)
68: printf("
  69: %d %d",stack[i][0],stack[i][1]);70: printf("
  71: %d %d",m,n);  72: getch();  73: exit(0);  74: }  75: }  76: }  77: }  78:    79:    80:    81:    82:             
No comments:
Post a Comment
please write your mail id for contact: