0
#include<stdio.h>
int l;
int check(int m[][l],int a,int r,int c,int e)
{
    int t,i,j;
    for(i=0;i<c;i++)
    {
        if(m[a][i]==e)
            return 1;
    }
    for(i=0;i<c;i++)
    {
        for(j=0;j<r;j++)
        {
            if(a!=j)
            {
                if(m[a][i]==m[j][i]&&m[a][i]!=-1)
                {
                    m[a][i]=-1;
                    return check(m,j,r,c,e);
                }
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    int i,j;
    scanf("%d",&t);
    for(t;t>0;t--)
    {
        l=0;
        int n,e,a,b,x,y;
        scanf("%d%d%d%d",&n,&e,&a,&b);
        int m[e][n];
        l=n;
        for(i=0;i<e;i++)
        {
            for(j=0;j<n;j++)
            {
                m[i][j]=-1;
            }
        }
        for(i=0;i<e;i++)
        {
            scanf("%d%d",&x,&y);
            for(j=0;j<((n-y)/x)+1;j++)
            {
                m[i][y+(j*x)]=y+(j*x);
            }
        }
        int v,g=0;
        for(i=0;i<e;i++)
        {
            for(j=0;j<n;j++)
            {
                if(m[i][j]==a)
                {
                    v=check(m,i,e,n,b);
                    g++;
                    break;
                }
            }
        }
        if(v==1)
        {
            printf("It is possible to move the furniture.\n");
        }
        else if(v==0||g==0)
            printf("The furniture cannot be moved.\n");
    }
    return 0;
}

“我得到了 n<=50000 值的正确答案,当我给出更多值时,我得到了运行时错误”“我已经为“http://www.spoj.pl/problems/SCRAPER/”编写了一个代码“..当我在ideone中运行它时,我得到的答案是“运行时错误”以获得更多'n'值

4

3 回答 3

1

您正在尝试分配比堆栈上可用的内存更多的内存,这会导致堆栈溢出malloc通常使用,calloc或动态分配像这样的巨大数组要好得多realloc

检查这个问题:C: Array initialization segfaults based on size and call to printf()

于 2012-06-28T07:46:04.000 回答
0

您的数组可能存储在堆栈上,并且堆栈通常比堆小。尝试动态分配您的数组(malloc, calloc, free)。

于 2012-06-28T07:53:45.903 回答
0

当你写“int m[e][n]”时,数组的实际空间是1!数组的空间是在编译时定义的,当'e''n'都为0时,你必须使用作为数组的长度,例如m[1000][1000]。并且您需要确保 e<1000。

于 2012-06-28T08:00:13.300 回答