鱼C论坛

 找回密码
 立即注册
查看: 2605|回复: 3

一个C语言程序运行时的问题

[复制链接]
发表于 2016-8-7 21:00:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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;
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2016-8-8 01:03:59 | 显示全部楼层
本帖最后由 人造人 于 2016-8-8 01:05 编辑
#include<stdio.h>
#include<stdlib.h>

struct hill
{
        int height;
        int pass;
}*path;

int R, L;

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;
}

int main()
{
        int i, j, length, longest = 0;
/*
        scanf("%d", &R);
        scanf("%d", &L);*/

        struct test
        {
                int i;
                int j;
        };
        i = sizeof(struct test);
        i = sizeof(2 * 3 * sizeof(struct hill));
        path = 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;
}
只是找了一个问题,其它还没看
51 行 path = malloc(sizeof(R * L * sizeof(struct hill)));
你认为 sizeof(2 * 3 * sizeof(struct hill)) 应该是多少
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-8 08:33:41 | 显示全部楼层
人造人 发表于 2016-8-8 01:03
只是找了一个问题,其它还没看
51 行 path = malloc(sizeof(R * L * sizeof(struct hill)));
你认为 si ...

天哪。。。。太感谢了   一直没检查出来。。。   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-8 08:52:27 | 显示全部楼层
人造人 发表于 2016-8-8 01:03
只是找了一个问题,其它还没看
51 行 path = malloc(sizeof(R * L * sizeof(struct hill)));
你认为 si ...

天哪。。。。太感谢了   一直没检查出来。。。   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-7-5 00:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表