|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我这个程序一开始运行成功,但试了几次以后就突然运行时候失败了。我也觉得很奇怪,有人能帮忙看看吗。
#include<stdio.h>
#include<stdlib.h>
struct hill
{ int height;
int pass;
}*path;
int R,L;
int slide(int ,int);
int main()
{ int i,j,length,longest=0;
scanf("%d",&R);
scanf("%d",&L);
path = (struct hill*)malloc(sizeof(R*L*sizeof(struct hill)));
for(i=0;i<R;i++)
for(j=0;j<L;j++)
{ scanf("%d",&((path+i*L+j)->height));
(path+i*L+j)->pass=0;
}
for(i=0;i<R;i++)
for(j=0;j<L;j++)
{ if((path+L*i+j)->pass)
length=(path+L*i+j)->pass;
else
length=slide(i,j);
if(length>longest)
longest=length;
}
free(path);
printf("%d",longest);
return 0;
}
int slide(int x,int y)
{ int i,temp,longest=0;
int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
for(i=0;i<4;i++)
{ if(x+d[i][0]<0 || x+d[i][0]==R || y+d[i][1]<0 || y+d[i][1]==L)
continue;
if( (path+(x+d[i][0])*L+y+d[i][1])->height >= (path+x*L+y)->height)
continue;
if((path+(x+d[i][0])*L+y+d[i][1])->pass)
temp=(path+(x+d[i][0])*L+y+d[i][1])->pass;
else
temp=slide(x+d[i][0],y+d[i][1]);
longest= temp>longest ? temp:longest;
}
(path+x*L+y)->pass=1+longest;
return 1+longest;
}
|
|