50pts

P11228 [CSP-J 2024] 地图探险

_liujunming_ @ 2024-10-31 19:57:02


#include<bits/stdc++.h>
using namespace std;
int t;
const int N=1005;
bool come[N][N];
char s[N][N];
int cx[4]={0,1,0,-1},cy[4]={1,0,-1,0};
int n,m,k,sx,sy,sd;
void dfs(int x,int y,int now,int d,int ld)
{
    if(come[x][y]&&ld==d||now>k)return ;
    else come[x][y]=true;
    if(now>k)return ;
    if(x+cx[d]>0&&x+cx[d]<=n&&cy[d]+y>0&&cy[d]+y<=m&&s[cx[d]+x][cy[d]+y]=='.')
        dfs(cx[d]+x,cy[d]+y,now+1,d,d);
    else
        dfs(x,y,now+1,(d+1)%4,d);
    return ;
}
void run()
{
    int ans=0;
    memset(come,false,sizeof(come));
    scanf("%d%d%d%d%d%d",&n,&m,&k,&sx,&sy,&sd);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>s[i][j];
    dfs(sx,sy,0,sd,-1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            ans+=(come[i][j]);
    printf("%d\n",ans);
}
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=t;i++)run();
    return 0;
# }

by mysb @ 2024-10-31 21:14:14

你用什么dfs啊,直接模拟就行了


by mysb @ 2024-10-31 21:24:09

11行没必要,也不对

它可能一个点走多次,但方向不同,就能重复走,不能return


by mysb @ 2024-10-31 21:25:07

#include<bits/stdc++.h>
using namespace std;
int t;
const int N=1005;
bool come[N][N];
char s[N][N];
int cx[4]={0,1,0,-1},cy[4]={1,0,-1,0};
int n,m,k,sx,sy,sd;
void dfs(int x,int y,int now,int d)
{if(now>k)return ;
    if(now>k)return ;
    come[x][y]=true;

    if(x+cx[d]>0&&x+cx[d]<=n&&cy[d]+y>0&&cy[d]+y<=m&&s[cx[d]+x][cy[d]+y]=='.')
        dfs(cx[d]+x,cy[d]+y,now+1,d);
    else
        dfs(x,y,now+1,(d+1)%4);
    return ;
}
void run()
{
    int ans=0;
    memset(come,false,sizeof(come));
    scanf("%d%d%d%d%d%d",&n,&m,&k,&sx,&sy,&sd);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>s[i][j];
    dfs(sx,sy,0,sd);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            ans+=(come[i][j]);
    printf("%d\n",ans);
}
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=t;i++)run();
    return 0;
}

by mysb @ 2024-10-31 21:26:25

把11行改了(看不懂你的ld,ld没用)


by mysb @ 2024-10-31 21:26:55

@ liujunming


|