您的位置:首页 > 其它

第5周实践项目5 迷宫问题(栈和队列实现)

2017-10-07 11:22 344 查看
#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,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;
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<<"一条迷宫路径如下"<<endl;
k=0;
while(!stackempty(s))
{
pop(s,e);
path[k++]=e;
}
while(k>=1)
{
k--;
cout<<" ("<<path[k].i<<'.'<<path[k].j<<')';
if((k+2)%5==0)
cout<<endl;
}
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;
}




队列实现bug:

#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
4000
,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=new box[maxsize]
q->front=q->rear=-1;
}
bool enqueue(qutype *&q,box &e)
{
if(q->rear==maxsize-1)
return false;
q->rear=q->rear+1;
q->date[q->rear]=e;
return true;
}
bool dequeue(qutype *&q,box &e)
{
if(q->rear==q->front)
return false;
q->front=q->front+1;
e=q->date[q->front];
return true;
}
bool destoryqueue(qutype *&q)
{
free(q);//delete[] date;
}
bool emptyqueue(qutype *&q)
{
return q->rear==q->front;
}
void print(qutype *&q,int front)
{
/*int k,ans=0;//自己的
for(int tag=front;tag!=-1;)
{
k=tag;
tag=q->date[tag].pre;
q->date[k].pre=-1;
}*/
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<<"一条路径如下:"<<endl;
k=0;
while(k<maxsize)
{
if(q->date[k].pre==-1)
{
ans++;
cout<<" ("<<q->date[k].i<<','<<q->date[k].j<<')';
if(ans%5==0)
cout<<endl;
}
++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);//(il,jl)方块进队
mg[il][jl]=-1;//将其赋值避免回过来反复搜索
}
}
}
destoryqueue(q);//销毁队列
return false;//未找到任何路径时返回假
}
int main()
{
if(!mgpath1(1,1,8,8))
cout<<"该迷宫无解"<<endl;
return 0;
}


转载一篇用类实现的 http://blog.csdn.net/seu_nuaa_zc/article/details/73188376?locationNum=8&fps=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: