帮帮忙吧……大难题!
请看以下一段程序:
#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函数有问题,但单步调试后一下子就错了,请大家帮帮忙吧!
[解决办法]
你自己加个打印信息检查一下不就行了嘛
我就不去看了
比如说:
定义个函数
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问题,用栈来做还是比较容易的