龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > C/C++开发 >

大家进来瞧瞧我编的小迷宫,望大虾不吝赐教啊

时间:2009-12-22 15:42来源:未知 作者:admin 点击:
分享到:
# define m2 50 # define n2 50 # define maxlen 200 /*栈长度*/ # define true 1 # define false 0 # define null 0 # include "stdio.h" # include "graphics.h" # include "stdlib.h" # include "dos.h" int m,n; typedef strUCt { int x,y,dir;}elemty

# define m2 50

  # define n2 50

  # define maxlen 200 /*栈长度*/

  # define true 1

  # define false 0

  # define null 0

  # include "stdio.h"

  # include "graphics.h"

  # include "stdlib.h"

  # include "dos.h"

  int m,n;

typedef strUCt

  { int x,y,dir;}elemtype;

  typedef struct

  { elemtype stack[maxlen];

   int top;

  }sqs;

  typedef struct

{ int dx,dy;}moved;

  void inimaze (int maze[][n2])

  { int i,j;

   for(i=1;i<=m;i++)

   {

   for(j=1;j<=n;j++)maze[i][j]=rand()/16383;

   }

  for (i=0,j=0;i<=m+1;i++)

   maze[i][j]=1;

  for (i=0,j=n+1;i<=m+1;i++)

   maze[i][j]=1;

  for (i=0,j=0;j<=n+1;j++)

   maze[i][j]=1;

  for (i=m+1,j=0;j<=n+1;j++)

   maze[i][j]=1;

  }

  void picture (int maze[][n2])

  {

   int i,j;

   setbkcolor(BLACK);

   for(i=0;i

   { for (j=0;j

   { if(maze[i][j]==1)

   { setfillstyle(1,LIGHTBLUE);

   bar (70+j*20,20+i*20,88+j*20,38+i*20);

   }

   else

   { setfillstyle(1,WHITE);

   bar (70+j*20,20+i*20,88+j*20,38+i*20);

   }

   }

   }

   outtextxy(90,460,"press any key to start");

   getch();

  }

void inimove(moved move[])

{ move[0].dx=0;move[0].dy=0;

   move[1].dx=0;move[1].dy=1;

   move[2].dx=1;move[2].dy=1;

   move[3].dx=1;move[3].dy=0;

   move[4].dx=1;move[4].dy=-1;

   move[5].dx=0;move[5].dy=-1;

   move[6].dx=-1;move[6].dy=-1;

   move[7].dx=-1;move[7].dy=0;

   move[8].dx=-1;move[8].dy=1;

  }

  void inistack(sqs *s)

{ s->top=-1;}

int push(sqs *s,elemtype t)

{ int i,j;

   if (s->top==maxlen-1)return(false);

   else

   {

   i=t.x;j=t.y;

   setfillstyle(1,GREEN);

   bar (70+j*20,20+i*20,88+j*20,38+i*20);

   s->stack[++s->top]=t;

   return(true);

   }

  }

  elemtype pop(sqs *s)

  

{ elemtype elem;

   if (s->top<0)

   {

   elem.x=null;

   elem.y=null;

   elem.dir=null;

   return(elem);

   }

   else

   { int i,j;

   i=s->stack[s->top].x;j=s->stack[s->top].y;

   setfillstyle(1,RED);

   bar (70+j*20,20+i*20,88+j*20,38+i*20);

   s->top--;

   return(s->stack[s->top+1]);

   }

  }

  void path(int maze[][n2],moved move[],sqs *s)

{ int i,j,dir,x,y,f;

   elemtype elem;

   i=1;j=1;dir=0;

   maze[1][1]=0;

do

   { x=i+move[dir].dx;

   y=j+move[dir].dy;

   if (maze[x][y]==0)

   { elem.x=x;elem.y=y;elem.dir=dir;

   f=push(s,elem);

   delay(15000);

   if (f==false) printf("栈长度太短");

   i=x;j=y;dir=0;maze[x][y]=-1;

   }

   else

   { if (dir<9) dir++;

else

   {

   elem=pop(s);

   if (elem.x!=null)

   {

   i=elem.x;

   j=elem.y;

   dir=elem.dir+1;

   }

   }

  }}while(!((s->top==-1)&&(dir>=7)(x==m)&&(y==n)&&(maze[x][y]==-1)));

if(s->top==-1)

   printf(" !!! no pass !!! ");

   else { elem.x=x;elem.y=y;elem.dir=dir;

   f=push(s,elem);

   setfillstyle(1,GREEN);

   bar (70+j*20,20+i*20,88+j*20,38+i*20);

   getch();

   }

  }

  void start()

  { int h;

   for(h=5;h<=18;h++)

   {

   setfillstyle(1,h);

   bar (80,80,520,180);

   setcolor(h+2);

   settextstyle(TRIPLEX_FONT,HORIZ_DIR,4);

   outtextxy(150,90,"!WELCOME TO MAZE!");

   delay(15000);

   }

   for(h=0;h<13;h++)printf(" ");

   settextstyle(SMALL_FONT,HORIZ_DIR,6);

   setcolor(LIGHTCYAN);

   printf(" ");printf(" ");

   outtextxy(90,200,"input the length of the maze(0-30):");

   scanf("%d",&m);

   for(h=0;h<2;h++)printf(" ");

   printf(" ");printf(" ");

   outtextxy(90,250,"input the wideth of the maze(0-30):");

   scanf("%d",&n);

  }

  void main()

  {

  sqs *s;

  int maze[m2][n2];

  moved move[8];

  initgraph(VGA,VGAHI,"");

  start();

  system("cls");

  inimaze(maze);

  picture(maze);

  s=(sqs*)malloc(sizeof(sqs));

  inistack(s);

  inimove(move);

  path(maze,move,s);

  getch();

  }

  

  

精彩图集

赞助商链接