40求调

P11228 [CSP-J 2024] 地图探险

luojingjie @ 2024-10-26 14:44:48

#include<bits/stdc++.h>
using namespace std;
int T,n,m,k,x,y,d;
char a[1005][1005];
bool f[1005][1005];
int main(){
    cin>>T;
    while(T--){
        cin>>n>>m>>k>>x>>y>>d;
        long long ans=1;
        memset(f,false,sizeof f);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
            }
        }
        for(int i=1;i<=k;i++){
            if(d==0){
                if(a[x][y+1]=='.'){
                    if(f[x][y+1]==false){
                        ans++;
                    }
                    y++;
                    f[x][y]=true;
                }
                else{
                    d=(d+1)%4;
                }
            }
            else if(d==1){
                if(a[x+1][y]=='.'){
                    if(f[x+1][y]==false){
                        ans++;
                    }
                    x++;
                    f[x][y]=true;
                }
                else{
                    d=(d+1)%4;
                }
            }
            else if(d==2){
                if(a[x][y-1]=='.'){
                    if(f[x][y-1]==false){
                        ans++;
                    }
                    y--;
                    f[x][y]=true;
                }
                else{
                    d=(d+1)%4;
                }
            }
            else{
                if(a[x-1][y]=='.'){
                    if(f[x-1][y]==false){
                        ans++;
                    }
                    x--;
                    f[x][y]=true;
                }
                else{
                    d=(d+1)%4;
                }
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}

by jinhaoyu142857 @ 2024-10-26 16:17:44

#include<bits/stdc++.h>
using namespace std;
char mp[1005][1005];
bool vis[1005][1005];
int fx[4]={0,1,0,-1},fy[4]={1,0,-1,0};
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--){
        int n,m,k,x,y,d,ans=1;
        cin>>n>>m>>k>>x>>y>>d;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>mp[i][j];
                vis[i][j]=0;
            }
        }
        vis[x][y]=1;
        for(int i=1;i<=k;i++){
            if(x+fx[d]>n||x+fx[d]<=0||y+fy[d]>m||y+fy[d]<=0||mp[x+fx[d]][y+fy[d]]!='.'){
                d=(d+1)%4;
                continue;
            }
            x+=fx[d];
            y+=fy[d];
            if(!vis[x][y]){
                vis[x][y]=1;
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}

|