#34178: 請幫我看看這題錯在哪,我的第8行輸出有錯


yuyantang921002@gmail.com (123456789)

學校 : 不指定學校
編號 : 195268
來源 : [140.118.135.213]
最後登入時間 :
2023-03-03 11:23:49
c082. 00118 - Mutant Flatworld Expolrers -- UVa118 | From: [140.118.135.213] | 發表日期 : 2023-03-03 09:47

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    int arr[b+1][a+1],x,y,x0,y0,i,j;
    for(i=0;i<b+1;i++)
    {
        for(j=0;j<a+1;j++)
        {
            arr[b+1][a+1]=0;
        }
    }
    char dire;
    int dir;
    string c;
    while(cin>>x)
    {
        cin>>y>>dire;
        switch(dire)
        {
            case 'N':
                dir=0;
                break;
            case 'E':
                dir=1;
                break;
            case 'S':
                dir=2;
                break;
            case 'W':
                dir=3;
                break;
        }
        getchar();
        getline(cin,c);
        int l=c.length();
        for(i=0;i<l&&dir!=5;i++)
        {
            if(c[i]=='R')
            {
                dir++;
                dir=dir%4;
            }
            else if(c[i]=='L')
            {
                dir--;
                if(dir==-1)
                {
                    dir=3;
                }
            }
            else
            {
                x0=x;
                y0=y;
                if(dir==0)
                {
                    y++;
                }
                else if(dir==1)
                {
                    x++;
                }
                else if(dir==2)
                {
                    y--;
                }
                else
                {
                    x--;
                }
                if(x<0||x>a||y<0||y>b)
                {
                    if(arr[y][x]==0)
                    {
                        dir=5;
                        arr[y][x]=1;
                    }
                    x=x0;
                    y=y0;
                }
            }
        }
        switch(dir)
        {
            case 0:
                dire='N';
                break;
            case 1:
                dire='E';
                break;
            case 2:
                dire='S';
                break;
            case 3:
                dire='W';
                break;
        }
        if(dir==5)
        {
           printf("%d %d %c LOST\n",x,y,dire);
        }
        else
        {
            printf("%d %d %c\n",x,y,dire);
        }
    }
}

 

 

 
#34189: Re: 請幫我看看這題錯在哪,我的第8行輸出有錯


dfd8282@gmail.com (fishhh)

學校 : 嘉義市私立嘉華高級中學
編號 : 99760
來源 : [114.40.219.99]
最後登入時間 :
2024-05-14 23:40:30
c082. 00118 - Mutant Flatworld Expolrers -- UVa118 | From: [114.40.254.118] | 發表日期 : 2023-03-04 17:50

第80行應該要改成這樣 if(arr[y0][x0]==0) 這樣才是紀錄掉下去之前的座標
原本 if(arr[y][x]==0)  的意思是看有沒有人從這裡掉下去過的座標 這樣就跟題意不合了 甚至還有可能因為x,y可能小於 0 造成 RE
 
至於會WA line7:
例如 位在左下角掉下去會有兩種可能 (-1,0) , (0,-1) 但這時如果紀錄的是掉下去過的座標的話 那麼機器人有可能會在同樣一個地方掉下去兩次
 
然後最後如果是掉下去了 要記得紀錄他掉下去時的方向 (程式裡沒寫到這個部分
 
 
程式實作的部分,陣列 arr 如果要讓他初始化(裡面全都是0) 就這樣寫就好 arr[b+1][a+1]={} 不用寫兩個for迴圈
 
上面這些改完應該就AC了~
 
ZeroJudge Forum