HDU - 1264 Counting Squares【模拟\线段树】

该博客介绍了一个编程问题,要求计算一系列矩形覆盖的单位正方形数量。输入包含多个矩形的顶点坐标,输出是每个矩形覆盖的正方形数。博主分享了使用暴力模拟方法解决此问题的思路,由于数据规模较小,因此可以有效避免复杂的数据结构。在实现过程中,需要注意每次统计后要清零bool数组以避免重复计数。
摘要由CSDN通过智能技术生成

Counting Squares

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2640 Accepted Submission(s): 1285

Problem Description
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
5 8 7 10
specifies the rectangle who’s corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.

Input
The input format is a series of lines, each containing 4 integers. Four -1’s are used to separate problems, and four -2’s are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.

Output
Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.

Sample Input
5 8 7 10
6 9 7 8
6 8 8 11
-1 -1 -1 -1
0 0 100 100
50 75 12 90
39 42 57 73
-2 -2 -2 -2

Sample Output
8
10000

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1264

简述:给出多个矩形四个顶点的坐标,求多个矩形的面积之和,覆盖的面积只算一次

分析:因为数据量小,所以暴力模拟的方法可行,即统计覆盖过的点的坐标,用bool数组记录。

说明:一开始忘了每统计一次都要用memset()函数清零(头文件<string.h>),接着WA。

AC代码如下:

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
bool ma[110][110];
int ans, i, j;
int answer()
{
	ans = 0;
	for (i = 0; i <= 100; i++)
		for (j = 0; j <= 100; j++)
			if (ma[i][j]) ans++;
	return ans;
}
int main()
{
	int a, b, c, d;
	memset(ma, false, sizeof(ma));
	while (cin >> a >> b >> c >> d)
	{
		if (a < 0)
		{
			cout << answer() << endl;
			memset(ma, false, sizeof(ma));
		}
		if (a == -2 && b == -2 && c == -2 && d == -2)
			break;
		else
		{
			if (a > c)swap(a, c);
			if (b > d)swap(b, d);
			for (i = a; i < c; i++)
				for (j = b; j < d; j++)
					ma[i][j] = true;
					
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值