40pts求出

P11228 [CSP-J 2024] 地图探险

scratch_szc @ 2024-10-27 09:12:40

#include<bits/stdc++.h>
//#pragma GCC optimize(2)
#define int long long
#define INF LLONG_MAX
#define _INF LLONG_MIN
#define ios ios::sync_with_stdio(0),cout.tie(0),cin.tie(0);
using namespace std;
int fx[4]={0,1,0,-1};
int fy[4]={1,0,-1,0};
int n,m;
const int N=1e3+11;
char mp[N][N];
bool flag[N][N];
int res=1;
void print(int a,int b,int c,int d,int e){cout<<a<<' '<<b<<' '<<c<<' '<<d<<' '<<e<<'\n';}
void dfs(int x,int y,int d,int k){
    flag[x][y]=1;
    if(k==0)return ;
    int dx=x+fx[d];
    int dy=y+fy[d];
    if(mp[dx][dy]=='x'||dx<1||dx>n||dy<1||dy>m||flag[x][y]==0)dfs(x,y,(d+1)%4,k-1);
    else res++,dfs(dx,dy,d,k-1);
}
signed main(){
    ios
    int t;
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>n>>m;
        int k;
        cin>>k;
        int xx,yy,dd;
        cin>>xx>>yy>>dd;
        for(int j=1;j<=n;j++)for(int z=1;z<=m;z++)cin>>mp[j][z];
        dfs(xx,yy,dd,k);
        cout<<res<<'\n';
        res=1;
    }
    return 0;
}

by leozhao123 @ 2024-10-27 09:36:29

@scratch_szc 把DFS下第一句话 flag[x][y]=1 移到最后一个 else if里面


by scratch_szc @ 2024-10-27 09:38:41

@leozhao123 a了thx


by leozhao123 @ 2024-10-27 09:42:48

@scratch_szc 求关


上一页 |