大家好,我是一个菜鸟,现在正在学习数据结构,希望大家指点我,我编写了一个走迷宫的程序,希望大家指点。源程序如下:(可以动态实现)
#include"time.h"
#include "stdio.h"
#include "graphics.h"
#include "conio.h"
#define null 0
int a[11][15]={ {1,0,1,1,1,0,0,1,1,1,0,0,0,0,0}, /定义迷宫,这是我们课程设计,所以就用老师给的了/
{0,1,1,1,0,0,1,0,0,0,0,1,1,0,0}, /0代表墙,1代表可以通行/
{1,0,0,1,1,1,1,0,0,0,0,0,1,0,0},
{0,0,1,0,0,0,0,1,0,0,0,1,0,1,1},
{0,0,1,0,1,1,0,1,0,0,0,0,0,0,0},
{1,1,0,0,1,0,0,0,1,0,1,1,0,0,0},
{1,0,0,0,0,1,1,0,0,0,0,0,0,0,0},
{1,1,0,0,1,0,0,1,0,0,0,0,0,1,0},
{0,0,1,1,1,0,0,1,0,0,1,1,1,1,1},
{1,1,0,0,0,0,0,1,1,1,0,0,0,0,1},
{1,0,1,1,0,0,0,0,0,1,0,0,0,0,1}
};
struct moving /定义走动方向/
{int x;
int y;
}move[9]={{0,0},{1,1},{0,1},{1,0},{-1,1},{1,-1},{-1,0},{0,-1},{-1,-1}};
typedef struct Position *PPosition;
struct Position /定义结构体/
{
int x;
int y;
int direction;
PPosition link;
};
struct LinkPosition /引入一层封装,其实就是好定义空栈罢了/
{
PPosition top;
};
typedef struct LinkPosition *PLinkPosition;
PLinkPosition createEmptyPosition(void) /建立空栈/
{
PLinkPosition plposition;
plposition=(PLinkPosition)malloc(sizeof(struct LinkPosition));
plposition->top=null;
return(plposition);
}
void push(PLinkPosition plposition,int x,int y) /压栈/
{
PPosition p;
p=(PPosition)malloc(sizeof(struct Position));
p->x=x;
p->y=y;
p->direction=1;
p->link=plposition->top;
plposition->top=p;
}
void pop(PLinkPosition plposition) /弹栈/
{
PPosition p;
p=plposition->top;
plposition->top=plposition->top->link;
free(p);
}
int judge(PLinkPosition plposition) /判定这个位置是不是栈中已有的/
{int m,n;
PPosition p;
m=plposition->top->x;
n=plposition->top->y;
p=plposition->top->link;
while(p!=null)
{if(p->x!=m||p->y!=n)p=p->link;
else
{return(0);
break;
}
}
return(1);
}
void maze(void) /画迷宫/
{
int i,j;
setbkcolor(1);
setcolor(4);
rectangle(90,50,540,380);
for(i=80;i<380;i=i+30)
line(90,i,540,i);
for(i=120;i<540;i=i+30)
line(i,50,i,380);
for(i=0;i<11;i++)
{for(j=0;j<15;j++)
if(a[i][j]==1)floodfill(105+j*30,65+i*30,4);
};
}
void erasermouse(PLinkPosition plposition) /擦除所走过的点/
{
int x1,y1;
x1=105+30*(plposition->top->y-1);
y1=65+30*(plposition->top->x-1);
setcolor(15);
line(x1-10,y1,x1+10,y1);
line(x1,y1-10,x1,y1+10);
setcolor(4);
}
void mouse(PLinkPosition plposition) /画出当前位置/
{
int x1,y1;
x1=105+30*(plposition->top->y-1);
y1=65+30*(plposition->top->x-1);
line(x1-10,y1,x1+10,y1);
line(x1,y1-10,x1,y1+10);
}
void TIMEDELAY() /时间延迟函数,这样动态实现时才可以看清/
{
clock_t time;
time=5+clock();
while(time>clock());
}
main()
{
PLinkPosition plposition,answer;
PPosition p;
int i,j,d,m,n,m1,n1,x1,y1,driver=VGA,mode=VGAHI;
int c[13][17]={0};
clock_t times;
initgraph(&driver,&mode,"");
for(i=1;i<12;i++)
for(j=1;j<16;j++)
c[i][j]=a[i-1][j-1]; /c数组就是给迷宫外层加了一层墙/
plposition=createEmptyPosition();
plposition->top->link=null;
push(plposition,1,1); /压入初始点/
maze();
mouse(plposition);
TIMEDELAY();
erasermouse(plposition);
a: /主结构判定并找出路径/
for(i=plposition->top->direction;i<=9;i++)
{
if(i==9)
{c[plposition->top->x][plposition->top->y]=0;
pop(plposition);
mouse(plposition);
TIMEDELAY();
erasermouse(plposition);
goto a;
}
m=plposition->top->x;
n=plposition->top->y;
m1=m+move[i].x;
n1=n+move[i].y;
push(plposition,m1,n1);
if(c[m1][n1]==0)
{pop(plposition);
plposition->top->direction++;
continue;
}
if(judge(plposition)==0)
{
pop(plposition);
continue;
}
mouse(plposition);
TIMEDELAY();
erasermouse(plposition);
if(m1==11&&n1==15)break;
goto a;
}
answer=createEmptyPosition(); /由于栈中元素弹出时是从出口到入口的路径,所以把它们反过来/
p=plposition->top;
while(p!=null)
{m=p->x;
n=p->y;
push(answer,m,n);
p=p->link;
}
p=answer->top;
for(i=1;i<25;i++)
printf("\n");
while(p->link!=null)
{printf("(%d,%d)-->",p->x,p->y);p=p->link;}
printf("(%d,%d)",p->x,p->y);
getch();
}
作者:曹开锐 哈尔滨工业
QQ号:185917511(希望爱好c语言的朋友加我啊!)
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |