1. 程式人生 > >資料結構課程實驗->迷宮->分別用鏈棧,鏈隊,順序棧,順序隊實現。

資料結構課程實驗->迷宮->分別用鏈棧,鏈隊,順序棧,順序隊實現。

迷宮
    實現老鼠走迷宮程式。
    功能要求:①輸出每一步嘗試的狀態,直到走完迷宮或無法走通②在迷宮有解的前提下,輸出從起點到終點的最短通路。
    設計要求:迷宮至少10×10;起點和終點可自定義,也可固定;迷宮可隨機生成,也可固定,可以無解;兩個功能可用不同演算法和程式實現;輸出可以是圖形介面,也可以是文字介面(如下例)。
# # # # # # # # # #
# 0 # # 0 0 0 # # #
# 0 0 0 0 # 0 0 0 #
# 0 0 # # # # # 0 #
# # #    #      # 0 #
# # #      #  0 0 0 #
# # #     #   0 # # #
# # # # # # 0 # # #
# # # # # # 0 0 0 #
# # # # # # # # # #

 

//歡迎大佬批評。。。

//哪裡不懂的評論。。。

 

順序棧

#include<iostream>
#include<malloc.h>
#define maxsize 100
using namespace std;
#define M 8
#define N 8
int mg[M+2][N+2]=
{
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,0,0,0,1,1,0,1},
    {1,0,1,1,0,0,1,1,0,1},
    {1,0,1,1,0,1,1,1,0,1},
    {1,0,1,1,0,1,0,1,1,1},
    {1,1,0,1,0,1,0,0,1,1},
    {1,0,1,1,0,1,1,1,0,1},
    {1,0,0,1,0,1,1,1,1,1},
    {1,1,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}
};
typedef struct
{
   int i;
   int j;
   int di;
}box;
typedef struct
{
    box date[maxsize];
    int top;
}sqstack;
void initstack(sqstack *&s)
{
    s=(sqstack *)malloc(sizeof(sqstack));
    s->top=-1;
}
void destorystack(sqstack *&s)
{
    free(s);
}
bool stackempty(sqstack *s)
{
    return s->top==-1;
}
bool push(sqstack *&s,box &e)
{
    if(s->top==maxsize-1)
        return false;
    s->top++;
    s->date[s->top]=e;
    return true;
}
bool pop(sqstack *&s,box &e)
{
    if(s->top==-1)
        return false;
    e=s->date[s->top];
    s->top--;
    return true;
}
bool gettop(sqstack *&s,box &e)
{
    if(s->top==-1)
        return false;
    e=s->date[s->top];
    return true;
}
bool mgpath(int xi,int yi,int xe,int ye)
{
    box path[maxsize],e;
    int i,j,di,il,jl,k;
    bool find;
    sqstack *s;
    initstack(s);
    e.i=xi;
    e.j=yi;
    e.di=-1;
    push(s,e);
    mg[xi][yi]=-1;
    while(!stackempty(s))
    {
        gettop(s,e);
        i=e.i;
        j=e.j;
        di=e.di;
        if(xe==i&&ye==j)
        {
            cout<<"\t迷宮路徑如下:"<<endl;
            cout<<"\t";
            k=0;
            while(!stackempty(s))
            {
                pop(s,e);
                path[k++]=e;
            }
            while(k>=1)
            {
                k--;
                cout<<"("<<path[k].i<<','<<path[k].j<<") ";
            }
            cout<<endl;
            destorystack(s);
            return true;
        }
        find=false;
        while(di<4&&!find)
        {
            di++;
            switch(di)
            {
                case 0:il=i-1;jl=j;break;
                case 1:il=i;jl=j+1;break;
                case 2:il=i+1;jl=j;break;
                case 3:il=i;jl=j-1;break;
            }
            if(mg[il][jl]==0)
                find=true;
        }
        if(find)
        {
            s->date[s->top].di=di;
            e.i=il;
            e.j=jl;
            e.di=-1;
            push(s,e);
            mg[il][jl]=-1;
        }
        else
        {
            pop(s,e);
            mg[e.i][e.j]=0;
        }
    }
    destorystack(s);
    return false;
}
int main()
{
    if(!mgpath(1,1,M,N))
        cout<<"改迷宮沒有解"<<endl;
    return 0;
}

鏈棧

