上传用户:ahhuaxin
上传日期:2013-01-26
资源大小:2k
文件大小:5k
开发平台:

C/C++

  1. #include<malloc.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #define M 6                          /*规定迷宫的行数*/
  5. #define N 4                          /*规定迷宫的列数*/
  6. int i=0;
  7. typedef struct shuju{
  8. int data;
  9. int flag;                          /*判断这个位置是否被走过,使其产生记忆效果*/
  10. }SHU;
  11. typedef struct maze{
  12. int c;
  13. int flag;
  14. int direct;
  15. int x;
  16. int y;
  17. struct maze *next;
  18. }MG;
  19. MG *top,*p;
  20. MG * pop(int bb,int xx,int yy,SHU t[M][N])     /*入栈操作*/
  21. {int i2,j2;
  22. i++;
  23. p=(MG *)malloc(sizeof(MG));
  24. p->c=bb;
  25. p->flag=1;
  26. p->direct=4;
  27. p->x=xx;
  28. p->y=yy;
  29. if(i==1)
  30.   {top=p;
  31.    top->next=NULL;
  32.     }
  33.    else
  34.     {p->next=top;
  35.      top=p;
  36.       }
  37. clrscr();
  38. for(i2=0;i2<M;i2++)
  39. {printf("n");
  40.    for(j2=0;j2<N;j2++)
  41.      if(t[i2][j2].data==8)
  42.        {textcolor(YELLOW);
  43.         cprintf("%3d",t[i2][j2].data);
  44.          }
  45.       else
  46.         {textcolor(WHITE);
  47.          cprintf("%3d",t[i2][j2].data);
  48.           }
  49.      }
  50. for(i2=0;i2<80;i2++)         /*时间延迟*/
  51. delay(1000);
  52. }
  53. int way(int x1,int y1,SHU s[M][N])     /*寻找路径函数*/
  54. { int x,y,f=0;
  55.   int i1,j1;
  56. while(top!=NULL)               /*以下是在四个方向上进行判断
  57.                                  通就入栈,不通就进行下个方向的
  58.                                  判断,如果四个方向都不通,就出栈*/
  59.    if(top->flag==1)
  60.     {x=top->x;
  61.      y=top->y;
  62.      if(x==x1&&y==y1)
  63.         { f=1;
  64.           break;
  65.             }                         /*判断右方向*/
  66.      if(top->direct==4)
  67.     {top->direct--;
  68.      if(0<=y+1&&y+1<N)               /*判断数组的下标是否超过了规定的界限*/
  69.        if(s[x][y+1].data==0&&s[x][y+1].flag==0)
  70.          pop(s[x][y+1].data=8,x,y+1,s);
  71.        }
  72.        else if(top->direct==3)        /*判断下方向*/
  73.      {top->direct--;
  74.       if(0<=x+1&&x+1<M)
  75.        if(s[x+1][y].data==0&&s[x+1][y].flag==0)
  76.         pop(s[x+1][y].data=8,x+1,y,s);
  77.          }
  78.       else if(top->direct==2)          /*判断左方向*/
  79.         {top->direct--;
  80.          if(0<=y-1&&y-1<N)
  81.           if(s[x][y-1].data==0&&s[x][y-1].flag==0)
  82.            pop(s[x][y-1].data=8,x,y-1,s);
  83.         }
  84.           else if(top->direct==1)      /*判断上方向*/
  85.         {top->direct--;
  86.          top->flag=0;
  87.          if(0<=x-1&&x-1<M)
  88.          if(s[x-1][y].data==0&&s[x-1][y].flag==0)
  89.            pop(s[x-1][y].data=8,x-1,y,s);
  90.            }
  91.        }
  92.    else
  93.      {s[top->x][top->y].data=0;
  94.       s[top->x][top->y].flag=1;
  95.       top=top->next;
  96.       clrscr();
  97.       for(i1=0;i1<M;i1++)
  98.        {printf("n");
  99.         for(j1=0;j1<N;j1++)
  100.          if(s[i1][j1].data==8)
  101.           {textcolor(YELLOW);
  102.            cprintf("%3d",s[i1][j1].data);
  103.             }
  104.            else
  105.              {textcolor(WHITE);
  106.               cprintf("%3d",s[i1][j1].data);
  107.                }
  108.           }
  109.       for(i1=0;i1<80;i1++)                     /*产生时间延迟,不同的CPU产生的延迟效果不一样*/
  110.        delay(1000);
  111.       }
  112. return f;
  113. }
  114. main()
  115. {SHU  s[M][N];
  116. int x,y,x1,y1;
  117. int i1,j1,i,j,F;
  118. for(i1=0;i1<M;i1++)
  119.      for(j1=0;j1<N;j1++)                                  /*迷宫图面,用一个数字表示*/
  120.         s[i1][j1].data=8;                                                                   
  121. for(i1=0;i1<M;i1++)
  122.   {printf("n");
  123.    for(j1=0;j1<N;j1++)
  124.     printf("%3d",s[i1][j1].data);                                                /*打印迷宫最原始的界面,用数字8表示每个        }                                                                   位置*/
  125.        }
  126. printf("nplese drink a migong --0 is yes --1 is non");
  127. for(i1=0;i1<M;i1++)
  128.   for(j1=0;j1<N;j1++)
  129.    {printf("nb[%d][%d]=",i1,j1);
  130.     scanf("%d",&s[i1][j1].data);                            /*绘制迷宫,0代表通,1代表不通*/
  131.      s[i1][j1].flag=0;
  132.      clrscr();
  133.      for(i=0;i<M;i++)
  134.      {printf("n");
  135.       for(j=0;j<N;j++)
  136.         if(s[i][j].data==0)
  137.           {textcolor(YELLOW);
  138.             cprintf("%3d",s[i][j].data);
  139.              }
  140.          else if(s[i][j].data==1)
  141.           {textcolor(RED);
  142.             cprintf("%3d",s[i][j].data);
  143.             }
  144.          else
  145.           {textcolor(WHITE);
  146.            cprintf("%3d",s[i][j].data);
  147.             }
  148.         }
  149.      }
  150. textcolor(WHITE);
  151. printf("nmi gong tu is :");
  152. for(i1=0;i1<M;i1++)
  153. {printf("n");
  154.   for(j1=0;j1<N;j1++)
  155.    printf("%3d",s[i1][j1].data);
  156.     }
  157. printf("nplease input the ru kou zuo biao(x&y)n for example input:0 0ninput:");     /*输入迷宫的入口坐标(X,Y)*/
  158.   scanf("%d %d",&x,&y);
  159. printf("please input the chu kou zuo biao(x1&y1)n for example input:3 3ninput:");    /*输入迷宫的出口坐标(X1,Y1)*/
  160.   scanf("%d %d",&x1,&y1);
  161.   printf("press any key to continue:");
  162.   getch();
  163. while(s[x][y].data!=0)                       /*判断是否有入口位置*/
  164.   {printf("Not find ru koun");
  165.     printf("please input again: ");
  166.     scanf("%d %d",x,y);
  167.      }
  168. pop(s[x][y].data=8,x,y,s);
  169. F=way(x1,y1,s);                                       /*路径进行判断函数*/
  170. textcolor(WHITE);
  171. if(F==0)
  172. cprintf("rnben mi gong mei you jie!n");
  173.   else
  174.     cprintf("rnben mi gong you  way:n");
  175. cprintf("rnpress any key to quit.n");
  176. getch();
  177. }