数据结构--迷宫问题

/* 
设计一个迷宫求解程序,要求如下:
√ 以M × N表示长方阵表示迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。
√ 能任意设定的迷宫
*/


#include <iostream>
using namespace std;


#define MaxSize 1000


int mg[MaxSize][MaxSize];


typedef struct{
    int i;                   //当前方块的行号 
    int j;                   //当前方块的列号 
    int di;                  // di是下一个可走的相邻方块的方位号 
}Box;


typedef struct{
    Box data[MaxSize];
    int top;                 //栈顶指针 
}StType;                     //定义顺序栈类型 


bool mgpath(int xi,int yi,int xe,int ye){     //求解路径(xi,yi)->(xe,ye) 
    int i,j,k,di,find;
    StType st;
    st.top=-1;
    st.top++;
    st.data[st.top].i=xi;   st.data[st.top].j=yi;
    st.data[st.top].di=-1;  mg[xi][yi]=-1;
    while(st.top>-1){
        i=st.data[st.top].i;   j=st.data[st.top].j;
        di=st.data[st.top].di;

        if(i=xe && j==ye){
            cout<<"迷宫路径如下:"<<endl;
            for(k=0;k<=st.top;k++){
                cout<<"\t("<<st.data[k].i<<","<<st.data[k].j<<")";
                if((k+1) % 5 == 0)
                    cout<<endl;
            }
            cout<<endl;
            return true;
        }
    
        find = 0;

        while(di<4&&find==0){
            di++;
            switch(di){
                case 0:i=st.data[st.top].i-1;j=st.data[st.top].j;break;
                case 1:i=st.data[st.top].i;j=st.data[st.top].j+1;break;
                case 2:i=st.data[st.top].i+1;j=st.data[st.top].j;break;
                case 3:i=st.data[st.top].i;j=st.data[st.top].j-1;break;
               }
            if(mg[i][j]==0)  find=1;
        }

        if(find==1){
            st.data[st.top].di=di;
            st.top++;
            st.data[st.top].i=i; st.data[st.top].j=j;
            st.data[st.top].di=-1;
            mg[i][j]=-1;
        }
        else{
            mg[st.data[st.top].i][st.data[st.top].j]=0;
            st.top--;
        }
    }
return false;
} 


int main(){
    int M,N;
    int a,b;
    int c,d;
    cout<<"设置迷宫大小:\t"; 
    cin>>M>>N;        //设置迷宫大小
    for(int i=0;i<M+2;i++)
        for(int j=0;j<N+2;j++)
    cin>>mg[i][j];
    cout<<"设置入口值\t";
    cin>>a>>b;
    cout<<"设置出口值\t";
    cin>>c>>d;
    if(!mgpath(a,b,c,d))
        cout<<"该迷宫问题无解"<<endl;
    return 0; 
}

#include <iostream>
using namespace std;


#define MaxSize 1000


int mg[MaxSize][MaxSize];


typedef struct{
int i;                   //当前方块的行号 
int j;                   //当前方块的列号 
int di;                  // di是下一个可走的相邻方块的方位号 
}Box;


typedef struct{
Box data[MaxSize];
int top;                 //栈顶指针 
}StType;                     //定义顺序栈类型 


bool mgpath(int xi,int yi,int xe,int ye){     //求解路径(xi,yi)->(xe,ye) 
int i,j,k,di,find;
StType st;
st.top=-1;
st.top++;
st.data[st.top].i=xi;   st.data[st.top].j=yi;
st.data[st.top].di=-1;  mg[xi][yi]=-1;
while(st.top>-1){
i=st.data[st.top].i;   j=st.data[st.top].j;
di=st.data[st.top].di;

if(i=xe && j==ye){
cout<<"迷宫路径如下:"<<endl;
for(k=0;k<=st.top;k++){
cout<<"\t("<<st.data[k].i<<","<<st.data[k].j<<")";
if((k+1) % 5 == 0)
cout<<endl;
}
cout<<endl;
return true;
}

find = 0;

while(di<4&&find==0){
di++;
switch(di){
case 0:i=st.data[st.top].i-1;j=st.data[st.top].j;break;
case 1:i=st.data[st.top].i;j=st.data[st.top].j+1;break;
case 2:i=st.data[st.top].i+1;j=st.data[st.top].j;break;
case 3:i=st.data[st.top].i;j=st.data[st.top].j-1;break;
}
if(mg[i][j]==0)  find=1;
}

if(find==1){
st.data[st.top].di=di;
st.top++;
st.data[st.top].i=i; st.data[st.top].j=j;
st.data[st.top].di=-1;
mg[i][j]=-1;
}
else{
mg[st.data[st.top].i][st.data[st.top].j]=0;
st.top--;
}
}
return false;



int main(){
int M,N;
int a,b;
int c,d;
cout<<"设置迷宫大小:\t"; 
cin>>M>>N;        //设置迷宫大小
for(int i=0;i<M+2;i++)
for(int j=0;j<N+2;j++)
cin>>mg[i][j];
cout<<"设置入口值\t";
cin>>a>>b;
cout<<"设置出口值\t";
cin>>c>>d;
if(!mgpath(a,b,c,d))
cout<<"该迷宫问题无解"<<endl;
return 0; 
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

【ACGO】我不会C++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值