为什么是20分?

P1141 01迷宫

Gan2024 @ 2024-09-16 08:34:30

只AC了两个点,WA了4个点,TLE了5个点。

代码如下(求调):

#include <bits/stdc++.h>
using namespace std;

int n,m,sum,x3,y3;
int a[5010][5010],ans[5010][5010],vis[5010][5010];
char aaaa[5010][5010];
int x1[]={0,-1,0,0,1};
int y21[]={0,0,-1,1,0};
struct d{
    int xx,yy;
}aa[1000001];

void f(int x,int y){
    for(int i=1;i<=n;i++){
        int x2=x-x1[i];
        int y2=y-y21[i];
        if(a[x][y]+a[x2][y2]==1&&!vis[x2][y2]&&x2<=n&&y2<=n&&x2>=1&&y2>=1){
            vis[x2][y2]=1;
            sum++;
            aa[sum].xx=x2;
            aa[sum].yy=y2;
//          cout<<x2<<" "<<y2<<endl;
            f(x2,y2);
        } 
    }
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>aaaa[i][j];
            a[i][j]=aaaa[i][j]-'0';
        }
    }
    for(int i=1;i<=m;i++){
        cin>>x3>>y3;
        if(ans[x3][y3]){
            cout<<ans[x3][y3]<<endl;
        }
        else{
            sum=1;
            vis[x3][y3]=1;
            f(x3,y3);
            for(int i=1;i<=sum;i++){
                ans[aa[i].xx][aa[i].yy]=sum;
                vis[aa[i].xx][aa[i].yy]=0;
            }
            ans[x3][y3]=sum;
            cout<<sum<<endl;
        }
    }
    return 0;
}

by Yxy7952 @ 2024-09-16 08:41:02

@Gan2024

你猜猜你为什么会 TLE


by Mzh2012 @ 2024-09-16 09:36:28

@Gan2024 暴力坑定 TLE 啊。


|