C++迷宫自动寻路

#include<stack>
#include<iostream>
#include<windows.h>
#include<ctime>
#include<stdlib.h>
#include<stdio.h>

using namespace std;

int  maze[12][12]=
    {
        {0,0,0,0,0,0,0,0,0,0,0,0},
        {0,1,1,0,1,1,1,1,0,0,1,0},
        {0,1,1,0,1,1,0,1,1,0,1,0},
        {0,1,1,1,1,0,1,1,0,0,1,0},
        {0,0,0,1,0,0,0,1,1,1,0,0},
        {0,0,1,1,1,0,1,0,0,1,0,0},
        {0,0,1,0,0,1,1,1,1,0,0,0},
        {0,0,1,1,1,1,0,0,1,0,0,0},
        {0,0,1,0,0,1,0,0,1,1,1,0},
        {0,0,0,0,0,0,0,0,0,0,1,0},
        {0,0,0,0,0,0,0,0,0,0,1,0},
        {0,0,0,0,0,0,0,0,0,0,0,0}
    };
typedef struct 
{
    int x;
    int y;
}jiegou;
stack<jiegou>z;
void xunlu(int maze[12][12])
{
    int mark[12][12]={0};
    int g,h;
    jiegou a,b;
    a.x=1,a.y=1;
    z.push(a);
    mark[1][1]=1;
    while(!(b.x==10&&b.y==10)&&!(z.empty()))
    {
        b=z.top();
        g=b.y;
        h=b.x;
        if(((maze[g][h+1]==1)||(maze[g][h+1]==5))&&(mark[g][h+1]==0))
        {
            b.x=h+1;
            mark[g][h+1]=1;
            for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
                else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;
            
        }
    
        Sleep(800);    
        system("cls");
            maze[g][h+1]=5;
            z.push(b);
        }
        else if(((maze[g+1][h]==1)||(maze[g+1][h]==5))&&(mark[g+1][h]==0))
        {
            b.y=g+1;
            mark[g+1][h]=1;
            for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
                else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;
            
        }
    
        Sleep(800);    
        system("cls");
            maze[g+1][h]=5;
           z.push(b);
        }
        else if(((maze[g][h-1]==1)||(maze[g][h-1]==5))&&(mark[g][h-1]==0))
        {
            b.x=h-1;
           mark[g][h-1]=1;
           for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
                else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;        
        }
    
        Sleep(800);    
        system("cls");
           maze[g][h-1]=5;
            z.push(b);
        }
        else if(((maze[g-1][h]==1)||(maze[g-1][h]==5))&&(mark[g-1][h]==0))
        {
            b.y=g-1;
            mark[g-1][h]=1;
            for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
                else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;
            
        }
    
        Sleep(800);    
        system("cls");
            maze[g-1][h]=5;
            z.push(b);
        }
        else if(((maze[g][h+1]==1)||(maze[g][h+1]==5))&&(mark[g][h+1]==1))
        {
            b.x=h+1;
            mark[g][h+1]=3;
            for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
            else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;
            
        }
    
        Sleep(800);    
        system("cls");
            maze[g][h+1]=5;
            z.push(b);
        }
        else if(((maze[g+1][h]==1)||(maze[g+1][h]==5))&&(mark[g+1][h]==1))
        {
            b.y=g+1;
            mark[g+1][h]=3;
            for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
            else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;
            
        }
    
        Sleep(800);    
        system("cls");
            maze[g+1][h]=5;
           z.push(b);
        }
        else if(((maze[g][h-1]==1)||(maze[g][h-1]==5))&&(mark[g][h-1]==1))
        {
            b.x=h-1;
           mark[g][h-1]=3;
           for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
                else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;        
        }
    
        Sleep(800);    
        system("cls");
           maze[g][h-1]=5;
            z.push(b);
        }
        else if(((maze[g-1][h]==1)||(maze[g-1][h]==5))&&(mark[g-1][h]==1))
        {
            b.y=g-1;
            mark[g-1][h]=3;
            for(int i=0;i<=11;i++) 
        {
            for(int j=0;j<=11;j++)
            {
             if(mark[i][j]==1&&maze[i][j]==1)
                {
                cout<<"☆";
                }
            else if(mark[i][j]==1&&maze[i][j]==5)
                {
                    cout<<"# ";
                }
            else if(mark[i][j]==3)
                {
                    cout<<"* ";
                }
            else{
                    if(maze[i][j]==0)
                    cout<<"●";
                    if(maze[i][j]==1)
                    cout<<"○";
                }
            }
            cout<<endl;
            
        }
    
        Sleep(800);    
        system("cls");
            maze[g-1][h]=5;
            z.push(b);
        }
        else z.pop();
        
    }
    if(!z.empty())
    {
        cout<<"所走的路径为:" <<endl;
        while(!(z.empty()))
    {
        b=z.top();
        cout<<"("<<b.y<<","<<b.x<<")"<<" ";
        z.pop();
    }
    
     }
     else
     cout<<"无路径可走!"<<endl; 
    
}
int main()
{
cout<<"                                                "<<endl;
cout<<"                                                "<<endl;    
cout<<"<             欢迎来到迷宫程序之家 !           >"<<endl;
cout<<"                                                "<<endl;

    Sleep(1000);
    system("cls");
     mciSendString("play ll.wav",NULL,0,NULL);    
  int f,r;
cout<<"符号说明:○(   此符号为路        )"<<endl;
cout<<"符号说明:●(   此符号为墙        )"<<endl;
cout<<"符号说明:☆(此符号为寻路图标     )"<<endl;
cout<<"符号说明:# (此符号为走过一次的路 )"<<endl;
cout<<"符号说明:* (此符号为重复走过的路 )"<<endl;
cout<<"                                                "<<endl;
cout<<"                                                "<<endl;
cout<<"                                                "<<endl;
cout<<"<   请选择您的模式!(输入情况 1 或者情况 2 )   >" <<endl;
cout<<"                                                "<<endl;
cout<<"<   情况1:默认迷宫(可以显示路径返回过程)    >"<<endl;
cout<<"                                                "<<endl;
cout<<"<   情况2:随机迷宫(大概率不会遇到死路)      >" <<endl;
cout<<"                                                "<<endl;
cout<<"<            请收入您的选择( 1 or 2 ):         "<<endl; 
cout<<"                    ";
cin>>f;
if(f==1)
{
    xunlu(maze);
}
if(f==2)
{
    for(int i=0;i<=11;i++)//生成一个随机的迷宫 
    {
        for(int j=0;j<=11;j++)
        {
            r=rand()%10;
            if(r<=1)
            maze[i][j]=0;
            else
            maze[i][j]=1;    
            if(i==0||j==0||i==11||j==11)    
            maze[i][j]=0;
            maze[10][10]=1;
            maze[1][1]=1;
        }
     }         
     xunlu(maze);
}

    return 0;
}

    

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值