#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <time.h>
#include <conio.h>
#define MAXSIZE 20
typedef struct
{ int x;
int y;
}PosType;
typedef struct
{ int di1;
int di2;
int di3;
int di4;
}Direct;
typedef int MazeElem;
typedef struct
{ MazeElem color[MAXSIZE+2][MAXSIZE+2];
PosType start;
PosType end;
}MazeType;
MazeType mazes;
int unit,rows,columns,ratio;
PosType offset;
PosType NextPos( PosType curpos,int di)
{ switch(di)
{case 1: curpos.y++;break; /*右*/
case 2: curpos.x++;break; /*下*/
case 3: curpos.y--;break; /*左*/
case 4: curpos.x--;break;/*上*/
}
return curpos;
}
void Near(PosType start,PosType end)
{ int i=0;
PosType curpos;
while(++i<=4)
{ curpos=NextPos(start,i);
if(curpos.x==end.x && curpos.y==end.y)
{ printf(" success!\n");
getch();
closegraph();
exit(0); }
} }
void direction(Direct *direct,PosType start,PosType end)
{ if(start.y<=end.y)
{ if(start.x<=end.x)
{ if(end.y-start.y<=end.x-start.x)
{ direct->di1=2; /*左*/
direct->di2=3; /*下*/
direct->di3=4; /*上*/
direct->di4=1; /*右*/
}
else
{ direct->di1=1;
direct->di2=2;
direct->di3=3;
direct->di4=4;
} }
else { if(end.y-start.y<=start.x-end.x)
{ direct->di1=4; /*上 */
direct->di2=1; /*右*/
direct->di3=3; /*左 */
direct->di4=2; /*下*/
}
else
{ direct->di1=1;
direct->di2=4;
direct->di3=3;
direct->di4=2; } } }
else { if(start.x<=end.x)
{ if(start.y-end.y<=end.x-start.x)
{ direct->di1=2;
direct->di2=3;
direct->di3=1;
direct->di4=4; }
else
{ direct->di1=3;
direct->di2=2;
direct->di3=4;
direct->di4=1; } }
else {
if(start.y-end.y<=start.x-end.x)
{ direct->di1=4;
direct->di2=3;
direct->di3=1;
direct->di4=2; }
else {
direct->di1=3;
direct->di2=4;
direct->di3=2;
direct->di4=1;
} } } }
void Init(void)/*图形初始化*/
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
void CreateMaze()
{ int i,j;
char m ;
int gdriver, gmode;
detectgraph(&gdriver, &gmode);
registerbgidriver(EGAVGA_driver);
clrscr();
setbkcolor(2);
settextstyle(0,0,2);
outtextxy(30,60,"《**This program is designed by heqiuyun**》" );
settextstyle(0,0,1);
outtextxy(60,100,"Operate handbook:");
outtextxy(70,140,"1.Black square denotations unpassable;" );
outtextxy(70,180,"2.White square denotations passable;" );
outtextxy(70,220,"3.Ratio mens white squares&&black squares" );
outtextxy(70,260,"4.Input rows,colums and ratio ");
scanf("{%d,%d,%d}",&rows,&columns,&ratio);
if(rows>MAXSIZE || rows<1 || columns>MAXSIZE ||columns<1)
{
getchar();
clrscr();
outtextxy(30,60,"attention! ");
outtextxy(60,110,"1.1<=rows<=MAXSIZE");
outtextxy(60,160,"2.1<=columns<=MAXSIZE");
outtextxy(60,210,"3.MAXSIZE is defined 20");
outtextxy(60,240," Please enter the \"enter\" key" );
}
getchar();
rows=20;
columns=20;
ratio=2;
for(i=1;i<=MAXSIZE;i++)
for(j=1;j<=MAXSIZE;j++)
mazes.color[i][j]=WHITE;
for(i=1;i<=rows;i++)
for(j=1;j<=columns;j++)
if(random(3)%(ratio))
mazes.color[i][j]=BLACK;
}
void PointColor(PosType curpos)
{ setcolor(GREEN); setfillstyle(SOLID_FILL,mazes.color[curpos.y][curpos.x]);
bar((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,(curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit);
rectangle((curpos.x+offset.x)*unit,(curpos.y+offset.y)*unit,(
curpos.x+offset.x+1)*unit,(curpos.y+offset.y+1)*unit); }
void DrawMaze()
{ int i,j,k,m,n;
PosType curpos;
clrscr();
m=getmaxx();
n=getmaxy();
k=(m>n?n:m);
unit=k/(MAXSIZE*2);
m=m/unit;
n=n/unit;
offset.x=m/4;
offset.y=n/4;
for(i=1;i<=rows;i++)
for(j=1;j<=columns;j++)
{ curpos.x=j;
curpos.y=i;
PointColor(curpos);
} printf("\n red square denotations maze start;yellow square denotations maze end.\n define maze start and end using (rowth,columnth).\n for example (2,3),(7,8) : ");
scanf("(%d,%d),(%d,%d)",&mazes.start.y,&mazes.start.x,&mazes.end.y,&mazes.end.x);
if(!mazes.start.y||!mazes.start.y||!mazes.end.y||!mazes.end.x)
{ mazes.start.y=1;
mazes.start.x=1;
mazes.end.y=20;
mazes.end.x=20
;}
else if(mazes.start.x<1||mazes.start.x>columns||mazes.start.y<1||mazes.start.y>rows||mazes.end.x<1||mazes.end.x>columns||mazes.end.y<1||mazes.end.y>rows)
{ printf(" attention! 1<=x<=rows,1<=y<=columns, you define rows=%d,columns=%d",rows,columns);
getch();
exit(0); }
mazes.color[mazes.start.y][mazes.start.x]=RED;
PointColor(mazes.start);
sleep(1);
mazes.color[mazes.end.y][mazes.end.x]=YELLOW; PointColor(mazes.end);
sleep(1);
i=0;
while(++i<=4)
{ curpos=NextPos(mazes.end,i);
if(mazes.color[curpos.y][curpos.x]==WHITE)
break;
}
if(i==5)
{ printf(" maze end unpassable!\n");
getch();
exit(0);
} }
void MazePath(PosType start,PosType end)
{ Direct direct;
if(start.x==end.x && start.y==end.y)
{ printf(" success!\n");
getch();
closegraph();
exit(0);
}
if(mazes.color[start.y][start.x]==RED ||mazes.color[start.y][start.x]==WHITE)
{ if(mazes.color[start.y][start.x]==WHITE)
{ mazes.color[start.y][start.x]=BLUE;
PointColor(start);
sleep(1);
}
Near(start,end);
direction(&direct,start,end);
MazePath(NextPos(start,direct.di1),end);
MazePath(NextPos(start,direct.di2),end);
MazePath(NextPos(start,direct.di3),end);
MazePath(NextPos(start,direct.di4),end);
if(mazes.color[start.y][start.x]!=RED)
{ mazes.color[start.y][start.x]=DARKGRAY;
PointColor(start); sleep(1);
}
}
}
main()
{
Init();
CreateMaze();
DrawMaze();
MazePath(mazes.start,mazes.end);
printf(" failed!\n");
getch();
closegraph();
}