求助

P11228 [CSP-J 2024] 地图探险

zyx_dzpd @ 2024-11-01 11:26:53

不记得代码了,打了一个大致

#include<bits/stdc++.h>
using namespace std;

int n,m,p,i,j,d,k,x,y,l=0;
char a[1001][1001];

void dp(){
    l=0;
    while(1){
        if(d==0){
            if(y+1>=1 and y+1<=m and a[x][y+1]!='x'){
                d=(d+1)%4;
                if(a[x][y]!='y') l++;
                a[x][y]='y';
                y++;
            }
            else d=(d+1)%4;
        }
        if(d==1){
            if(x+1>=1 and x+1<=n and a[x+1][y]!='x'){
                d=(d+1)%4;
                if(a[x][y]!='y') l++;
                a[x][y]='y';
                x++;
            }
            else d=(d+1)%4;
        }
        if(d==2){
            if(y-1>=1 and y-1<=m and a[x][y-1]!='x'){
                d=(d+1)%4;
                if(a[x][y]!='y') l++;
                a[x][y]='y';
                y--;
            }
            else d=(d+1)%4;
        }
        if(d==3){
            if(x-1>=1 and x-1<=n and a[x-1][y]!='x'){
                d=(d+1)%4;
                if(a[x][y]!='y') l++;
                a[x][y]='y';
                x--;
            }
            else d=(d+1)%4;
        }
        k--;
        if(k==0) return ;
//      cout<<"x:"<<x<<"  y:"<<y<<" k:"<<k<<"\n";
    }
}

int main(){
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
    int T;
    cin>>T;
    for(i=1;i<=T;i++){
        cin>>n>>m>>k;
        cin>>x>>y>>d;
        for(j=1;j<=n;j++){
            for(p=1;p<=m;p++){
                cin>>a[j][p];
            }
        }
        dp();
        cout<<l<<"\n";
    }
    return 0;
}

|