大佬求助 bfs tle一个点 开o2 wa一个点
  • 板块P1141 01迷宫
  • 楼主acwww
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/3/15 16:16
  • 上次更新2023/10/28 06:32:53
查看原帖
大佬求助 bfs tle一个点 开o2 wa一个点
592775
acwww楼主2022/3/15 16:16
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 1010;
#define endl '\n'
char a[N][N];
int vis[N][N],n,m,sum,flag[N][N],ans[100010];
int cx[4] = { -1,1,0,0 },cy[4]={0,0,-1,1};
struct node
{
	int x, y, sign;
};
queue <node>q;
void bfs(int x, int y)
{
    while(q.size()) q.pop();
	int res = 1;
	vis[x][y] = 1;
	q.push({ x,y,a[x][y] - '0' });
	while (q.size())
	{
		auto v = q.front();
		q.pop();
		for (int i = 0; i < 4; i++)
		{
			int dx = v.x + cx[i];
			int dy = v.y + cy[i];
			if (dx>=1 && dx <= n && dy >= 1 && dy <= n && flag[dx][dy] == 0 && (a[dx][dy]-'0') == (!v.sign))
			{
				flag[dx][dy]=sum;
				q.push({ dx,dy,!v.sign });
				res++;
			}
		}
	}
	ans[sum]=res;
}
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> a[i][j];
		}
	}
		for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			if(flag[i][j]==0)
		{
		    flag[i][j]=++sum;
		    bfs(i,j);
		}
		}
	}
	while (m--)
	{
		int x, y;
		cin >> x >> y;
        cout<<ans[flag[x][y]]<<endl;
	}
}


2022/3/15 16:16
加载中...