递归全RE给我整慌了

P11228 [CSP-J 2024] 地图探险

SY_TLE_Yearly_Judge @ 2024-11-01 23:05:30

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,m,x,y,k,d;
char mp[1005][1005];
ll vis[1005][1005];
ll dx[5] = {0,1,0,-1};
ll dy[5] = {1,0,-1,0};
ll dg(ll x,ll y,ll d,ll st,ll sq){
    ll nx = x + dx[d];
    ll ny = y + dy[d];
    if(st >= k){
        return sq;
    }
    if(mp[nx][ny] == '.'){
        if(vis[nx][ny] == 0){
            vis[nx][ny] = 1;
            dg(nx,ny,d,st+1,sq+1);
        }else{
            dg(nx,ny,d,st+1,sq);
        }
    }else{
        dg(x,y,(d+1)%4,st+1,sq);
    }
}
int main(){
    freopen("explore5.in","r",stdin);
    cin>>T;
    while(T--){
        memset(mp,0,sizeof(mp));
        memset(vis,0,sizeof(vis));
        cin>>n>>m>>k>>x>>y>>d;
        for(ll i = 1;i <= n;i++){
            for(ll j = 1;j <= m;j++){
                cin>>mp[i][j];
            }
        }
        cout<<dg(x,y,d,0,1)<<endl;
    }
    return 0; 
}

不知道是落谷数据太大还是我return写的有问题,用递归全报RE了,自测也就第五个样例没过


by UNDERTALE_RS @ 2024-11-01 23:12:36

15行没判断x,y越界的情况


by wangjiaqiysxx @ 2024-11-01 23:34:52

为什么要用递归?


by yingzhang1108 @ 2024-11-02 10:30:47

@wangjiaqiysxx 二维数组也可以(doge


|