考场代码求条 洛谷60分

P11228 [CSP-J 2024] 地图探险

欧阳逸owen @ 2024-10-26 21:31:02

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll N=1e3+10;
char c[N][N];
ll ans,f[N][N]; 
bool vis[N][N];
ll n,m,k,num;
void dfs(ll x,ll y,ll d,ll temp){
//  cout << x << " " << y << " " << d << " " << temp << " " << num << endl;
    if(vis[x][y]){
        num=f[x][y]+1;
        return ;
    } 
    if(!vis[x][y]) vis[x][y]=1;
    if(temp<=0){
        return ;
    } 
    f[x][y]=num++;
    if(d==0){
        if(y+1>m||c[x][y+1]=='x'){
            vis[x][y]=0;
            dfs(x,y,1,temp-1);
        }
        else dfs(x,y+1,d,temp-1);
    }
    else if(d==1){
        if(x+1>n||c[x+1][y]=='x'){
            vis[x][y]=0;
            dfs(x,y,2,temp-1);
        }
        else{
            dfs(x+1,y,d,temp-1);
        }
    }
    else if(d==2){
        if(y-1<1||c[x][y-1]=='x'){
            vis[x][y]=0;
            dfs(x,y,3,temp-1);
        }
        else dfs(x,y-1,d,temp-1);
    }
    else{
        if(x-1<1||c[x-1][y]=='x'){
            vis[x][y]=0;
            dfs(x,y,0,temp-1);
        }   
        else dfs(x-1,y,d,temp-1);
    }
    return ;
}
ll x,y,d;
void solve(){
    ans=0;
    num=0;
    cin >> n >> m >> k;
    cin >> x >> y >> d;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin >> c[i][j];
            f[i][j]=0;
            vis[i][j]=0;
        }
    }
    dfs(x,y,d,k);
    vis[x][y]=0;
    if(d==3||d==0){
        num+=1;
    } 
//  cout << num << endl;
    dfs(x,y,(d+2)%4,k-2*num);
    num=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[i][j]) ans++;
        } 
    }
    cout << ans << endl;
    return ;
}
ll T;
int main(){
//  freopen("explore2.in","r",stdin);
//  freopen("explore.out","w",stdout);
    cin >> T;
    while(T--) solve(); 
    return 0;
}

by 欧阳逸owen @ 2024-10-26 21:33:50

考场上打出了正解 结果因为栈溢出跑不出来大样例(Windows老问题 我忘记了。。。。) 就想了半天咋优化 写出了这个狗东西


by jxn123 @ 2024-10-26 21:40:30

@欧阳逸owen 你算是废了


by 欧阳逸owen @ 2024-10-26 21:42:26

@jxn123 ....别逮捕我


by starish @ 2024-10-30 20:00:04

@jxn123 @欧阳逸owen 请尝试用while循环进行模拟


|