灌水 flooding.cpp

版权声明:写得不好,随便转载,但请注明出处,感激不尽 https://blog.csdn.net/xyc1719/article/details/83540819

【一句话题意】给一个有弧形的图,问(x,y)所在的区域的面积大小。

【分析】由于x和y都是整型,极大地限制了图的可能性,所以直接按点广搜并且稍微分类讨论一下就可以了。所有询问都可以放在离线上做。

【code】

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi 3.14159265
using namespace std;
const int maxn=110;
int n,m,q;
int g[maxn][maxn];
char s[maxn];
double block[maxn*maxn<<2];
int clr[maxn<<1][maxn<<1],tot;
int used[maxn<<1][maxn<<1];
struct node{
	int x,y;
};
void bfs(int px,int py){
	clr[px][py]=++tot;
	block[tot]=0;
	queue<node>q;
	q.push((node){px,py});
	while(!q.empty()){
		node u=q.front();q.pop();
		int x=u.x>>1,y=u.y>>1;
		if(x>0&&y>0&&!used[u.x][u.y]){
			if(g[x][y]) block[tot]+=4-pi/2,clr[u.x-1][u.y-1]=tot,clr[u.x-2][u.y-2]=tot,used[u.x][u.y]=1,used[u.x-1][u.y-1]=1,q.push((node){u.x-2,u.y-2});
			else block[tot]+=pi/4,used[u.x][u.y]=1;
		}
		if(x>0&&y<m&&!used[u.x][u.y+1]){
			if(g[x][y+1]) block[tot]+=pi/4,used[u.x][u.y+1]=1;
			else block[tot]+=4-pi/2,clr[u.x-1][u.y+1]=tot,clr[u.x-2][u.y+2]=tot,used[u.x][u.y+1]=1,used[u.x-1][u.y+2]=1,q.push((node){u.x-2,u.y+2});
		}
		if(x<n&&y>0&&!used[u.x+1][u.y]){
			if(g[x+1][y]) block[tot]+=pi/4,used[u.x+1][u.y]=1;
			else block[tot]+=4-pi/2,clr[u.x+1][u.y-1]=tot,clr[u.x+2][u.y-2]=tot,used[u.x+1][u.y]=1,used[u.x+2][u.y-1]=1,q.push((node){u.x+2,u.y-2});
		}
		if(x<n&&y<m&&!used[u.x+1][u.y+1]){
			if(g[x+1][y+1]) block[tot]+=4-pi/2,clr[u.x+1][u.y+1]=tot,clr[u.x+2][u.y+2]=tot,used[u.x+1][u.y+1]=1,used[u.x+2][u.y+2]=1,q.push((node){u.x+2,u.y+2});
			else block[tot]+=pi/4,used[u.x+1][u.y+1]=1;
		}
	}
}
int main(){
	freopen("flooding.in","r",stdin);
	freopen("flooding.out","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		scanf("%s",s+1);
		for(int j=1;j<=m;j++)
			g[i][j]=s[j]-'0';
	}
	for(int i=0;i<=2*n;i+=2)
		for(int j=0;j<=2*m;j+=2)
			if(!clr[i][j])
				bfs(i,j);
	cin>>q;
	while(q--){
		int x,y;
//		scanf("%d%d",&x,&y);
		cin>>x>>y;//学校oj对scanf极不友好,所以不是快读就是cin
		if((x&1)^(y&1)) printf("%.4lf\n",0);
		else printf("%.4lf\n",block[clr[x][y]]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xyc1719/article/details/83540819
今日推荐