#3#10#11#1 TLE,大佬帮忙看看

P1141 01迷宫

Be_myself @ 2024-03-14 11:29:56

#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
int a[1001][1001];
int b[100001][2];
int vis[1001][1001];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
typedef pair<int,int> PII;
queue<PII> q;
void BFS(int x,int y){
    cnt=1;
    vis[x][y]=1;
    q.push({x,y});
    while(!q.empty()){
        auto cur=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int x1=cur.first+dx[i];
            int y1=cur.second+dy[i];
            if(x1>n||x1<1||y1<1||y1>n)continue;
            else if(vis[x1][y1]!=-1)continue;
            else if(a[x1][y1]==a[cur.first][cur.second])continue;
            q.push({x1,y1});
            vis[x1][y1]=vis[cur.first][cur.second]+1;
            cnt++;
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            char x;
            cin>>x;
            a[i][j]=x-'0';
        }
    }
    for(int i=1;i<=m;i++){
        cin>>b[i][0]>>b[i][1];
        memset(vis,-1,sizeof(vis));
        BFS(b[i][0],b[i][1]);
        cout<<cnt<<endl;
    }
    return 0;
} 

by coding2024 @ 2024-03-15 19:40:13

同问


by xxc123 @ 2024-03-15 21:00:42

@Be_myself ```

include<iostream>

include<cstring>

include<cstdio>

using namespace std; int fx[5]={-1,0,0,1},fy[5]={0,-1,1,0},ans[100005]; bool mp[1050][1050]; int visited[1050][1050]; int s,n,m; void f(int x,int y,int sign,int v) { //cout<<x<<' '<<y<<' '<<sign<<endl; if(x<0||x>=n||y<0||y>=n) return; if(mp[x][y]==sign||visited[x][y]) return; s++; visited[x][y]=v; for(int i=0;i<4;i++) { f(x+fx[i],y+fy[i],mp[x][y],v); } return; } int main() { cin>>n>>m; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { char t; cin>>t; // cout<<"aa "<<t<<endl; mp[i][j]=t-'0'; } } for(int i=0;i<m;i++) { int x,y; cin>>x>>y; if(visited[x-1][y-1]) { cout<<ans[visited[x-1][y-1]]<<endl; }else { s=0; f(x-1,y-1,!mp[x-1][y-1],i+1); ans[i+1]=s; cout<<s<<endl; }
} return 0; }


by xxc123 @ 2024-03-15 21:01:23

#include<iostream>
#include<cstring>
#include<cstdio> 
using namespace std;
int fx[5]={-1,0,0,1},fy[5]={0,-1,1,0},ans[100005];
bool mp[1050][1050];
int visited[1050][1050];
int s,n,m;
void f(int x,int y,int sign,int v)
{
    //cout<<x<<' '<<y<<' '<<sign<<endl;
    if(x<0||x>=n||y<0||y>=n) return;
    if(mp[x][y]==sign||visited[x][y]) return;
    s++;
    visited[x][y]=v;
    for(int i=0;i<4;i++)
    {
        f(x+fx[i],y+fy[i],mp[x][y],v);
    }
    return;
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            char t;
            cin>>t;
        //  cout<<"aa "<<t<<endl;
            mp[i][j]=t-'0';
        }
    }
    for(int i=0;i<m;i++)
    {
        int x,y;
        cin>>x>>y;
        if(visited[x-1][y-1])
        {
            cout<<ans[visited[x-1][y-1]]<<endl;
        }else
        {
            s=0;
            f(x-1,y-1,!mp[x-1][y-1],i+1);
            ans[i+1]=s;
            cout<<s<<endl;
        }       
    }
    return 0;
}

@Be_myself


|