帮帮忙吧……大难题!

yach_jerry 2007-04-30 04:24:13
请看以下一段程序:
#include <iostream.h>
#include <stdio.h>
#define M 100
int z[50][50];

typedef struct
{
int h,l;
int data;
}Node;

typedef struct
{
Node a[100];
int front,rear;
}ser;

ser s;



void jing(Node temp)
{

s.a[s.rear].h=temp.h;
s.a[s.rear].l=temp.l;
s.a[s.rear].data=temp.data;
s.rear=(s.rear+1)%M;
}





void chu(Node &temp)
{
temp.h=s.a[s.front].h;
temp.l=s.a[s.front].l;
temp.data=s.a[s.front].data;
s.front=(s.front+1)%M;
}



void qingkong(int m,int n)
{
int p,q;
for (p=0;p<n;p++)
for (q=0;q<m;q++)
z[p][q]=0;
}



void qiang(int m,int n)
{
int p,q,k;
printf ("Please enter the num of qiang heng ,zong and k(heng<%d,zong<%d),break of k=-1:\n\n",m,n);
do{
cin>>p>>q>>k;
p=p-1;
q=q-1;
z[p][q]=-1;
}while(k!=-1);
}



int fuzhi(int x1,int y1,int x2,int y2,int m,int n)
{
Node temp;
temp.h=x1;
temp.l=y1;
temp.data=0;
jing(temp);
do{
chu(temp);
if ((temp.h-1>=0)&&(z[temp.h-1][temp.l]==0))
{
z[temp.h-1][temp.l]=temp.data+1;
temp.h=temp.h-1;
temp.data=temp.data+1;
jing(temp);
if ((temp.h==x2)&&(temp.l==y2))
{
return(1);
}
temp.h=temp.h+1;
temp.data=temp.data-1;
}
if ((temp.h+1<n)&&(z[temp.h+1][temp.l]==0))
{
z[temp.h+1][temp.l]=temp.data+1;
temp.h=temp.h+1;
temp.data=temp.data+1;
jing(temp);
if ((temp.h==x2)&&(temp.l==y2))
{
return(1);
}
temp.h=temp.h-1;
temp.data=temp.data-1;
}
if ((temp.l-1>=0)&&(z[temp.h][temp.l-1]==0))
{
z[temp.h][temp.l-1]=temp.data+1;
temp.l=temp.l-1;
temp.data=temp.data+1;
jing(temp);
if ((temp.h==x2)&&(temp.l==y2))
{
return(1);
}
temp.l=temp.l+1;
temp.data=temp.data-1;
}
if ((temp.l+1<m)&&(z[temp.h][temp.l+1]==0))
{
z[temp.h][temp.l+1]=temp.data+1;
temp.l=temp.l+1;
temp.data=temp.data+1;
jing(temp);
if ((temp.h==x2)&&(temp.l==y2))
{
return(1);
}
temp.l=temp.l-1;
temp.data=temp.data-1;
}
}while(s.front!=s.rear);
return(0);
}





void print(int m,int n,int i,int j,int x,int y,Node temp)
{
int p,q,t,r;
t=i;
r=j;
if (fuzhi(t,r,x,y,m,n))
{
printf("Bushu:%4d\n",temp.data);
printf("\n");
for (p=0;p<n;p++)
for(q=0;q<m;q++)
{
printf("%4d",z[p][q]);
if (q==m-1)
printf("\n");
}
}
else
{
printf("Can't find b!\n");
printf("\n");
for (p=0;p<n;p++)
for(q=0;q<m;q++)
{
printf("%4d",z[x][q]);
if (q==m-1)
printf("\n");
}
}
printf("\n");
}






void main()
{
int i,j,m,n,x,y;
Node temp;
printf ("Please enter the num of m,n:\n\n");
cin>>m>>n;
printf("\n");
qingkong(m,n);
qiang(m,n);
printf ("Please enter the num of a(hang,zong):\n\n");
cin>>i>>j;
printf("\n");
printf ("Please enter the num of b(hang,zong):\n\n");
cin>>x>>y;
printf("\n");
i=i-1;
j=j-1;
x=x-1;
y=y-1;
print(m,n,i,j,x,y,temp);
}

其中qiang为不能走的地方,请按正常思维输入(非数组,从1开始,其他均是),m为列数,n为行数。
fuzhi函数有问题,但单步调试后一下子就错了,请大家帮帮忙吧!

...全文
177 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yach_jerry 2007-04-30
  • 打赏
  • 举报
回复
这道题不是迷宫,而且以有打印函数,错误在fuzh函数上.不过谢啦!
飞哥 2007-04-30
  • 打赏
  • 举报
回复
你自己加个打印信息检查一下不就行了嘛
我就不去看了
比如说:
定义个函数
void PrintMaze(int m, int n)
{
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
printf("%d ",z[i][j]);
}
printf("\n");
}
}

你在觉得可能改变的地方调用一次
如果真的有改变,就看是谁可以改变强的值
如果是在 求解路径时改的值的话,那你就再研究一下你的算法吧
还有
Maze问题,用栈来做还是比较容易的
飞哥 2007-04-30
  • 打赏
  • 举报
回复
看来lz英语还不大好
看着函数名字头疼死了
yach_jerry 2007-04-30
  • 打赏
  • 举报
回复
输入,运行后,原来的墙(qiang)不见了,或不知道无规律的变化了位置,付值也不对.
本算法是从a点向外循环付值至b点,以此计算a至b的最短路径.
mochen5460 2007-04-30
  • 打赏
  • 举报
回复
是迷宫吗?说详细一点嘛,问题在哪?

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