Ⅰ 求C++老鼠走迷宮源代碼
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define stack_init_size 200
#define stack_increment 10
#define OVERFLOW 0
#define OK 1
#define ERROE 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef struct{
int x;
int y;
}PosType;
typedef struct {
int ord; // 通道塊在路徑上的"序號"
PosType seat; //通道塊在迷宮中的"坐標位置"
int di; //從此通道塊走向下一通道塊的"方向"
}SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
int mg[20][20];
/*隨機生成迷宮的函數
/*為了能夠讓盡量能通過,將能通過的塊和不能通過的塊數量比大致為2:1*/
void Random(){
int i,j,k;
srand(time(NULL));
mg[1][0]=mg[1][1]=mg[18][19]=0; //將入口、出口設置為"0"即可通過
for(j=0;j<20;j++)
mg[0][j]=mg[19][j]=1; /*設置迷宮外圍"不可走",保證只有一個出口和入口*/
for(i=2;i<19;i++)
mg[i][0]=mg[i-1][19]=1; /*設置迷宮外圍"不可走",保證只有一個出口和入口*/
for(i=1;i<19;i++)
for(j=1;j<19;j++){
k=rand()%3; //隨機生成0、1、2三個數
if(k)
mg[i][j]=0;
else{
if((i==1&&j==1)||(i==18&&j==18)) /*因為距入口或出口一步的路是必經之路,故設該通道塊為"0"加大迷宮能通行的概率*/
mg[i][j]=0;
else
mg[i][j]=1;
}
}
}
//構造一個空棧
Status InitStack(SqStack &s){
s.base =(SElemType *)malloc(stack_init_size * sizeof(SElemType));
if(!s.base) return OVERFLOW;
s.top=s.base;
s.stacksize=stack_init_size;
return OK;
}
//當前塊可否通過
Status Pass(PosType e){
if (mg[e.x][e.y]==0) //0時可以通過
return OK; // 如果當前位置是可以通過,返回1
return OVERFLOW; // 其它情況返回0
}
//留下通過的足跡
Status FootPrint(PosType e){
mg[e.x][e.y]=7;
return OK;
}
//壓入棧
Status Push(SqStack &s,SElemType e){
if(s.top-s.base>=s.stacksize){
s.base=(SElemType *)realloc(s.base,(s.stacksize+stack_increment) *sizeof(SElemType));
if(!s.base)exit(OVERFLOW);
s.top=s.base+s.stacksize;
s.stacksize+=stack_increment;
}
*s.top++=e;
return OK;
}
//出棧
Status Pop(SqStack &s,SElemType &e){
if(s.top==s.base)
return ERROE;
e=*--s.top;
return OK;
}
//下一步
PosType NextPos(PosType &e,int dir){
PosType E;
switch(dir){
case 1:E.x=e.x; //向下
E.y=e.y+1;
break;
case 2:E.x=e.x+1; //向右
E.y=e.y;
break;
case 3:E.x=e.x; //向上
E.y=e.y-1;
break;
case 4:E.x=e.x-1; //向左
E.y=e.y;
break;
}
return E;
}
//是否空棧
Status StackEmpty(SqStack s){
if (s.top==s.base)
return OK;
return OVERFLOW;
}
//留下不能通過的足跡
Status MarkPrint(PosType e){
mg[e.x][e.y]=3;
return OK;
}
//迷宮函數
// 若迷宮maze中從入口 start到出口 end的通道,則求得一條存放在棧中
// (從棧底到棧頂),並返回TRUE;否則返回FALSE
Status MazePath(int mg,PosType start,PosType end,SqStack &s){
PosType curpos;
InitStack(s);
SElemType e;
int curstep;
curpos=start; // 設定"當前位置"為"入口位置"
curstep=1; // 探索第一步
do{
if(Pass(curpos)){ // 當前位置可通過,即是未曾走到過的通道塊
FootPrint(curpos); // 留下足跡
e.di =1;
e.ord = curstep;
e.seat= curpos;
Push(s,e); // 加入路徑
if(curpos.x==end.x&&curpos.y==end.y){
printf("\n\n0∩_∩0 能到達終點!");
return TRUE;
}
curpos=NextPos(curpos,1); // 下一位置是當前位置的東鄰
curstep++; // 探索下一步
}
else{ // 當前位置不能通過
if(!StackEmpty(s)){
Pop(s,e);
while(e.di==4&&!StackEmpty(s)){
MarkPrint(e.seat);
Pop(s,e);
}
if(e.di<4){
e.di++;
Push(s,e); // 留下不能通過的標記,並退回一步
curpos=NextPos(e.seat,e.di); /* 當前位置設為新方向的相鄰塊*/
}//if
}//if
}//else
}while(!StackEmpty(s));
printf("\n\n囧 ! 不能到達終點!");
return FALSE;
}
//列印迷宮
void PrintMaze(){
int i,j;
printf("運行路徑:\n\n");
for(i=0;i<20;i++){
for(j=0;j<20;j++){
if(mg[i][j]==0)printf(" ");
else if(mg[i][j]==1) printf("■"); //迷宮的"牆"
else if(mg[i][j]==3) printf("◇"); //不通的路
else if(mg[i][j]==7)printf("○"); //通過的路徑
}
printf("\n");
}
printf("\n");
}
void main(){
SqStack S;
PosType start,end;
start.x=1;start.y=0; //起點坐標
end.x=18;end.y=19; //終點坐標
printf("\n==================迷宮游戲==================");
printf("\n說明:■不能走的區域\t◇走不通的區域");
printf("\n '空格'代表未到過的區域");
printf("\n ○代表能通過的路徑,指向終點");
printf("\n============================================");
Random();
printf("\n\nTest 1:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
system("pause");
Random();
printf("\nTest 2:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
system("pause");
Random();
printf("\nTest 3:");
MazePath(mg[20][20],start,end,S);
PrintMaze();
printf("\n==========程序退出,感謝使用!==========\n");
}