CSP-J T2 AK I

P11228 [CSP-J 2024] 地图探险

qiang_xqh @ 2024-10-28 13:29:14

all TLE

#include<bits/stdc++.h>
using namespace std;
char c[1005][1005];
int v[1005][1005];
int n,m,k,ans;
int dfs(int x,int y,int d,int s){
    if(s>=k)
        return ans+1;
    int nx=0,ny=0;
    if(d==0)
    {
        nx=x;
        ny=y+1;
    } 
    if(d==1)
    {
        nx=x+1;
        ny=y;
    }
    if(d==2)
    {
        nx=x;
        ny=y-1;
    }
    if(d==3)
    {
        nx=x-1;
        ny=y;
    }
    if(nx>=1 && nx<=n && ny>=1 && ny<=m && c[nx][ny]=='.')
    {
        if(v[nx][ny]==0)
            ans++;
        v[nx][ny]=1;
        dfs(nx,ny,d,s+1);
    }
    else
        dfs(x,y,(d+1)%4,s+1);
}
int main(){
    int t;
    cin>>t;
    while(t--)
    {
        ans=0;  
        int x,y,d;
        cin>>n>>m>>k>>x>>y>>d;
        for(int i=1; i<=n; i++)
        {
            string s;
            cin>>s;
            for(int j=1; j<=m; j++)
                c[i][j]=s[j-1];
        }
        cout<<dfs(x,y,d,0)<<'\n';
        memset(c,0,sizeof(c));
        memset(v,0,sizeof(v));
    }
    return 0;
}

QAQ


by Seniorsocialiewithme @ 2024-10-28 13:34:03

啊啊啊啊,ME too


by Seniorsocialiewithme @ 2024-10-28 13:34:34

贴个代码,痛苦


#include<bits/stdc++.h>
using namespace std;
long long T,n,m,k,x0,sb,d,res,da[100001],he=0;
char s[2001][2001];
bool vis[2001][2001];
void dfs(int x,int y,int d){
    if(k==0)return;
    k--;
    if(d==0){
        if(s[x][y+1]=='.'){
            vis[x][y+1]=1;
            dfs(x,y+1,d);
        }else{
            d=(d+1)%4;
            dfs(x,y,d);
        }
    } 
    else if(d==1){
        if(s[x+1][y]=='.'){
            vis[x+1][y]=1;
            dfs(x+1,y,d);
        }else{
            d=(d+1)%4;
            dfs(x,y,d);
        }
    }
    else if(d==2){
        if(s[x][y-1]=='.'){
            vis[x][y-1]=1;
            dfs(x,y-1,d);
        }else{
            d=(d+1)%4;
            dfs(x,y,d);
        }
    }
    else{
        if(s[x-1][y]=='.'){
            vis[x-1][y]=1;
            dfs(x-1,y,d);
        }else{
            d=(d+1)%4;
            dfs(x,y,d);
        }
    }
}
int main(){
    cin>>T;
    while(T--){
        cin>>n>>m>>k;
        cin>>x0>>sb>>d;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                cin>>s[i][j];   
            }
        }
        vis[x0][sb]=1;
        dfs(x0,sb,d);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                if(vis[i][j]==1){
                    res++;  
                }
                vis[i][j]=0;
            }
        }   
        da[he]=res;
        he++;
        res=0;
    }
    for(int i=0;i<he;i++)cout<<da[i]<<endl;
}

|