HDU - 1264(水)

Time limit1000 ms
Memory limit32768 kB
OSWindows

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

思路:数据不大,直接模拟即可。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int a, b, c, d;
bool mark[105][105];
void Out_map()
{
	int num = 0;
	for (int i = 0; i <=105; i++)                         /*这里还可以优化*/
		for (int j =0; j <105; j++)
			if (mark[i][j]==1) num++;
	cout << num << endl;
}
int main()
{
	while(cin>>a>>b>>c>>d)
	{
		if (a < 0)
		{
			Out_map();
			memset(mark, false, sizeof(mark));
			if (a == -2) break;
		}
		else {
			if (a > c) swap(a, c);              /*这里不要忘了*/
			if (b > d) swap(b, d);
			for (int i = a; i < c; i++)
				for (int j = b; j < d; j++)
					mark[i][j]=1;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43967023/article/details/86682780