论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

小迷宫

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:44:26

# 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<m+2;i++)
  { for (j=0;j<n+2;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("\n");
    settextstyle(SMALL_FONT,HORIZ_DIR,6);
    setcolor(LIGHTCYAN);
    printf("\t\t\t\t\t\t");printf("     ");
    outtextxy(90,200,"input the length of the maze(0-30):");
    scanf("%d",&m);
    for(h=0;h<2;h++)printf("\n");
    printf("\t\t\t\t\t\t");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();
}

上一篇:{实例}关于魔方阵的解法 人气:5278
下一篇:{实例}蛇吃蛋 人气:4970
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058