///////////////程序名称:迷宫问题[栈的应用]///////////////////////
///////////////作者:信科0402 刘志磊//////////////////////////////
///////////////时间:2007.4.11.10.10////////////////////////////////
#include<iostream>
#include <stdio.h>
#include<time.h>
using namespace std;
#define TIMEMAX 10
#define OK 0
#define ERROR -1
#define UP 1 //用于存储方向的常量
#define DOWN 2
#define LEFT 3
#define RIGHT 4
#define Rank 17
#define File 65
#define Rand 100
char Maze[Rank][File]; //定义存储迷宫用的字符型二维数组
char mark=0; //标记
char Bar=2; //地图
char Player=12; //游戏者
/*定义存储走过路线的栈*/
typedef struct SNode
{
int data;
struct SNode *next;
}SNode;
typedef struct
{
int length;
SNode *top;
}STACK;
/*显示迷宫函数*/
void ShowMaze()
{
int i,j;
system(“cls”);
system(“color 0a”);
cout<<“/t/t/t/t*****迷宫问题*****”<<“/n/n/n”;
for(i=0;i<Rank;i++)
{
cout<<“/t”<<Bar;
for(j=0;j<File;j++)
cout<<Maze[i][j];
cout<<Bar<<“/n”;
}
}
/*迷宫初始化函数*/
void InitMaze()
{
int n,i,j;
for(i=0;i<Rank;i++)
for(j=0;j<File;j++)
Maze[i][j]=2;
srand((unsigned)time(NULL));
for(i=1;i<Rank-1;i++)//for开始
for(j=1;j<File;j++)
{
n=rand()%Rand;//随机函数
if(n<Rand*8/11) Maze[i][j]=’ ‘;
}//for结束
Maze[1][0]=’ ‘;//给迷宫留入口
Maze[1][1]=’ ‘;
Maze[1][2]=’ ‘;
Maze[1][3]=’ ‘;
Maze[Rank-2][File-4]=’ ‘;//给迷宫留出口
Maze[Rank-2][File-3]=’ ‘;
Maze[Rank-2][File-2]=’ ‘;
Maze[Rank-2][File-1]=’ ‘;
}
/*栈初始化*/
void InitStack(STACK *S)
{
S->top=NULL;
S->length=NULL;
}
/*元素e入栈*/
int Push(STACK *S,int e)
{
SNode *p;
p=new SNode[sizeof(SNode)];
if(!p) return ERROR;
p->data=e;
p->next=S->top;
S->top=p;
S->length++;
return OK;
}
/*栈顶元素出栈,e带回栈顶元数*/
int Pop(STACK *S,int *e)
{
SNode *p;
if(S->top==NULL) return ERROR;
p=S->top;
*e=p->data;
S->top=p->next;
S->length–;
delete p;
return OK;
}
/*判断S是否为空栈*/
int Empty(STACK S)
{
if(S.top==NULL) return OK;
return ERROR;
}
int main()
{
int i,j,path,speed=0;
long timei,timej;
/* char temp;
for(int t=0;t<100;t++)
{
temp=t;
cout<<t<<” “<<temp<<endl;
}
system(“pause”);*/
STACK S; //定义一个用于存储老鼠走过的路线的栈
InitMaze(); //随机成生迷宫
InitStack(&S); //初始化栈
i=1;
j=0;
Maze[i][j]=Player; //初始化老鼠位置
ShowMaze(); //显示迷宫
cout<<“/n/t/t/t请选择速度:1 快速 2 慢 “<<endl;
while(speed!=1 && speed!=2) cin>>speed;
while(i>=0 && i<Rank && j>=0 && j<File)//开始钻迷宫
{
ShowMaze(); //显示迷宫
if(speed==2) //选择2较慢时进入空循环延时
for(timei=0;timei<TIMEMAX;timei++)
for(timej=0;timej<TIMEMAX;timej++);//空循环,延时用的!
if(i==Rank-2&&j==File-1) //判断是否到达出口
{
cout<<“/n/t/t/t/t恭喜成功走出!所用的步数为:”<<S.length<<endl;
cout<<“/t/t/t注:”<<Player<<“为游戏者 “<<Bar<<“为地图障碍 “<<mark<<“为所留印记”<<endl;
system(“pause”);
exit(1);
}
if(Maze[i][j+1]==’ ‘) //向右走一步
{
char dir=26;
Maze[i][j]=dir;
j=j+1;
Maze[i][j]=Player;
Push(&S,RIGHT);
}
else
{
if(Maze[i+1][j]==’ ‘) //向下走一步
{
char dir=25;
Maze[i][j]=dir;
i=i+1;
Maze[i][j]=Player;
Push(&S,DOWN);
}
else
{
if(Maze[i-1][j]==’ ‘) //向上走一步
{
char dir=24;
Maze[i][j]=dir;
i=i-1;
Maze[i][j]=Player;
Push(&S,UP);
}
else
{
if(Maze[i][j-1]==’ ‘) //向左走一步
{
char dir=27;
Maze[i][j]=dir;
j=j-1;
Maze[i][j]=Player;
Push(&S,LEFT);
}
else //遇到障碍往回走一步
{
if(Empty(S)==OK) //判断是否回到起点了,是则退出程序
{
cout<<“/n/t/t/t/t@^@ 迷宫没有出路! @^@/n”<<endl;
system(“pause”);
exit(1);
}
else
{
if(Pop(&S,&path)==OK) //判断能否取回上一步路径
{
switch(path)
{
case LEFT:
Maze[i][j]=mark;
j=j+1;
Maze[i][j]=Player;
break;
case UP:
Maze[i][j]=mark;
i=i+1;
Maze[i][j]=Player;
break;
case DOWN:
Maze[i][j]=mark;
i=i-1;
Maze[i][j]=Player;
break;
case RIGHT:
Maze[i][j]=mark;
j=j-1;
Maze[i][j]=Player;
break;
} //结束分支语句switch
} //结束判断能否取回上一步路径
}
}
}
}
}
}//结束while循环
system(“pause”);
return 0;
}