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. 求最大子串和以及其中一个子串(java)

    public static void getMaxSum(int[] a){ int max = a[0]; int sum = a[0]; int temp = 0; int start = 0; ...

  2. caffe环境搭建笔记

    首先安装以下库或软件 sudo apt-get install gitsudo apt-get install      libprotobuf-dev     libleveldb-dev    l ...

  3. JavaScript与OC的交互-WebViewJavascriptBridge

    WebViewJavascriptBridge实现了在使用UIWebView时JS与ios 的Objective-C nativecode之间的互相调用, 支持的功能有消息发送.接收.消息处理器的注册 ...

  4. WPF和Expression Blend开发实例:模拟QQ登陆界面打开和关闭特效

    不管在消费者的心中腾讯是一个怎么样的模仿者抄袭者的形象,但是腾讯在软件交互上的设计一直是一流的.正如某位已故的知名产品经理所说的:设计并非外观怎样,感觉如何.设计的是产品的工作原理.我觉得腾讯掌握了其 ...

  5. Linux的cut命令

    cut是一个选取命令,就是将一段数据经过分析,取出我们想要的.一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的. (1)其语法格式为:cut  [-bn] [file] 或 cut ...

  6. 【移动端debug-1】css3中box-shadow的溢出问题

    今天做项目遇到一个box-shadow的溢出父容器的问题,如下面的代码中,子容器inner的box-shadow在没有任何设置的情况下是溢出父容器的. 代码: <!DOCTYPE html> ...

  7. 【bzoj2829】信用卡凸包 凸包

    题目描述 输入 输出 样例输入 26.0 2.0 0.00.0 0.0 0.02.0 -2.0 1.5707963268 样例输出 21.66 题解 凸包 傻逼题,答案显然为:所有圆心构成的凸包周长+ ...

  8. presence_of_element_located与visibility_of_element_located区别

    selenium 问题:加了显性等待后,操作元素依然出错   背景: 用WebDriverWait时,一开始用的是presence_of_element_located,我对它的想法就是他就是用来等待 ...

  9. Now or later UVALive - 3211(2-SAT 最小值最大化)

    emmm...去吃早饭了... rujia讲的很好.. 最小值最大化问题,,,二分枚举答案   设x1.x2为同一个集合中的元素,y1.y2为另一个集合中的元素,如果x1与y1之差小于mid,那么如果 ...

  10. [NOIP2016 D1T3]换教室 【floyd+概率dp】

    题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 2n2n 节课程安排在 nn 个时间段上.在第 ii(1 \leq i \leq n1≤ ...