当前位置:系统粉 > 电脑问答 > 其他问答 > 迷宫改错

迷宫改错

提问者:wfs646551  |  浏览 次  |  提问时间:2017-02-18  |  回答数量:5

迷宫改错 #define initstack_size200#include<iostream>using namespace std;int M[10][10]={0,0,0,0,0,0,0,0,0,0, 0,1,1,0,1,1,1,0,1,0, 0,1,1,0,1,1,1,0,1,0, 0,1,1,1,1,0,0,1,1,0, 0,1,0,0,0,1,1,1,1,0, 0,1,1,1,0,1,1,1,1,0, 0,1,0,1,1,1,0,1,1,0, 0,1,0,0,0,1,0,0,1,0, 0,0,1,1,1,1,1,1,1,0, 0,0,0,0,0,0,0,0,0,0};int zx[5],zy[5];typedef struct//定义坐标类,x,y分别为横纵坐标{int x;int y;}position;typedef struct //定义结构体中包含坐标和方向标记{//其中di为1,2,3,4分别代表向右,向上,向左,向下position seat;int di;}stackelem;typedef struct //定义一个栈结构体,其中包含栈顶指针和栈底指针,

已有5条答案
2211828499

2211828499

回答数:114  |  被采纳数:91

2017-02-18 22:29:54
批量替换stack s为stack& s
赞 11
雨中盛开之花

雨中盛开之花

回答数:46  |  被采纳数:72

2017-02-19 02:37:45
哦了,我试试
赞 4
指纹诉说着

指纹诉说着

回答数:171  |  被采纳数:2

2017-02-19 02:07:45
改完了,就出来一个点,我觉得我的path函数没有错,看了挺多次的
赞 17
边脚趾姆儿丶

边脚趾姆儿丶

回答数:141  |  被采纳数:72

2017-02-19 08:27:27
#define initstack_size200
#include<iostream>
using namespace std;
int M[10][10]={0,0,0,0,0,0,0,0,0,0,
0,1,1,0,1,1,1,0,1,0,
0,1,1,0,1,1,1,0,1,0,
0,1,1,1,1,0,0,1,1,0,
0,1,0,0,0,1,1,1,1,0,
0,1,1,1,0,1,1,1,1,0,
0,1,0,1,1,1,0,1,1,0,
0,1,0,0,0,1,0,0,1,0,
0,0,1,1,1,1,1,1,1,0,
0,0,0,0,0,0,0,0,0,0};
int zx[5],zy[5];

typedef struct//定义坐标类,x,y分别为横纵坐标
{
int x;
int y;
}position;

typedef struct //定义结构体中包含坐标和方向标记
{//其中di为1,2,3,4分别代表向右,向上,向左,向下
position seat;
int di;


}stackelem;


typedef struct //定义一个栈结构体,其中包含栈顶指针和栈底指针,以及栈的大小
{
stackelem *base;
stackelem *top;
int stack_size;
}stack;
stack s;
void initialize(stack &s) //将栈初始化为空栈,并且开辟空间
{

s.base=(stackelem *)malloc(initstack_size*sizeof(stackelem)) ;
s.top=s.base;
s.stack_size=initstack_size;
}

int empty(stack &s)//empty为判断函数,如果栈为空,则返回一,否则返回零
{
if(s.base==s.top)
return 1;
else
return 0;
}
void push(stack &s,stackelem e) //入栈函数,将stackelem类型的变量压入栈
{
*s.top++=e;
}


stackelem pop(stack &s)//出栈函数,并且返回stackelem类型的变量,即返回出栈的元素
{
stackelem e;
e=*--s.top;
return e;

}
int pass(stackelem e)//此为判断是否为可通过函数,如果为点的值为1则可通过,否则不可通过
{
int i,j;
i=e.seat.x;
j=e.seat.y;
if(M[j]==1)
return 1;
else return 0;
}
stackelem nextstep(stackelem curpos,int di) //下移函数,将目前的位置移动,并返回一个stackelem变量
{
curpos.seat.x=curpos.seat.x+zx[di];
curpos.seat.y=curpos.seat.y+zy[di];
curpos.di=1; //默认每个点的方向都是向右;
return(curpos);
}
void footprint(stackelem e) //留下足迹函数,走过的标记为-1
{
int i,j;
i=e.seat.x;
j=e.seat.y;
M[j]=-1;
}
void markprint(stackelem e) //标记函数,将此刻的点的值标记为0
{
int i,j;
i=e.seat.x;
j=e.seat.y;
M[j]=0;
}
void path(stackelem start,stackelem end,stack &s) //路径函数,接收(开始节点,结束节点,和栈)
{
stackelem curpos;
curpos=start;//将stackelem类型的curpos变量赋初值
do
{
if(pass(curpos))//判断当前的curpos是否可通
{

push(s,curpos); //如果可通过,则压入栈;
footprint(curpos);
if((curpos.seat.x=end.seat.x)&&(curpos.seat.y=end.seat.y))//判断此刻的curpos是否和end相等,如果相等,则终止;
break;

curpos=nextstep(curpos,curpos.di); //不相等就移动到下一个stackelem类型的变量的位置

}
else
{

while(!empty(s)) //不可通过,而且栈不空
{
curpos=pop(s); //后退一步,出栈,取出栈顶元素赋值给curpos

while(curpos.di==4&&!empty(s))//如果curpos的方向值为4,而且栈非空
{
markprint(curpos);//做标记,这是个死胡同,标记此点的值为0;

curpos=pop(s);//出栈,后退一步,取出栈顶元素赋值给curpos

}
if(curpos.di<4)//curpos的di值小于四,说明还没有将此点的四周都尝完
{
curpos.di++; //将curpos的di值加一,也是为了改变方向;
push(s,curpos);//将curpos压入栈;
curpos=nextstep(curpos,curpos.di);//根据新的方向移动,可以找的新的点,赋值给curpos
}

}
}
}while(!empty(s));//只要栈不空,就进行循环;
}



