迷宮改錯
提問者: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
回答數:114 | 被采納數:91
2017-02-18 22:29:54
批量替換stack s為stack& s
改完了,就出來一個點,我覺得我的path函數沒有錯,看了挺多次的
#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;
}
這是修改完的,還是有問題
#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);}
}