POJ2043 Area of Polygons

Yoko's math homework today was to calculate areas of polygons in the xy-plane. Vertices are all aligned to grid points (i.e. they have integer coordinates). 
Your job is to help Yoko, not good either at math or at computer programming, get her homework done. A polygon is given by listing the coordinates of its vertices. Your program should approximate its area by counting the number of unit squares (whose vertices are also grid points) intersecting the polygon. Precisely, a unit square "intersects the polygon" if and only if the intersection of the two has non-zero area. In the figure below, dashed horizontal and vertical lines are grid lines, and solid lines are edges of the polygon. Shaded unit squares are considered intersecting the polygon. Your program should output 55 for this polygon (as you see, the number of shaded unit squares is 55). 

Input

The input file describes polygons one after another, followed by a terminating line that only contains a single zero. 

A description of a polygon begins with a line containing a single integer, m (>= 3), that gives the number of its vertices. It is followed by m lines, each containing two integers x and y, the coordinates of a vertex. The x and y are separated by a single space. The i-th of these m lines gives the coordinates of the i-th vertex (i = 1,...,m). For each i = 1,...,m-1, the i-th vertex and the (i+1)-th vertex are connected by an edge. The m-th vertex and the first vertex are also connected by an edge (i.e., the curve is closed). Edges intersect only at vertices. No three edges share a single vertex (i.e., the curve is simple). The number of polygons is no more than 100. For each polygon, the number of vertices (m) is no more than 100. All coordinates x and y satisfy -2000 <= x <= 2000 and -2000 <= y <= 2000. 

Output

The output should consist of as many lines as the number of polygons. The k-th output line should print an integer which is the area of the k-th polygon, approximated in the way described above. No other characters, including whitespaces, should be printed.

Sample Input

4
5 -3
1 0
1 7
-7 -1
3
5 5
18 5
5 10
3
-5 -5
-5 -10
-18 -10
5
0 0
20 2
11 1
21 2
2 0
0

Sample Output

55
41
41
23

题意:给你多边形的顶点坐标,求最少要用多少个1*1的正方形方格去覆盖

思路:求出每条直线对于每个x坐标的y,然后直接求,注意ceil和floor

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define eps 1e-9
#define N 110
#define inf 1000000007
struct Point
{
    double x,y;
}e[N];
struct node
{
    Point s,t;
}a[N];
struct Node
{
    double l,r;
}b[N];
int cmp(Node u,Node v)
{
    if(u.l==v.l)
        return u.r<v.r;
    return u.l<v.l;
}
int main()
{
    int i,j,k,n,ans,t,p;
    double x1,x2,d,t1,t2;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        x1=inf;
        x2=-inf;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&e[i].x,&e[i].y);
            if(e[i].x<x1)
                x1=e[i].x;
            if(e[i].x>x2)
                x2=e[i].x;

          //  求横坐标的最大值和最小值不能用min和max函数,好像是double的原因,找了很久的BUG。。。
        }
        e[n]=e[0];
        for(i=0;i<n;i++)
        {
            if(e[i].x<e[i+1].x)
            {
                a[i].s=e[i];
                a[i].t=e[i+1];
            }
            else
            {
                a[i].s=e[i+1];
                a[i].t=e[i];
            }
        }
        ans=0;
        for(d=x1;d+1<=x2;d=d+1)
        {
            k=0;
            for(i=0;i<n;i++)
            if(a[i].s.x<=d&&a[i].t.x>=d+1)
            {
                b[k].l=a[i].s.y+(a[i].t.y-a[i].s.y)*(d-a[i].s.x)/(a[i].t.x-a[i].s.x);
                b[k++].r=a[i].s.y+(a[i].t.y-a[i].s.y)*(d+1-a[i].s.x)/(a[i].t.x-a[i].s.x);
            }
            sort(b,b+k,cmp);
            t=0;
            p=-2010;
            for(i=0;i+1<k;i=i+2)
            {
                t1=ceil(max(b[i+1].r,b[i+1].l));
                t2=floor(min(b[i].r,b[i].l));
                t+=t1-t2;
                if(t2<=p)
                    t-=(p-t2);
                p=t1;
            }
            if(t>0)
                ans=ans+t;
        }
        printf("%d\n",ans);
    }
    return 0;
}
 

猜你喜欢

转载自blog.csdn.net/qq_37751662/article/details/81415842