#31024: C++ 記憶體區段錯誤怎麼辦QQ


weimao_48@go.edu.tw (葳貓ฅ●ω●ฅ)

學校 : 不指定學校
編號 : 162161
來源 : [118.163.199.174]
最後登入時間 :
2023-09-13 13:26:06
i401. 3. 雷射測試 -- 2022年6月APCS | From: [110.26.64.230] | 發表日期 : 2022-07-05 02:12

#include <iostream>
using namespace std;

int main()
{
    int N;
    int i, j;
    int now[2]={0, 0};
    int stop=0;
    int ndec=4;
    int next[2];
    int wstop=0;
    int m=0;
    int nowg;


    cin >> N;
    int glass[N][2];
    int dec[N];
    for (i=0; i<N; i++)
    {
            cin >> glass[i][0] >> glass[i][1] >> dec[i];
    }

    while (stop == 0)
    {
        wstop = 1;
        if (ndec == 1)
        {
            //cout << "1" << " m\n";
            next[1] = 30001;
            for (i=0; i<N; i++)
            {
                if (glass[i][0] == now[0])
                {
                    next[0] = now[0];
                    if (glass[i][1] > now[1])
                    {
                        if (glass[i][1] <= next[1])
                        {
                            next[1] = glass[i][1];
                            nowg = i;
                        }
                        wstop = 0;
                    }
                }
            }
            if (dec[nowg] == 0)
            {
                ndec = 4;
            }
            else
            {
                ndec = 3;
            }
            if (wstop == 1)
            {
                stop = 1;
            }
            else
            {
                now[1] = next[1];
                m+=1;
            }
        }
        else if (ndec == 2)
        {
            //cout << "2" << " m\n";
            next[1] = -30001;
            for (i=0; i<N; i++)
            {
                if (glass[i][0] == now[0])
                {
                    //cout << i << " 1\n";
                    next[0] = now[0];
                    if (glass[i][1] < now[1])
                    {
                        //cout << i << " 2\n";
                        if (glass[i][1] >= next[1])

                        //cout << i << " 3\n";
                            next[1] = glass[i][1];
                            nowg = i;
                        wstop = 0;
                    }
                }
            }
            if (dec[nowg] == 0)
            {
                ndec = 3;
            }
            else
            {
                ndec = 4;
            }
            if (wstop == 1)
            {
                stop = 1;
            }
            else
            {
                now[1] = next[1];
                m+=1;
            }
        }
        else if (ndec == 3)
        {
            //cout << "3" << " m\n";
            next[0] = 0;
            for (i=0; i<N; i++)
            {
                if (glass[i][1]== now[1])
                {
                    next[1] = now[1];
                    if (glass[i][0] < now[0])
                    {
                        if (glass[i][0] >= next[0])
                        {
                            next[0] = glass[i][0];
                            nowg = i;
                        }
                        wstop = 0;
                    }
                }
            }

            if (dec[nowg] == 0)
            {
                ndec = 2;
            }
            else
            {
                ndec = 1;
            }
            if (wstop == 1)
            {
                stop = 1;
            }
            else
            {
                now[0] = next[0];
                m+=1;
            }
        }
        else
        {
            //cout << "4" << " m\n";
            next[0] = 30001;
            for (i=0; i<N; i++)
            {
                if (glass[i][1]== now[1])
                {
                    next[1] = now[1];
                    if (glass[i][0] > now[0])
                    {
                        if (glass[i][0] <= next[0])
                        {
                            next[0] = glass[i][0];
                            nowg = i;
                        }
                        wstop = 0;
                    }
                }
            }
            if (dec[nowg] == 0)
            {
                ndec = 1;
            }
            else
            {
                ndec = 2;
            }
            if (wstop == 1)
            {
                stop = 1;
            }
            else
            {
                now[0] = next[0];
                m+=1;
            }
        }
    }

    cout << m;
    return 0;
}

 

 
#31083: Re: C++ 記憶體區段錯誤怎麼辦QQ


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
i401. 3. 雷射測試 -- 2022年6月APCS | From: [39.9.78.186] | 發表日期 : 2022-07-10 10:49


            if (dec[nowg] == 0)

 


nowg沒有初始化

 
ZeroJudge Forum