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:21:28

@scratch_szc 走过的点重复算了


by scratch_szc @ 2024-10-27 09:22:15

@leozhao123 怎么改


by leozhao123 @ 2024-10-27 09:23:09

@scratch_szc flag 没初始化


by leozhao123 @ 2024-10-27 09:24:39

@scratch_szc 设一个 vis[][] 数组记录走过的点,++res 时判断!vis[x][y]


by leozhao123 @ 2024-10-27 09:25:47

@scratch_szc 建议直接在主函数里循环模拟,dfs 容易爆


by scratch_szc @ 2024-10-27 09:27:15

https://www.luogu.com.cn/record/185130831 @leozhao123 40分


by leozhao123 @ 2024-10-27 09:27:41

@scratch_szc ?所以你这 flag 有何意义?


by scratch_szc @ 2024-10-27 09:28:35

@leozhao123 所以怎么改(代码)


by leozhao123 @ 2024-10-27 09:30:55

@scratch_szc 呃 电脑不在身边


by scratch_szc @ 2024-10-27 09:31:34

@leozhao123 改了,50分


| 下一页