我做的一个程序。迷宫的那个。
/* my first graphic program under TurboC
time:2005/10/24
by Chiron
*/
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#define N 15
struct pot
{
int x;
int y;
struct pot* nextPtr;
};
typedef struct pot Pot;
typedef Pot* PotPtr;
void initMap(int (*map)[N]);
void init(); /*init graph*/
void print(int (*map)[N]);
Pot findWay(PotPtr* tempPtr,PotPtr* pathPtr,int (*map)[N]);
int len(PotPtr*); /*get the stack lenth*/
Pot pop(PotPtr*);
void push(PotPtr*,Pot);
void drawR(Pot);
void drawPath(PotPtr); /*draw the way to pathPtr*/
Pot getTop(PotPtr*);
void printList(PotPtr);
void test(PotPtr*);
int main()
{
int map[N][N];
PotPtr pathPtr,tempPtr;
Pot pot;
pathPtr=tempPtr=NULL;
clrscr();
/*star pot*/
pot.x=1;
pot.y=1;
push(&tempPtr,pot);
init();
initMap(map);
#if 1
print(map);
#endif
#if 1
while(pot.x!=N-2||pot.y!=N-2) /*end pot*/
{
pot=findWay(&tempPtr,&pathPtr,map);
}
#endif
#if 0
drawPath(&pathPtr);
#endif
getch();
}
void init()
{
int drive=DETECT,mod;
initgraph(&drive,&mod,"");
}
void initMap(int (*map)[N])
{
int i,j;
srand((int)time());
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==0||j==0||i==N-1||j==N-1)
{
map[i][j]=1;
}else if(i==1&&j==1||i==N-2&&j==N-2)
{
map[i][j]=0;
}else{
map[i][j]=rand()%2;
}
}
}
}
void print(int (*map)[N])
{
Pot pot;
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==1&&j==1||i==N-2&&j==N-2)
{
pot.x=i;
pot.y=j;
drawR(pot);
}
if(map[i][j]==1)
{
setfillstyle(SOLID_FILL,WHITE);
bar(100+20*i-8,100+20*j-8,100+20*i+8,100+20*j+8);
}
}
printf("\n");
}
}
Pot findWay(PotPtr* tempPtr,PotPtr* pathPtr,int (*map)[N])
{
Pot pot;
Pot tempPot;
int length=0;
pot=getTop(tempPtr);
map[pot.x][pot.y]=1;
push(pathPtr,pot);
drawR(pot);
if(map[pot.x-1][pot.y-1]==0)
{
tempPot.x=pot.x-1;
tempPot.y=pot.y-1;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x][pot.y-1]==0)
{
tempPot.x=pot.x;
tempPot.y=pot.y-1;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x-1][pot.y]==0)
{
tempPot.x=pot.x-1;
tempPot.y=pot.y;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x+1][pot.y-1]==0)
{
tempPot.x=pot.x+1;
tempPot.y=pot.y-1;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x-1][pot.y+1]==0)
/* my first graphic program under TurboC
time:2005/10/24
by Chiron
*/
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#define N 15
struct pot
{
int x;
int y;
struct pot* nextPtr;
};
typedef struct pot Pot;
typedef Pot* PotPtr;
void initMap(int (*map)[N]);
void init(); /*init graph*/
void print(int (*map)[N]);
Pot findWay(PotPtr* tempPtr,PotPtr* pathPtr,int (*map)[N]);
int len(PotPtr*); /*get the stack lenth*/
Pot pop(PotPtr*);
void push(PotPtr*,Pot);
void drawR(Pot);
void drawPath(PotPtr); /*draw the way to pathPtr*/
Pot getTop(PotPtr*);
void printList(PotPtr);
void test(PotPtr*);
int main()
{
int map[N][N];
PotPtr pathPtr,tempPtr;
Pot pot;
pathPtr=tempPtr=NULL;
clrscr();
/*star pot*/
pot.x=1;
pot.y=1;
push(&tempPtr,pot);
init();
initMap(map);
#if 1
print(map);
#endif
#if 1
while(pot.x!=N-2||pot.y!=N-2) /*end pot*/
{
pot=findWay(&tempPtr,&pathPtr,map);
}
#endif
#if 0
drawPath(&pathPtr);
#endif
getch();
}
void init()
{
int drive=DETECT,mod;
initgraph(&drive,&mod,"");
}
void initMap(int (*map)[N])
{
int i,j;
srand((int)time());
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==0||j==0||i==N-1||j==N-1)
{
map[i][j]=1;
}else if(i==1&&j==1||i==N-2&&j==N-2)
{
map[i][j]=0;
}else{
map[i][j]=rand()%2;
}
}
}
}
void print(int (*map)[N])
{
Pot pot;
int i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(i==1&&j==1||i==N-2&&j==N-2)
{
pot.x=i;
pot.y=j;
drawR(pot);
}
if(map[i][j]==1)
{
setfillstyle(SOLID_FILL,WHITE);
bar(100+20*i-8,100+20*j-8,100+20*i+8,100+20*j+8);
}
}
printf("\n");
}
}
Pot findWay(PotPtr* tempPtr,PotPtr* pathPtr,int (*map)[N])
{
Pot pot;
Pot tempPot;
int length=0;
pot=getTop(tempPtr);
map[pot.x][pot.y]=1;
push(pathPtr,pot);
drawR(pot);
if(map[pot.x-1][pot.y-1]==0)
{
tempPot.x=pot.x-1;
tempPot.y=pot.y-1;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x][pot.y-1]==0)
{
tempPot.x=pot.x;
tempPot.y=pot.y-1;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x-1][pot.y]==0)
{
tempPot.x=pot.x-1;
tempPot.y=pot.y;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x+1][pot.y-1]==0)
{
tempPot.x=pot.x+1;
tempPot.y=pot.y-1;
map[tempPot.x][tempPot.y]=1;
push(tempPtr,tempPot);
length++;
}
if(map[pot.x-1][pot.y+1]==0)