2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-F-Color it(暴力)

链接:https://www.nowcoder.com/acm/contest/163/F
来源:牛客网
 

题目描述

There is a matrix A that has N rows and M columns. Each grid (i,j)(0 ≤ i < N, 0 ≤ j < M) is painted in white at first.
Then we perform q operations:
For each operation, we are given (xc, yc) and r. We will paint all grids (i, j) that meets to black.
You need to calculate the number of white grids left in matrix A.

输入描述:

 

The first line of the input is T(1≤ T ≤ 40), which stands for the number of test cases you need to solve.

The first line of each case contains three integers N, M and q (1 ≤ N, M ≤ 2 x 104; 1 ≤ q ≤ 200), as mentioned above.

The next q lines, each lines contains three integers xc, yc and r (0 ≤ xc < N; 0 ≤ yc < M; 0 ≤ r ≤ 105), as mentioned above.

输出描述:

For each test case, output one number.

示例1

输入

复制

2
39 49 2
12 31 6
15 41 26
1 1 1
0 0 1

输出

复制

729
0

题意:给你一个n*m的格点,一开始全部是白色的。有q次操作,每次操作给你一个点的坐标和一个半径,使得以该点为圆心的圆所覆盖的所有点全部染成黑色,如果已经是黑色,则还是黑色,不会再变白了!,问你q次操作后还剩多少个点是白色?

扫描二维码关注公众号,回复: 2847486 查看本文章

题解:我们考虑暴力每一行,对于当前行,暴力q个圆,看每一个圆在这一行所能染的格点数即可。

#include<math.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,m,q;
struct node
{
	int x,y,r;
}a[205];
struct node1
{
	int l,r;
}b[1005];
bool comp(node1 a,node1 b)
{
	if(a.l==b.l)
		return a.r<b.r;
	return a.l<b.l;
}
int main(void)
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int cnt=0,ans=0;
		scanf("%d%d%d",&n,&m,&q);
		for(int i=0;i<q;i++)
			scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].r);
		for(int i=0;i<n;i++)
		{
			int w,h;cnt=0;
			for(int j=0;j<q;j++)
			{
				if(abs(i-a[j].x)>a[j].r)
					continue;
				w=abs(i-a[j].x);
				h=floor(sqrt(a[j].r*a[j].r-w*w));
				b[++cnt].l=max(0,a[j].y-h);
				b[cnt].r=min(m-1,a[j].y+h);
			}
			sort(b+1,b+cnt+1,comp);
			if(cnt) ans+=b[1].r-b[1].l+1;
			for(int j=2;j<=cnt;j++)
			{
				if(b[j-1].r>=b[j].l)
					b[j].l=b[j-1].r+1;
				if(b[j].r>=b[j].l)
					ans+=b[j].r-b[j].l+1;
				b[j].r=max(b[j].r,b[j-1].r);
			}
		}
		printf("%d\n",n*m-ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/haut_ykc/article/details/81436971