50分求调

P11228 [CSP-J 2024] 地图探险

oi_afish @ 2024-10-27 21:12:00

#include<iostream>
using namespace std;
int t;
int n,m,k;
int x,y,d;
int ans;
char s[1005][1005];
bool ss[1005][1005];
int main(){
    cin>>t;
    for (int i=1;i<=t;i++){
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        for (int j=1;j<=n;j++){
            for (int jj=1;jj<=m;jj++){
                cin>>s[j][jj];
                ss[j][jj]=0;
            }
        }
        ans=0;
        for (int j=1;j<=k;j++){
            ss[x][y]=1;
            //
            if (d==0){
                if (x>=1&&x<=n&&(y+1)>=1&&(y+1)<=m){
                    if (s[x][y+1]=='.')y+=1;
                    else d=(d+1)%4;
                }
                else d=(d+1)%4;
            }
            //
            if (d==1){
                if ((x+1)>=1&&(x+1)<=n&&y>=1&&y<=m){
                    if (s[x+1][y]=='.')x+=1;
                    else d=(d+1)%4;
                }
                else d=(d+1)%4;
            }
            //
            if (d==2){
                if (x>=1&&x<=n&&(y-1)>=1&&(y-1)<=m){
                    if (s[x][y-1]=='.')y-=1;
                    else d=(d+1)%4;
                }
                else d=(d+1)%4;
            }
            //
            if (d==3){
                if ((x-1)>=1&&(x-1)<=n&&y>=1&&y<=m){
                    if (s[x-1][y]=='.')x-=1;
                    else d=(d+1)%4;
                }
                else d=(d+1)%4;
            }
        }
        for (int j=1;j<=n;j++){
            for (int jj=1;jj<=m;jj++){
                if (ss[j][jj]!=0)ans++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

貌似大概是一些边界问题


by __Function__ @ 2024-10-27 21:17:23

@oi_afish if (d==1) 这些前面都要加else,否则d会一直加


by __Function__ @ 2024-10-27 21:18:18

@oi_afish ans从一开始


|