迷宫问题求解(用栈实现)

迷宫问题求解

任务:可以输入一个任意大小的迷宫数据,用非递归的方法求出一条走出迷宫的路径,并将路径输出;

要求:在上交资料中请写明:存储结构、基本算法(可以使用程序流程图)、源程序、测试数据和结果、算法的时间复杂度、另外可以提出算法的改进方法;



 #include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define MAX 500 //栈的最大值

struct Point
{
    int x,y;
    int vector[4],sum;///0 1 2 3 表示上下左右
    char c;
    int visit[4];
    Point operator=(Point &a)
    {
        x=a.x;y=a.y;sum=a.sum;c=a.c;
        for(int i=0;i<4;i++)
        {
            visit[i]=a.visit[i];
            vector[i]=a.vector[i];
        }
    }
}point[500][500];
typedef struct
{  Point base[MAX];
    int top;
}SqStack;
SqStack SqStackInit()//初始化栈
{
    SqStack S;
    S.top=-1;
    return S;
}
bool StackEmpty(SqStack S)//判断栈空
{
    if(S.top==-1)
        return true;
    else
        return false;
}
void SqStackPush(SqStack &S,Point x)//入栈
{
    if(S.top==MAX-1)
    {
        cout<<"栈满了";
        return ;
    }
    S.top++;
    S.base[S.top]=x;

}
void SqStackPop(SqStack &S)//出栈
{
    if(S.top==-1)
    {
        cout<<"栈满了";
        return ;
    }
    S.top--;
}
Point GetSqStackTop(SqStack S)//取栈顶元素
{
    return (S.base[S.top]);
}

char answer[500][500];
SqStack S=SqStackInit();
void input(int n,int m)
{
    cout<<"请输入迷宫矩阵,‘.’代表路径,‘x’代表墙壁\n";
    for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {cin>>point[i][j].c;point[i][j].x=i;point[i][j].y=j;}
}
void init(int n,int m)
{
    int a[4]={1,-1,0,0};
    int b[4]={0,0,-1,1};
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {   point[i][j].sum=0;
                for(int k=0;k<4;k++)
                    if(point[i][j].c=='.'&&point[i+a[k]][j+b[k]].c=='.')
                            {point[i][j].vector[k]=1;point[i][j].sum++;point[i][j].visit[k]=0;}
                    else {point[i][j].vector[k]=0;point[i][j].visit[k]=1;}
        }
}
void show(int n,int m)
{
    for(int i=1;i<=n;i++)
         {
             for(int j=1;j<=m;j++)
             cout<>n>>m)
    {
        memset(point,'x',sizeof(point));//初始化全墙壁
        input(n,m);
        init(n,m);

        //show2(n,m);
        solve(n,m);
        cout<<"输入行数和列数:\n";
    }
    return 0;
}



测试数据:(给一组,稍作改动即是多组)

17 54
.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
.xxxxxx.......xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx
.xxxxxx.xxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx........xx
........xxxx.xxxxxx..........xxxxxxxxxxxxxxxxx.xxxxxxx
xxxxxxx.xxxx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx
xxxxxxx.xxxx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx
xxxxxxx.xxxx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxx
xxxxxxx.xxxx.xxxxxx..................xxxxxxxxx.xxxxxxx
xxxxxxx.xxxx.xxxxxx.xxxxxxxxxxxxxxxx.xxxxxxxxx.xxxxxxx
xxxxxxx.............xxxxxxxxxxxxxxxx.xxxxxxxxx.xxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxx.xxxxxxx
xxxxxxxxxxxxxxxxxx...................xx........xxxxxxx
xxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxx.xxxxxx.xxxxxxx
xxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxx.xxxxxx.xxxxxxx
xxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxx.xxxxxx......xx
xxxxxxxxxxxxxxxxxx......................xxxxxxxxxxx..x
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..




你可能感兴趣的:(数据结构)