#include<iostream>
#include<malloc.h>
#define maxsize 100
using namespace std;
#define M 8
#define N 8
int mg[M+2][N+2]=
{
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,0,0,0,1,1,0,1},
    {1,0,1,1,0,0,1,1,0,1},
    {1,0,1,1,0,1,1,1,0,1},
    {1,0,1,1,0,1,0,1,1,1},
    {1,1,0,1,0,1,0,0,1,1},
    {1,0,1,1,0,1,1,1,0,1},
    {1,0,0,1,0,1,1,1,1,1},
    {1,1,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}
};
typedef struct
{
   int i;
   int j;
   int di;
}box;
typedef struct a
{
    box date;
    struct a *next;
}sqstack;
void initstack(sqstack *&s)
{
    s=(sqstack *)malloc(sizeof(sqstack));
    s->next=NULL;
}
void destorystack(sqstack *&s)
{
    sqstack *pre=s,*p=s->next;
    while(p!=NULL)
    {

        free(pre);
        pre=p;
        p=pre->next;
    }
    free(pre);
}
bool stackempty(sqstack *s)
{
    return (s->next==NULL);
}
bool push(sqstack *&s,box &e)
{
    sqstack *p;
    p=(sqstack *)malloc(sizeof(sqstack));
    p->date=e;
    p->next=s->next;
    s->next=p;
}
bool pop(sqstack *&s,box &e)
{
    sqstack *p;
    if(s->next==NULL)
        return false;
    p=s->next;
    e=p->date;
    s->next=p->next;
    free(p);
    return true;
}
bool gettop(sqstack *&s,box &e)
{
    e=s->next->date;
    return true;
}
bool mgpath(int xi,int yi,int xe,int ye)
{
    box path[maxsize],e;
    int i,j,di,il,jl,k;
    bool find;
    sqstack *s;
    initstack(s);
    e.i=xi;
    e.j=yi;
    e.di=-1;
    push(s,e);
    mg[xi][yi]=-1;
    while(!stackempty(s))
    {
        gettop(s,e);
        i=e.i;
        j=e.j;
        di=e.di;
        if(xe==i&&ye==j)
        {
            cout<<"\t迷宮路徑如下:"<<endl;
            cout<<"\t";
            k=0;
            while(!stackempty(s))
            {
                pop(s,e);
                path[k++]=e;
            }
            while(k>=1)
            {
                k--;
                cout<<"("<<path[k].i<<','<<path[k].j<<") ";
            }
            cout<<endl;
            destorystack(s);
            return true;
        }
        find=false;
        while(di<4&&!find)
        {
            di++;
            switch(di)
            {
                case 0:il=i-1;jl=j;break;
                case 1:il=i;jl=j+1;break;
                case 2:il=i+1;jl=j;break;
                case 3:il=i;jl=j-1;break;
            }
            if(mg[il][jl]==0)
                find=true;
        }
        if(find)
        {
            s->date.di=di;
            e.i=il;
            e.j=jl;
            e.di=-1;
            push(s,e);
            mg[il][jl]=-1;
        }
        else
        {
            pop(s,e);
            mg[e.i][e.j]=0;
        }
    }
    destorystack(s);
    return false;
}
int main()
{
    if(!mgpath(1,1,M,N))
        cout<<"改迷宮沒有解"<<endl;
    return 0;
}

順序隊

#include<iostream>
#include<malloc.h>
#define maxsize 100
using namespace std;
int mg[10][10]=
{
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,0,0,1,1,0,0,1},
    {1,0,1,1,1,0,0,0,0,1},
    {1,0,0,0,1,0,0,0,0,1},
    {1,0,1,0,0,0,1,0,0,1},
    {1,0,1,1,1,0,1,1,0,1},
    {1,1,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}
};
typedef struct
{
    int i,j;
    int pre;
}box;
typedef struct
{
    box date[maxsize];
    int front,rear;
}qutype;
void initqueue(qutype *&q)
{
    q=(qutype *)malloc(sizeof(qutype));
    q->front=q->rear=-1;
}
bool enqueue(qutype *&q,box &e)
{
    if(q->rear==maxsize-1)
        return false;
    q->rear++;
    q->date[q->rear]=e;
    return true;
}
bool dequeue(qutype *&q,box &e)
{
    if(q->rear==q->front)
        return false;
    q->front++;
    e=q->date[q->front];
    return true;
}
bool destoryqueue(qutype *&q)
{
    free(q);
}
bool emptyqueue(qutype *&q)
{
    return (q->rear==q->front);
}
void print(qutype *&q,int front)
{
    int k=front,j,ans=0;
    cout<<endl;
    do
    {
        j=k;
        k=q->date[k].pre;
        q->date[j].pre=-1;
    }while(k!=0);
    cout<<"\t迷宮路徑如下:"<<endl;
    cout<<"\t";
    k=0;
    while(k<maxsize)
    {
        if(q->date[k].pre==-1)
        {
            cout<<"("<<q->date[k].i<<','<<q->date[k].j<<") ";
        }
        k++;
    }
    cout<<endl;
}
bool mgpath1(int xi,int yi,int xe,int ye)
{
    box e;
    int i,j,di,il,jl;
    qutype *q;
    initqueue(q);
    e.i=xi;
    e.j=yi;
    e.pre=-1;
    enqueue(q,e);
    mg[xi][yi]=-1;
    while(!emptyqueue(q))
    {
        dequeue(q,e);
        i=e.i;
        j=e.j;
        if(xe==i&&ye==j)
        {
            print(q,q->front);
            destoryqueue(q);
            return true;
        }
        for(di=0;di<4;di++)
        {
            switch(di)
            {
                case 0:il=i-1;jl=j;break;
                case 1:il=i;jl=j+1;break;
                case 2:il=i+1;jl=j;break;
                case 3:il=i;jl=j-1;break;
            }
            if(mg[il][jl]==0)
            {
                e.i=il;
                e.j=jl;
                e.pre=q->front;
                enqueue(q,e);
                mg[il][jl]=-1;
            }
        }
    }
    destoryqueue(q);
    return false;
}
int main()
{
    if(!mgpath1(1,1,8,8))
        cout<<"該迷宮無解"<<endl;
    return 0;
}

 

