第十二次-E题-hdu-1264

题目:hdu-1264
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

题意:给你4个数字分别代表长方形对角线的两个点
问你这些长方形一共覆盖了多少面积用1*1的正方形个数来表示数目
找到点跟正方形的关系
我的思路:用正方形的左上角的点来表示一个正方形
比如点(5,8),(5,10),(7,8) , (7,10)组成的长方形中
(5,10)(6,10)(5,9)(6,9)就是四个正方形的左顶点
顶点范围=(长方形边的最小值,长方形边的最大值-1)
我的代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 105
int dp[maxn][maxn];
int main()
{
	int a, b, c, d, s = 0;//a,b,c,d为对角线坐标的点(a,b),(c,d),s为点的数目
	memset(dp, 0, sizeof(dp));
	while (cin >> a >> b >> c >> d)
	{
		if (a == -1 && b == -1 && c == -1 && d == -1)// Four -1's are used to separate problems
		{
			for (int i = 0; i < 105; i++)
			{
				for (int j = 0; j < 105; j++)
				{
					if (dp[i][j])
					{
						s++;
					}
				}
			}
			cout << s  << endl;
			memset(dp, 0, sizeof(dp));
			s = 0;
		}
		else if (a == -2 && b == -2 && c == -2 && d == -2)
		{
			for (int i = 0; i < 105; i++)
			{
				for (int j = 0; j < 105; j++)
				{
					if (dp[i][j])
					{
						s++;
					}
				}
			}
			cout << s  << endl;
			memset(dp, 0, sizeof(dp));
			s = 0;
			break;
		}
		else
		{
			int a1, b1, c1, d1;
			a1 = a > c ? a : c;
			b1 = b > d ? b : d;
			c1 = a > c ? c : a;
			d1 = b > d ? d : b;
			for (int i = c1; i < a1; i++)//用每个正方形最左上角的点代表它
			{
				for (int j = d1; j < b1; j++)
					dp[i][j] = 1;
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43866317/article/details/86679547