#37508: 我的程式為何第二個測試案例會有Segmentation fault (core dumped)


alexfan (unknown)

學校 : 國立武陵高級中學
編號 : 245214
來源 : [180.217.10.216]
最後登入時間 :
2023-12-30 14:41:10
i401. 3. 雷射測試 -- 2022年6月APCS | From: [112.78.72.198] | 發表日期 : 2023-09-14 21:19

記憶體區段錯誤!
Segmentation fault (core dumped)

 

#include <iostream>

using namespace std;

int a[250000][3];
int b;
int sum = 0;
int f(int x, int y, int z);

int main()
{
    cin >> b;
    for(int i=0 ; i<b ; i++){
        for(int j=0 ; j<=2 ; j++){
            cin >> a[i][j];
        }
    }

    f(0,0,1);
    cout << sum << endl;
    return 0;
}

int f(int x, int y, int z){
    if(z == 1){
        long n=100000;
        long x1, m;
        for(int i=0 ; i<b ; i++){
            if(a[i][1] == y && a[i][0] - x > 0 && a[i][0] - x < n){
                n = a[i][0] - x;
                x1 = a[i][0];
                m = a[i][2];
            }
        }
        if(n == 100000)
            return 0;
        if(m == 0)
            z = 4;
        else
            z = 2;
        sum++;
        f(x1, y, z);
    }
    if(z == 2){
        long n=-100000;
        long y1, m;
        for(int i=0 ; i<b ; i++){
            if(a[i][0] == x && a[i][1] - y < 0 && a[i][1] - y > n){
                n = a[i][1] - y;
                y1 = a[i][1];
                m = a[i][2];
            }
        }
        if(n == -100000)
            return 0;
        if(m == 0)
            z = 3;
        else
            z = 1;
        sum++;
        f(x, y1, z);
    }
    if(z == 3){
        long n=-100000;
        long x1, m;
        for(int i=0 ; i<b ; i++){
            if(a[i][1] == y && a[i][0] - x < 0 && a[i][0] - x > n){
                n = a[i][0] - x;
                x1 = a[i][0];
                m = a[i][2];
            }
        }
        if(n == -100000)
            return 0;
        if(m == 0)
            z = 2;
        else
            z = 4;
        sum++;
        f(x1, y, z);
    }
    if(z == 4){
        long n=100000;
        long y1, m;
        for(int i=0 ; i<b ; i++){
            if(a[i][0] == x && a[i][1] - y > 0 && a[i][1] - y < n){
                n = a[i][1] - y;
                y1 = a[i][1];
                m = a[i][2];
            }
        }
        if(n == 100000)
            return 0;
        if(m == 0)
            z = 1;
        else
            z = 3;
        sum++;
        f(x, y1, z);
    }
}

 
#37509: Re: 我的程式為何第二個測試案例會有Segmentation fault (core dumped)


alexfan (unknown)

學校 : 國立武陵高級中學
編號 : 245214
來源 : [180.217.10.216]
最後登入時間 :
2023-12-30 14:41:10
i401. 3. 雷射測試 -- 2022年6月APCS | From: [112.78.72.198] | 發表日期 : 2023-09-14 21:21

我用code::block是可以正常執行得到結果,但是上傳zerojudge卻會有

 
記憶體區段錯誤!
Segmentation fault (core dumped)
 
謝謝。
 
ZeroJudge Forum