全MLE求调

P11228 [CSP-J 2024] 地图探险

fly_code @ 2024-11-12 17:51:07

#include<bits/stdc++.h>
using namespace std;
int T,n,m,k,xz,yz,dz,ans=0;
char xy[1005][1005];
bool inf[1005][1005];
int exp(int lx,int ly,int ld,int k){
    if(k==0)return 0;
    int nx,ny,nd;
    if(ld==0){
        nx=lx;
        ny=ly+1;
    }
    if(ld==1){
        nx=lx+1;
        ny=ly;
    }
    if(ld==2){
        nx=lx;
        ny=ly-1;
    }
    if(ld==3){
        nx=lx-1;
        ny=ly;
    }
    if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&xy[nx][ny]=='.')
    {
        if(inf[nx][ny]==false)ans++;
        inf[nx][ny]=true;

        exp(nx,ny,ld,k-1);

    }
    else
    {
        nd=(ld+1)%4;

        exp(lx,ly,nd,k-1);
    }
}
int main()
{

    cin>>T;
    for(int i=1;i<=T;i++)
    {
        cin>>n>>m>>k;
        cin>>xz>>yz>>dz;
        for(int j=1;j<=n;j++)
        {

            for(int l=1;l<=m;l++)
            {
                cin>>xy[j][l];
            }
        }
        exp(xz,yz,dz,k);
        cout<<ans+1<<endl;
        for(int j=1;j<=n;j++)
        {

            for(int l=1;l<=m;l++)
            {
                xy[j][l]=0;
                inf[j][l]=0;
            }
        }
        ans=0;
    }
    return 0;
}
谢谢!

by K_yuxiang_rose @ 2024-11-12 18:28:21

@fly_code 别用递归,爆栈了


by joe_001 @ 2024-11-16 10:09:53

拿上这个

#include<bits/stdc++.h>
using namespace std;
long long k,t,n,m,x,y,d,fx[4][2]={{0,1},{1,0},{0,-1},{-1,0}},s;
char a[1001][1001];
int f(int n,int m)
{
    a[x][y]='s';
    s=0;
    for(int i=1;i<=k;i++)
    {   
        int xx=x+fx[d][0];
        int yy=y+fx[d][1]; 
        if((yy<1 || yy>m) || (xx<1 || xx>n) || a[xx][yy]=='x')
            if(d==3)
                d=0;
            else
                d++;
        else
        {
            x+=fx[d][0];
            y+=fx[d][1];
            a[x][y]='s';
        }   
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(a[i][j]=='s')
                s++;
    return s;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>m>>k>>x>>y>>d;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                cin>>a[i][j];
        cout<<f(n,m)<<endl;
    }
    return 0;
}

求关


by fly_code @ 2024-11-19 17:42:25

@K_yuxiang_rose 谢谢


|