Counting Squares HDU - 1264(扫描)

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

问题链接
HDU - 1264

问题简述
给你四个点作为矩形坐标,求矩形覆盖面积。

问题分析
由题例来看,四个点对应的坐标是(X1,Y1,X2,Y2)
由于坐标轴空间不大,可以将其拆分为n个1X1格子,扫描标记。

#include <cstring>
#include <ctime>
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define maxn 105  //规定坐标范围
bool mp[maxn][maxn];
int main()
{
    int x1,y1,x2,y2;
    int cnt=0;
    memset(mp,false,sizeof(mp));
    while(cin>>x1>>y1>>x2>>y2)
    {
        if(x1==-1)//当全部为-1时输入下一组
		{
			cout<<cnt<<endl;
			memset(mp,false,sizeof(mp));
			cnt=0; 
		}
        if(x1==-2)//当全为-2时结束输入
		{
        	cout<<cnt<<endl;
			break;
		}
        if(x1>x2)swap(x1,x2);
        if(y1>y2)swap(y1,y2);
        for(int i=x1;i<x2;i++)
        for(int j=y1;j<y2;j++)
        if(mp[i][j])continue;
        else
		{
		 	cnt++;
			mp[i][j]=true;//标记
		}
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值