玄关,bfs并查集WA求调
  • 板块P1141 01迷宫
  • 楼主Const_AKer
  • 当前回复19
  • 已保存回复20
  • 发布时间2024/9/23 21:21
  • 上次更新2024/9/24 00:04:37
查看原帖
玄关,bfs并查集WA求调
1038686
Const_AKer楼主2024/9/23 21:21

rt
bfs并查集计数除了hack全部WA

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,m,i,j,k,nowx,nowy;
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
bool a[N][N],vis[N][N];
char s;
struct point
{
	int x,y;
	friend bool operator ==(point p,point q)
	{
		return (p.x==q.x&&p.y==q.y);
	}
	friend bool operator <(point p,point q)
	{
		return p.x<q.x;
	}
}now,tmp,vising;
map<point,point> fa;
map<point,int> cnt;
queue<point> q;
point find(point p)
{
	if(p==fa[p]) return p;
	return fa[p]=find(fa[p]);
}
int main()
{
	cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			cin>>s;
			a[i][j]=(s-'0');
			tmp.x=i,tmp.y=j;
			fa[tmp]=tmp;
		}
	}
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		{
			if(!vis[i][j])
			{
				vising.x=i,vising.y=j;
				q.push(vising);
				while(!q.empty())
				{
					now=q.front();q.pop();
					nowx=now.x,nowy=now.y;
					fa[now]=vising;
					if(!vis[nowx][nowy])
					{
						vis[nowx][nowy]=1;
						for(k=0;k<4;k++)
						{
							tmp.x=nowx+fx[k],tmp.y=nowy+fy[k];
							if(tmp.x>0&&tmp.x<=n&&tmp.y>0&&tmp.y<=n) q.push(tmp);
						}
					}
				}
			}
		}
	}
	for(i=1;i<=n;i++) 
	{
		for(j=1;j<=n;j++)
		{
			tmp.x=i,tmp.y=j;
			tmp=find(tmp);
			cnt[tmp]++;
		}
	}
	while(m--)
	{
		cin>>i>>j;
		tmp.x=i,tmp.y=j;
		tmp=find(tmp);
		cout<<cnt[tmp]<<'\n';
	}
	return 0;
}
2024/9/23 21:21
加载中...