int main()
{
stackelem start,end,pathelem; //定义stackelem类型的变量;

zx[1]=1,zy[1]=0,zx[2]=0,zy[2]=-1,zx[3]=-1,zy[3]=0,zx[4]=0,zy[4]=1;

start.di=1;
start.seat.x=1;
start.seat.y=1;
end.seat.x=8;
end.seat.y=8;
end.di=1;
initialize(s);
path(start,end,s);
while(!empty(s))
{
pathelem=pop(s);
cout<<pathelem.seat.x<<pathelem.seat.y<<endl;
}

return 0;
}
这是修改完的,还是有问题
赞 14
杦蓝

杦蓝

回答数:188  |  被采纳数:109

2017-02-19 04:24:55
#define stack_init_size 200

#define overflow 0
#define ok 1
#define error 0

#include<stdio.h>
#include<malloc.h>

typedef int Status;

typedef struct{
int x;
int y;
}PosType;

int mg[10][10]={ 0,0,0,0,0,0,0,0,0,0,
0,1,1,0,1,1,1,0,1,0,
0,1,1,0,1,1,1,0,1,0,
0,1,1,1,1,0,0,1,1,0,
0,1,0,0,0,1,1,1,1,0,
0,1,1,1,0,1,1,1,1,0,
0,1,0,1,1,1,0,1,1,0,
0,1,0,0,0,1,0,0,1,0,
0,0,1,1,1,1,1,1,1,0,
0,0,0,0,0,0,0,0,0,0};


int zx[5],zy[5];

typedef struct {
int ord;
PosType seat;
int di;
}SElemType;

typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;

SqStack s;

//构造一个空栈
Status InitStack(SqStack &s){
s.base =(SElemType *)malloc(stack_init_size * sizeof(SElemType));
if(!s.base) return overflow;
s.top=s.base;
s.stacksize=stack_init_size;
return ok;
}

//当前块可否通过
Status Pass(PosType e){
int i,j;
i=e.x;
j=e.y;
if (mg[j]==1)//
return ok;
return overflow;
}

//留下足迹
Status FootPrint(PosType e){
int i,j;
i=e.x;
j=e.y;
mg[j]=-1;
return ok;}

//压入栈
Status Push(SqStack &s,SElemType e){
*s.top++=e;
return ok;}

//出栈
Status Pop(SqStack &s,SElemType &e){
if(s.top==s.base)
return error;
e=*--s.top;
return ok;
}

//下一步
PosType NextPos(PosType &e,int di){
e.x=e.x+zx[di];
e.y=e.y+zy[di];
return e;
}

//是否空栈
Status StackEmpty(SqStack s){
if (s.top==s.base)
return ok;
return overflow;
}

//留下不能通过的足迹
Status MarkPrint(PosType e){
int i,j;
i=e.x;
j=e.y;
mg[j]=0;
return ok;}

//迷宫函数
Status MazePath(PosType start,PosType end,SqStack &s){
PosType curpos;
SElemType e;
int curstep;

curpos=start;
curstep=1;
do{
if(Pass(curpos)){
FootPrint(curpos);
e.ord=curstep;
e.seat.x=curpos.x;
e.seat.y=curpos.y;
e.di=1;
Push(s,e);
if((curpos.x==end.x)&&(curpos.y==end.y))
return ok;
curpos=NextPos(curpos,1);
curstep++;}//if
else{
if(!StackEmpty(s)){
Pop(s,e);
while(e.di==4 && !StackEmpty(s)){
MarkPrint(e.seat);
Pop(s,e);
}//while
if(e.di<4){
e.di++;
Push(s,e);
curpos=NextPos(e.seat,e.di);
}//if
}//if
}//else
}while(!StackEmpty(s));
return error;}//MazePath

//主函数
void main()
{
PosType start,end;
int x,y;
SElemType e;
InitStack(s);
start.x=1;
start.y=1;
end.x=8;
end.y=8;
zx[1]=0;
zy[1]=1;
zx[2]=1;
zy[2]=0;
zx[3]=0;
zy[3]=-1;
zx[4]=-1;
zy[4]=0;
MazePath(start,end,s);
while(!StackEmpty(s))
{Pop(s,e);
x=e.seat.x;
y=e.seat.y;
printf(("%d,%d"),x,y);}

}
赞 18
解决方法
版权信息

Copyright @ 2011 系统粉 版权声明 最新发布内容 网站导航