Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 1020   Accepted: 407

Description

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

Source

数学问题 几何 扫描线

看到题面心惊胆战,看到数据范围发现就是个暴力扫描线。

维护一条扫描线从x轴最左边往最右边移动,对于每一列记录原图形上线段和扫描线的交点纵坐标。

将交点坐标用floor和ceil取整,就可以愉快地做线段覆盖了。

注意计算交点时候,要先乘后除。先除后乘会被卡精度。

日常犯蠢WA一串,身败名裂。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct point{
double x,y;
point(){}
point(double _x,double _y):x(_x),y(_y){}
}p[mxn];
struct Seg{point s,t;}L[mxn];
double F(const point &a,const point &b,double x){
return a.y+(b.y-a.y)*(x-a.x)/(b.x-a.x);
}
struct Line{
double L,R;
bool operator < (const Line &b)const{
return (L==b.L && R<b.R) || (L<b.L);
}
}s[mxn<<];
int sct=;
int ans=;
void solve(){
sort(s+,s+sct+);
int last=-1e10;
for(int i=;i<sct;i+=){
int x=floor(min(min(s[i].L,s[i+].L),min(s[i].R,s[i+].R)));
int y=ceil(max(max(s[i].L,s[i+].L),max(s[i].R,s[i+].R)));
if(x>=last)ans+=y-x;
else if(y>last)ans+=y-last;
last=y;
}
return;
}
int n;
int main(){
int i,j;
while(scanf("%d",&n)!=EOF && n){
ans=;
int sx=1e8,mx=-1e8;
for(i=;i<=n;i++){
p[i].x=read();p[i].y=read();
sx=min(sx,(int)p[i].x);mx=max(mx,(int)p[i].x);
}
p[n+]=p[];
for(i=;i<=n;i++){//segment
if(p[i].x<p[i+].x){L[i].s=p[i];L[i].t=p[i+];}
else{L[i].s=p[i+];L[i].t=p[i];}
}
for(i=sx;i<mx;i++){
sct=;
for(j=;j<=n;j++){
if(L[j].s.x<=i && L[j].t.x>=i+){
++sct;
s[sct].L=F(L[j].s,L[j].t,i);
s[sct].R=F(L[j].s,L[j].t,i+);
if(s[sct].L>s[sct].R)swap(s[sct].L,s[sct].R);
}
}
solve();
}
printf("%d\n",ans);
}
return ;
}

POJ2043 Area of Polygons的更多相关文章

  1. 【POJ】2043.Area of Polygons

    原题戳这里 开始一小段时间的POJ计算几何练习计划(估计很快就会被恶心回去) 题解 用一条平行于y轴的扫描线,计算两条扫描线之间多少格子被覆盖了 精度可tm变态了,可能是因为题目要求的关系吧,需要上取 ...

  2. HPU暑期集训积分赛1

    A. Nth power of n 单点时限: 1.0 sec 内存限制: 512 MB 求 nn 的个位数. 输入格式 多组输入,处理到文件结束.每组数据输入一个 n.(1≤n≤109) 输出格式 ...

  3. POJ Area of Simple Polygons 扫描线

    这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...

  4. POJ1389 Area of Simple Polygons 线段树

    POJ1389 给定n个整数点矩形,求面积并. 显然ans必然是整数. 记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束. 进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了 ...

  5. 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)

    离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...

  6. Area of Simple Polygons

    poj1389:http://poj.org/problem?id=1389 题意:求矩形面积的并题解:扫描线加线段树 同poj1389 #include<iostream> #inclu ...

  7. POJ 1389 Area of Simple Polygons 扫描线+线段树面积并

    ---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...

  8. POJ1389:Area of Simple Polygons——扫描线线段树题解+全套代码注释

    http://poj.org/problem?id=1389 题面描述在二维xy平面中有N,1 <= N <= 1,000个矩形.矩形的四边是水平或垂直线段.矩形由左下角和右上角的点定义. ...

  9. [poj] 1389 Area of Simple Polygons

    原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...

随机推荐

  1. QCryptographicHash实现哈希值计算,支持多种算法

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QCryptographicHash实现哈希值计算,支持多种算法     本文地址:http: ...

  2. url传带有汉字的参数乱码解决

    url传带有汉字的参数乱码解决 var reg = new RegExp("(^|&)createName=([^&]*)(&|$)"); var r = ...

  3. 修改mac的hosts文件

    第一步:请先打开 Mac 系统中的 Finder 应用,接下来请按快捷键组合 Shift+Command+G 三个组合按键,并输入 Hosts 文件的所在路径:/etc/hosts , 随后即可在 F ...

  4. Monkey自动化测试

    Monkey简介 语法参数 实际应用 一.Monkey简介 1.什么是Monkey? 基于健壮性.稳定性的考虑:如果将一个应用交给一个人长时间不停地乱点乱按,程序会怎么样? 有时候运行相同系列的测试, ...

  5. 第194天:js---函数对象详解(arguments、length)

    一.函数即对象 function add1(a,b){ return a+b; } //Function对象的实例 -- 高级技巧 --- 写框架必须用的... //前面表示参数,后面表示函数语句 v ...

  6. 整合SSM框架应用

    普通方式 新建spring模块时引入如下内容: 启用devtools插件(热部署插件) idea需要做如下配置 settings-build-compiler->勾选build project ...

  7. 【JavaScript】20款漂亮的css字体

    样式一: body { margin: 0; padding: 0; line-height: 1.5em; font-family: "Times New Roman", Tim ...

  8. pycharm中新建并且运行django

    1.对于Bottle框架其本身未实现类似于Tornado自己基于socket实现Web服务,所以必须依赖WSGI,默认Bottle已经实现并且支持的WSGI有: 帮助我们写socket的server. ...

  9. BZOJ 1070 修车 【费用流】

    Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同 的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序, ...

  10. 第三周——构建一个简单的Linux系统MenuOS

    [洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周  构建一个 ...