纯c语言迷宫源码.zip
立即下载
资源介绍:
纯c语言迷宫源码.zip
#include
#include
#include
/* 【自学去】网站收集 http://www.zixue7.com */
/*迷宫的数组*/
int maze[100][100];
/*迷宫的行数和列数*/
int m=0,n=0;
/*
*对迷宫进行初始化,用随机数产生迷宫
*/
void InitMaze()
{
int i,j,temp;
srand((unsigned)time(NULL));
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
temp=rand()%100;
if(temp>30)
{
maze[i-1][j-1]=0;
}else
{
maze[i-1][j-1]=1;
}
}
maze[0][0]=0;
maze[m-1][n-1]=9;
}
/*
*定义栈和栈的节点
*/
typedef struct Node
{
int x;
int y;
struct Node *next;
}Node,*Stack;
/*
*初始化Stack
*/
void InitStack(Node *Stack)
{
Stack=(Node *)malloc(sizeof(Node));
if(Stack==NULL)
{
printf("分配空间失败\n");
exit(0);
}else
{
Stack->next=NULL;
}
}
/*
*压栈
*/
void push(Node *Stack,int x,int y)
{
Node *temp;
temp=(Node *)malloc(sizeof(Node));
if (!temp)
{
printf("分配内存空间错误");
return;
}
else
{
temp->x=x;
temp->y=y;
temp->next=Stack->next;
Stack->next=temp;
}
}
/*
*出栈
*/
void pop(Node *Stack,int *x,int *y)
{
Node *temp;
temp=Stack->next;
if(!temp){
return;
}else{
*x=temp->x;
*y=temp->y;
}
Stack->next=temp->next;
free(temp);
}
/*
*判断栈是否为空
*/
int isEmpty(Node *Stasck)
{
return ((Stasck->next)==NULL);
}
/*
*判断从该点时候可以向其他方向移动,并返回移动的方向
*/
int pass(int i,int j)
{
/*右方向*/
if(j=1&&(maze[i][j-1]==0||maze[i][j-1]==9))
{
return 4;
}
/*上方向*/
if(i>=1&&(maze[i-1][j]==0||maze[i-1][j]==9))
{
return 5;
}
return -1;
}
/*
*对迷宫进行打印
*/
void drawMaze()
{
int i=0,j=0;
for(i=0;i0,m<100):");
scanf("%d",&m);
printf("plase input the number of line n(n>0,n<100):");
scanf("%d",&n);
if(m<0||m>100||n<0||n>100){
printf("The number is error,process will exit !\n");
exit(-1);
}
printf("The character is 'a',it is area.\n");
printf("The character is 'b',it is wall.\n");
printf("\n");
InitMaze();
printf("The oid Maze:\n");
printf("\n");
drawMaze();
printf("\n show the path ?(y/n)");
fflush(stdin);
if(toupper(getch())=='Y')
{
printf("\n");
ShowPath();
printf("\n go on play ?(y/n)");
fflush(stdin);
if(toupper(getch())=='Y')
{
goto loop;
}
else
{
exit(1);
}
}
else
{
exit(1);
}
getch();
return 0;
}