//*laoshu.h 系统主文件
//迷宫用字符型二维数组存储
//迷宫随机生成
//其中”*”表示墙
//” “表示路
//”==”表示走过的无用的路
//”+”表示走过的有用的路
//”^”表示当前老鼠所在的位置
//TIMEMAX可以设定执行速度
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
#include<time.h>
#include<malloc.h>
#define TIMEMAX 6000
#define OK 0
#define ERROR -1
#define UP 1
#define DOWN 2
#define LEFT 3
#define RIGHT 4
char arr[18][70];

typedef struct SNode
{
    int data;
    struct SNode *next;
}SNode;

class Stack
{
    private:
    SNode *top;
    public:
    Stack();//构造函数,与类同名
    int totallength;//记载栈操作次数
    int length;//记录栈深度
    int Push(int e);//元素e入栈操作
    int Pop();//出栈操作,返回栈顶元素
    int IsEmpty();//判断栈是否为空
};

Stack::Stack()//构造函数,与类同名
{
    top=NULL;
    length=0;
    totallength=0;
}
int Stack::Push(int e)//元素e入栈操作
{
    SNode *p;
    p=(SNode *)malloc(sizeof(SNode));
    if(!p)
    return ERROR;
    p->data=e;
    p->next=top;
    top=p;
    length++;
    totallength++;
    return OK;
}
int Stack::Pop()//出栈操作,返回栈顶元素
{
    int e;
    SNode *p;
    if(top==NULL)
    return ERROR;
    e=top->data;
    p=top;
    top=p->next;
    length–;
    totallength++;
    delete p;
    return e;
}
int Stack::IsEmpty()//判断栈是否为空
{
    if(top==NULL)
    return OK;
    return ERROR;
}

//显示迷宫函数
void ShowMaze()
{
    int i,j;
    system(“cls.exe”);//清屏
    for(i=0;i<18;i++)
    {
        //利用二维数组arr绘制老鼠及迷宫图
        for(j=0;j<70;j++)
        cout<<arr[i][j];
        cout<<“n”;
    }
    cout<<“*表示墙
^表示老鼠 空格表示路 +表示有用的路 =表示无用的路”<<endl;
}

//迷宫初始化函数
void InitMaze()
{
    int n,i,j;
    for(i=0;i<18;i++)
    for(j=0;j<70;j++)
    arr[i][j]=’*’;//先把迷宫的所有位置都画上’*’表示墙
    srand((unsigned)time(NULL));//随机函数做种
    for(i=1;i<17;i++)
    for(j=1;j<69;j++)
    {
        n=rand()%25;//利用随机函数随机生成n值
        if(n<17)
        arr[i][j]=’ ‘;//随机设置通道
    }
    arr[1][0]=’
‘;
    arr[1][1]=’
‘;
    arr[1][2]=’
‘;
    arr[1][3]=’
‘;
    arr[16][66]=’
‘;
    arr[16][67]=’
‘;
    arr[16][68]=’
‘;
    arr[16][69]=’
‘;
}//给迷宫绘制出口

//主函数
void main()
{
    int i,j,path,speed=0;
    long timei,timej;
    Stack s;//定义一个用于存储老鼠走过路线的栈
    InitMaze();//随机生成迷宫
    
    i=1;
    j=0;
    arr[i][j]=’^’;//初始化老鼠位置
    ShowMaze();//显示迷宫
    cout<<“选择速度:1
快速 2较慢”<<endl;
    while(speed!=1&&speed!=2)
    cin>>speed;
    while(i>=0&&i<18&&j>=0&&j<70)//开始进迷宫
    {
        ShowMaze();
        if(speed==2)
        for(timei=0;timei<TIMEMAX;timei++)
        for(timej=0;timej<TIMEMAX;timej++)
        if(i==16&&j==69)
        {
            cout<<“老鼠逃出来了!”;
            cout<<“老鼠总共跑了:”<<s.totallength<<“步
“;
            cout<<“有用的步数为:”<<s.length <<endl;
            exit(1);
        };
        if(arr[i][j+1]==’
‘)//向右一步
        {
            arr[i][j]=’+’;
            j=j+1;
            arr[i][j]=’^’;
            s.Push(RIGHT);
            continue;
        };
        if(arr[i+1][j]==’
‘)//向下走一步
        {
            arr[i][j]=’+’;
            i=i+1;
            arr[i][j]=’^’;
            s.Push(DOWN);
            continue;
        };
        if(arr[i-1][j]==’
‘)//向上走一步
        {
            arr[i][j]=’+’;
            i=i-1;
            arr[i][j]=’^’;
            s.Push(UP);
            continue;
        };
        if(arr[i][j-1]==’
‘)//向左走一步
        {
            arr[i][j]=’+’;
            j=j-1;
            arr[i][j]=’^’;
            s.Push(LEFT);
            continue;
        };
        //上下左右都无路可走
        if(s.IsEmpty()==OK)
        {
            cout<<“可怜的老鼠,迷宫没有出路!n”<<endl;
            exit(1);
        };
        path=s.Pop();
        switch(path)
        {
            case LEFT:
            arr[i][j]=’=’;
            j=j+1;
            arr[i][j]=’^’;
            break;
            case UP:
            arr[i][j]=’=’;
            i=i+1;
            arr[i][j]=’^’;
            break;
            case DOWN:
            arr[i][j]=’=’;
            i=i-1;
            arr[i][j]=’^’;
            break;
            case RIGHT:
            arr[i][j]=’=’;
            j=j-1;
            arr[i][j]=’^’;
            break;
        }
    };
}

相关文章