MLE求调

P11228 [CSP-J 2024] 地图探险

Minosyzy @ 2024-10-28 22:15:10

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
long long t;
long long n,m,k;
char a[N][N];
int vis[N][N];
long long dfs(long long u,long long x,long long y,long long d,long long sum){
    if(u==0){
        return sum;
    }
    if(d==0){
        if(x>=1&&x<=n&&y+1>=1&&y+1<=m&&a[x][y+1]!='x'){
            y+=1;
            x+=0;
            if(!vis[x][y]){
                vis[x][y]=1;sum++;
            }
        }else{
            d=(d+1)%4;
        }
    }else if(d==1){
        if(x+1>=1&&x+1<=n&&y>=1&&y<=m&&a[x+1][y]!='x'){
            y+=0;
            x+=1;
            if(!vis[x][y]){
                vis[x][y]=1;sum++;
            }

        }else{
            d=(d+1)%4;
        }
    }else if(d==2){
        if(x>=1&&x<=n&&y-1>=1&&y-1<=m&&a[x][y-1]!='x'){
            y-=1;
            x+=0;
            if(!vis[x][y]){
                vis[x][y]=1;sum++;
            }
        }else{
            d=(d+1)%4;
        }
    }else if(d==3){
        if(x-1>=1&&x-1<=n&&y>=1&&y<=m&&a[x-1][y]!='x'){
            y+=0;
            x-=1;
            if(!vis[x][y]){
                vis[x][y]=1;sum++;
            }
        }else{
            d=(d+1)%4;
        }
    }
    dfs(u-1,x,y,d,sum);
}
int main(){
    //freopen("explore.in","r",stdin);
    //freopen("explore.out","w",stdout);
    scanf("%lld",&t);
    while(t--){
        scanf("%lld%lld%lld",&n,&m,&k);
        long long x,y,d;
        scanf("%lld%lld%lld",&x,&y,&d);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>a[i][j];
                vis[i][j]=0;
            }
        }
        long long num=0;
        vis[x][y]=1;
        num=dfs(k,x,y,d,1);
        printf("%lld\n",num);
    }
    return 0;
}

|