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. lintcode-382-三角形计数

    382-三角形计数 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? 样例 例如,给定数组 S = {3,4,6,7},返回 3 其 ...

  2. 01.1 Windows环境下JDK安装与环境变量配置详细的图文教程

    01.1 Windows环境下JDK安装与环境变量配置详细的图文教程 本节内容:JDK安装与环境变量配置 以下是详细步骤 一.准备工具: 1.JDK JDK 可以到官网下载 http://www.or ...

  3. 第八章 Mysql运算符

    算术运算符 符号 表达式形式 作用 + x1+x2 加法 - x1-x2 减法 * x1*x2 乘法 / x1/x2 除法 div x1 div x2 同上 % x1%x2 取余 mod mod(x1 ...

  4. 构建一个内网的私有CA步骤

    1:使用openssl命令生成一个私钥,私钥必须放在/etc/pki/CA/private/目录下 (umask 077; openssl genrsa -out /etc/pki/CA/privat ...

  5. laraven安装记录

    版本4.2.11 下载地址:https://codeload.github.com/laravel/laravel/zip/v4.2.11 步骤: 1.解压到目录 2.下载composer,并放到/u ...

  6. 【C++】new和delete表达式与内存管理

    new和delete表达式可以用来动态创建和释放单个对象,也可以用来动态创建和释放动态数组. 定义变量时,必须指定其数据类型和名字.而动态创建对象时,只需指定其数据类型,而不必为该对象命名.new表达 ...

  7. (转)linux下压缩和归档相关命令tar,zip,gzip,bzip2

    压缩包也有两种形式,一种是tar.gz包(.tgz包也是这种),一种是tar.bz2包. tar.gz包的解压方法:tar zxvf [PackageName].tar.gz tar.bz2包的解压方 ...

  8. Spring Boot系列教程八: Mybatis使用分页插件PageHelper

    一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...

  9. BZOJ4104:[Thu Summer Camp 2015]解密运算——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4104 对于一个长度为N的字符串,我们在字符串的末尾添加一个特殊的字符".".之 ...

  10. 廖大大python学习笔记1

    列表classmates = ['Michael', 'Bob', 'Tracy']classmates.append('tom')print classmates# classmates.inser ...