严蔚敏的数据结构上的迷宫求解问题,我自己编的c代码,那位高手跟我改一下,谢谢!
#include<stdio.h>#defineSTACK-INIT-SIZE100#defineSTACKINCREMENT10typedefstruct{intord...
#include<stdio.h>
#define STACK-INIT-SIZE 100
#define STACKINCREMENT 10
typedef struct
{ int ord;
int p;
int q;
}SELETYPE;
SELETYPE e;
typedef struct
{ SELETYPE*base;
SELETYPE*top;
int stacksize;
}sqstack;
int Initstack(sqstack*s)
{s->base=(SELETYPE*)malloc(STACK-INIT-SIZE*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK-INIT-SIZE;
return ok;
}
int push(sqstack*s,SELETYPE f)
{if(s->top-s->base>=s->stacksize)
{s->base=(SELETYPE*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=f;
return ok;
}
int pop(sqstack*s,SELETYPE f)
{if(s->top==s->base)
return ERROR;
f=*--s.top;
return ok;
}
int stackempty(sqstack*s)
{if(s->top==s->base)
return ture;
else
return false;
}
void print()
{int i,j;
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
printf("%3d",a[i][j]);
}
}
int main()
{int i,j,a[10][10],r1,r2,c1,c2,curstep=1;
sqstack*p;
printf("please input the enter:");
scanf("%d%d",&r1,&c1);
printf("please input the exit:")
scanf("%d%d",&r2,&c2);
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
scanf("%d",a[i][j]);
}
do
{if(a[r1][c1]==1)
{a[r1][c1]=curstep;
e.ord=curstep;
e.p=r1;
e.q=c1;
push(p,e);
if(r1==r2&&c1==c2)
return ture;
c1++;
curstep++;
}
else
{if(!stackempty)
{pop(p,e);
while(dr==4&&!stackempty(s))
{a[r1][c1]=-1;
pop(p,e);
}
if(e.dr<4)
{e.dr++;
push(p,e);
switch(e.dr)
case 2:
r1--;
case 3:
c1--;
case 4:
r1++;
}
}
}
}while(!stackempty(s))
print();
} 展开
#define STACK-INIT-SIZE 100
#define STACKINCREMENT 10
typedef struct
{ int ord;
int p;
int q;
}SELETYPE;
SELETYPE e;
typedef struct
{ SELETYPE*base;
SELETYPE*top;
int stacksize;
}sqstack;
int Initstack(sqstack*s)
{s->base=(SELETYPE*)malloc(STACK-INIT-SIZE*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK-INIT-SIZE;
return ok;
}
int push(sqstack*s,SELETYPE f)
{if(s->top-s->base>=s->stacksize)
{s->base=(SELETYPE*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=f;
return ok;
}
int pop(sqstack*s,SELETYPE f)
{if(s->top==s->base)
return ERROR;
f=*--s.top;
return ok;
}
int stackempty(sqstack*s)
{if(s->top==s->base)
return ture;
else
return false;
}
void print()
{int i,j;
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
printf("%3d",a[i][j]);
}
}
int main()
{int i,j,a[10][10],r1,r2,c1,c2,curstep=1;
sqstack*p;
printf("please input the enter:");
scanf("%d%d",&r1,&c1);
printf("please input the exit:")
scanf("%d%d",&r2,&c2);
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
scanf("%d",a[i][j]);
}
do
{if(a[r1][c1]==1)
{a[r1][c1]=curstep;
e.ord=curstep;
e.p=r1;
e.q=c1;
push(p,e);
if(r1==r2&&c1==c2)
return ture;
c1++;
curstep++;
}
else
{if(!stackempty)
{pop(p,e);
while(dr==4&&!stackempty(s))
{a[r1][c1]=-1;
pop(p,e);
}
if(e.dr<4)
{e.dr++;
push(p,e);
switch(e.dr)
case 2:
r1--;
case 3:
c1--;
case 4:
r1++;
}
}
}
}while(!stackempty(s))
print();
} 展开
1个回答
展开全部
#include<stdio.h>
#define STACK_INIT_SIZE 100 /*标识符定义不能使用减号*/
#define STACKINCREMENT 10
#define ERROR -1 /*添加ERROR定义*/
#define OVERFLOW -2 /*添加OVERFLOW定义*/
#define ok 1 /*添加ok定义*/
#define true 1 /*添加true定义,下文中ture全改为true*/
#define false 0 /*添加false定义*/
typedef struct
{ int ord;
int p;
int q;
}SELETYPE;
SELETYPE e;
typedef struct
{ SELETYPE*base;
SELETYPE*top;
int stacksize;
}sqstack;
int a[10][10]; /*你在print()函数中使用了a[][],必须定义为全局变量*/
int Initstack(sqstack*s)
{s->base=(SELETYPE*)malloc(STACK_INIT_SIZE*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE; /*修改,全改为STACK_INIT_SIZE*/
return ok;
}
int push(sqstack*s,SELETYPE f)
{if(s->top-s->base>=s->stacksize)
{s->base=(SELETYPE*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=f;
return ok;
}
int pop(sqstack*s,SELETYPE f)
{if(s->top==s->base)
return ERROR;
f=*--s->top; /* s.top 改为s->top */
return ok;
}
int stackempty(sqstack*s)
{if(s->top==s->base)
return true;
else
return false;
}
void print()
{int i,j;
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
printf("%3d",a[i][j]);
}
}
int main()
{int i,j,r1,r2,c1,c2,curstep=1;
sqstack*p;
printf("please input the enter:");
scanf("%d%d",&r1,&c1);
printf("please input the exit:");
scanf("%d%d",&r2,&c2);
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
scanf("%d",&a[i][j]); /* 输入 a[i][j] 改为 &(a[i][j])*/
}
do
{if(a[r1][c1]==1)
{a[r1][c1]=curstep;
e.ord=curstep;
e.p=r1;
e.q=c1;
push(p,e);
if(r1==r2&&c1==c2)
return true;
c1++;
curstep++;
}
else
{if(!stackempty)
{pop(p,e);
while(e.ord==4&&!stackempty(p)) /* dr 改为 e.ord, s 改为 p */
{a[r1][c1]=-1;
pop(p,e);
}
if(e.ord<4) /* e.rd 改为 e.ord */
{e.ord++; /* e.rd 改为 e.ord */
push(p,e);
switch(e.ord) /* e.rd 改为 e.ord */
{ /* switch 后加上花括号 */
case 2:
r1--;
break; /* case 后要加上 break; */
case 3:
c1--;
break; /* case 后要加上 break; */
case 4:
r1++;
break; /* case 后要加上 break; */
} /* switch 后加上花括号 */
}
}
}
}while(!stackempty(p)); /* s 改为 p */
print();
}
#define STACK_INIT_SIZE 100 /*标识符定义不能使用减号*/
#define STACKINCREMENT 10
#define ERROR -1 /*添加ERROR定义*/
#define OVERFLOW -2 /*添加OVERFLOW定义*/
#define ok 1 /*添加ok定义*/
#define true 1 /*添加true定义,下文中ture全改为true*/
#define false 0 /*添加false定义*/
typedef struct
{ int ord;
int p;
int q;
}SELETYPE;
SELETYPE e;
typedef struct
{ SELETYPE*base;
SELETYPE*top;
int stacksize;
}sqstack;
int a[10][10]; /*你在print()函数中使用了a[][],必须定义为全局变量*/
int Initstack(sqstack*s)
{s->base=(SELETYPE*)malloc(STACK_INIT_SIZE*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base;
s->stacksize=STACK_INIT_SIZE; /*修改,全改为STACK_INIT_SIZE*/
return ok;
}
int push(sqstack*s,SELETYPE f)
{if(s->top-s->base>=s->stacksize)
{s->base=(SELETYPE*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SELETYPE));
if(!s->base)
exit(OVERFLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*s->top++=f;
return ok;
}
int pop(sqstack*s,SELETYPE f)
{if(s->top==s->base)
return ERROR;
f=*--s->top; /* s.top 改为s->top */
return ok;
}
int stackempty(sqstack*s)
{if(s->top==s->base)
return true;
else
return false;
}
void print()
{int i,j;
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
printf("%3d",a[i][j]);
}
}
int main()
{int i,j,r1,r2,c1,c2,curstep=1;
sqstack*p;
printf("please input the enter:");
scanf("%d%d",&r1,&c1);
printf("please input the exit:");
scanf("%d%d",&r2,&c2);
for(i=0;i<10;i++)
{printf('\n');
for(j=0;j<10;j++)
scanf("%d",&a[i][j]); /* 输入 a[i][j] 改为 &(a[i][j])*/
}
do
{if(a[r1][c1]==1)
{a[r1][c1]=curstep;
e.ord=curstep;
e.p=r1;
e.q=c1;
push(p,e);
if(r1==r2&&c1==c2)
return true;
c1++;
curstep++;
}
else
{if(!stackempty)
{pop(p,e);
while(e.ord==4&&!stackempty(p)) /* dr 改为 e.ord, s 改为 p */
{a[r1][c1]=-1;
pop(p,e);
}
if(e.ord<4) /* e.rd 改为 e.ord */
{e.ord++; /* e.rd 改为 e.ord */
push(p,e);
switch(e.ord) /* e.rd 改为 e.ord */
{ /* switch 后加上花括号 */
case 2:
r1--;
break; /* case 后要加上 break; */
case 3:
c1--;
break; /* case 后要加上 break; */
case 4:
r1++;
break; /* case 后要加上 break; */
} /* switch 后加上花括号 */
}
}
}
}while(!stackempty(p)); /* s 改为 p */
print();
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
厦门鲎试剂生物科技股份有限公司
2023-08-01 广告
2023-08-01 广告
BG试验又称为G试验,是一种基于真菌细胞壁成分的血清学试验。BG试验检测的是真菌细胞壁中的葡聚糖成分。操作步骤如下:1. 左键单击【View】2. 左键单击【Residual Diagnostics】3. 左键单击【Series Corre...
点击进入详情页
本回答由厦门鲎试剂生物科技股份有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询