求助优化 ,TLE*3
查看原帖
求助优化 ,TLE*3
517637
瀛洲仙子楼主2022/11/24 15:25
#include<bits/stdc++.h>
using namespace std;
bool vis[1005][1005];
char mp[1005][1005];int n,m;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
bool inrange(int x,int y)
{
    return 1<=x and x<=n and 1<=y and y<=n;
}
int bfs(int x,int y)
{
    queue<pair<int,int>>q;vis[x][y]=true;
    q.push(pair<int,int>(x,y));int cnt=1;
    while(q.size())
    {
        pair<int,int>point=q.front();q.pop();
        int px=point.first,py=point.second;
        for(int i=0;i<4;++i)
        {
            int fx=px+dx[i];
            int fy=py+dy[i];
            if(inrange(fx,fy) and !vis[fx][fy] and mp[fx][fy]^mp[px][py])
            {
                vis[fx][fy]=true;++cnt;
                q.push(pair<int,int>(fx,fy));
            }
        }
        if(cnt==n*n)break;
    }
    return cnt;
}
int main()
{
    cin>>n>>m;for(int i=1;i<=n;++i)
        for(int j=1;j<=n;++j)
    cin>>mp[i][j];while(m--)
    {
        memset(vis,false,sizeof(vis));
        int x,y;cin>>x>>y;
        int ans=bfs(x,y);
        cout<<ans<<endl;
    }
}
2022/11/24 15:25
加载中...