鏈隊

#include<iostream>
#include<cstring>
#include<cstring>
#include<malloc.h>
using namespace std;
int map[10][10]={
    {1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,1,0,0,0,1,0,1},
    {1,0,0,0,0,1,1,0,0,1},
    {1,0,1,1,1,0,0,0,0,1},
    {1,0,0,0,1,0,0,0,0,1},
    {1,0,1,0,0,0,1,0,0,1},
    {1,0,1,1,1,0,1,1,0,1},
    {1,1,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1}
};
int m,n;
int point[4][2]={1,0,0,1,-1,0,0,-1};
int startx,starty,endx,endy;
int e1,e2;
int ans=1;
bool flag=false;
bool vis[1000][10000];
typedef struct linknode
{
    int xx,yy;
    int data;
    struct linknode *pre;
    struct linknode *next;
}qutype;

void InitQueue(qutype *&s)
{
    s=(qutype *)malloc(sizeof(qutype));
    s->next=NULL;
}

void DestoryQueue(qutype *&s)
{
    qutype *pre=s,*p=s->next;
    while(p!=NULL)
    {
        free(pre);
        pre=p;
        p=p->next;
    }
    free(pre);
}

bool QueueEmpty(qutype *s)
{
    return s->next==NULL?true:false;
}

void Enqueue(qutype *&s,int e1,int e2)
{
    qutype *p,*t;
    t=s;
    while(t->next!=NULL)
        t=t->next;
    p=(qutype *)malloc(sizeof(qutype));
    p->xx=e1;
    p->yy=e2;
    t->next=p;
    p->next=NULL;
}
void Enqueue1(qutype *&head,int e1,int e2,int x1,int x2)
{
    qutype *p,*t,*r=NULL;
    t=head;
    while(t->next!=NULL)
    {
        if(t->xx==x1&&t->yy==x2)
            r=t;
        t=t->next;
    }
    p=(qutype *)malloc(sizeof(qutype));
    p->xx=e1;
    p->yy=e2;
    t->next=p;
    p->next=NULL;
    p->pre=r;
}
bool Dequeue(qutype *&s)
{
    qutype *p;
    if(s->next==NULL)
        return false;
    p=s->next;
    s->next=p->next;
    free(p);
    return true;
}
qutype GetTop(qutype *s)
{
    linknode xy;
    if(s->next==NULL)
    {
        xy.xx=-1;
        xy.yy=-1;
    }
    else
    {
        xy.xx=s->next->xx;
        xy.yy=s->next->yy;
    }
    return xy;
}

bool check(int x,int y)
{
    if(map[x][y]==0&&vis[x][y]==false)
        return true;
    return false;
}

void display(qutype *&head,int end_x,int end_y)
{
    linknode *p;
    p=head;
    while(p)
    {
        if(p->xx==end_x&&p->yy==end_y)
            break;
        p=p->next;
    }
    while(p->pre!=NULL)
    {
        cout<<"("<<p->xx<<","<<p->yy<<")"<<" ";
        p=p->pre;
    }
    cout<<"("<<startx<<","<<startx<<")"<<endl;
}
void BFS(qutype *&s,qutype *&head,int x,int y)
{
    vis[x][y]=true;
    Enqueue(s,x,y);
    Enqueue1(head,x,y,-1,-1);
    while(!QueueEmpty(s))
    {
        linknode xy;
        xy=GetTop(s);
        if(xy.xx==endx&&xy.yy==endy)
        {
            cout<<endl;
            cout<<"\tDisplay!"<<endl;
            cout<<"\t";
            display(head, endx, endy);
            flag=true;
            break;
        }
        Dequeue(s);
        for(int i=0;i<4;i++)
        {
            int new_x=xy.xx+point[i][0];
            int new_y=xy.yy+point[i][1];
            if(new_x>=0&&new_x<m&&new_y>=0&&new_y<n&&check(new_x, new_y))
            {
                vis[xy.xx+point[i][0]][xy.yy+point[i][1]]=true;
                Enqueue(s, xy.xx+point[i][0], xy.yy+point[i][1]);
                Enqueue1(head,xy.xx+point[i][0],xy.yy+point[i][1],xy.xx,xy.yy);
            }
        }
    }
    DestoryQueue(s);
    if(!flag)
    {
        cout<<"NO_Way"<<endl;
    }
}
int main()
{
    qutype *s;
    qutype *head;
    InitQueue(s);
    InitQueue(head);
    m=10;n=10;
    startx=1;starty=1;
    endx=8;
    endy=8;
    memset(vis ,false,sizeof vis);
    head->xx=startx;
    head->yy=starty;
    head->pre=NULL;
    head->next=NULL;
    BFS(s,head,startx,starty);
    return 0;
}

 

//強行上棧上佇列。。。