POJ2043 Area of Polygons
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 1020 | Accepted: 407 |
Description
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
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
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的更多相关文章
- 【POJ】2043.Area of Polygons
原题戳这里 开始一小段时间的POJ计算几何练习计划(估计很快就会被恶心回去) 题解 用一条平行于y轴的扫描线,计算两条扫描线之间多少格子被覆盖了 精度可tm变态了,可能是因为题目要求的关系吧,需要上取 ...
- HPU暑期集训积分赛1
A. Nth power of n 单点时限: 1.0 sec 内存限制: 512 MB 求 nn 的个位数. 输入格式 多组输入,处理到文件结束.每组数据输入一个 n.(1≤n≤109) 输出格式 ...
- POJ Area of Simple Polygons 扫描线
这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...
- POJ1389 Area of Simple Polygons 线段树
POJ1389 给定n个整数点矩形,求面积并. 显然ans必然是整数. 记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束. 进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了 ...
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)
离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...
- Area of Simple Polygons
poj1389:http://poj.org/problem?id=1389 题意:求矩形面积的并题解:扫描线加线段树 同poj1389 #include<iostream> #inclu ...
- POJ 1389 Area of Simple Polygons 扫描线+线段树面积并
---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...
- POJ1389:Area of Simple Polygons——扫描线线段树题解+全套代码注释
http://poj.org/problem?id=1389 题面描述在二维xy平面中有N,1 <= N <= 1,000个矩形.矩形的四边是水平或垂直线段.矩形由左下角和右上角的点定义. ...
- [poj] 1389 Area of Simple Polygons
原题 线段树+扫描线 对于这样一个不规则图形,我们要求他的面积有两种方法,割和补. 补显然不行,因为补完你需要求补上去的内部分不规则图形面积-- 那么怎么割呢? 像这样: 我们就转化成了无数个矩形的和 ...
随机推荐
- lintcode-413-反转整数
413-反转整数 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 标签 整数 ...
- 【Linux】- 简明Vim练习攻略
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- javascriptDOM编程
DOM - Document Object Model,它是W3C国际组织的一套Web标准,它定义了访问HTML文档对象的一套属性,方法和事件. <html> <head> & ...
- 我以前不知道的 Session
之前只知道 Session 是服务器与客户端的一个会话,有默认过期时间,是服务器端的技术,与之对应的是 Cookie 技术,是客户端技术. 下面的几点是之前不知道的:[或者是忘了] 1 . Sessi ...
- [LeetCode] Search in Rotated Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- django学习系列-01
安装Django > pip install django==1.10.3(py2)或者>python3 -m pip install django==1.10.3(py3) 成功安装 D ...
- node web 应用热更新
在每次更改完 node.js 项目后,我们都需要先将 node.js停止(快捷键: Ctrl+C),然后再通过命令再次运行,这样特别麻烦.这里我推荐使用 supervisor工具, npm 安装命令为 ...
- 【Python】Python的time和datetime模块
time 常用的有time.time()和time.sleep()函数. import time print(time.time()) 1499305554.3239055 上面的浮点数称为UNIX纪 ...
- 【数据库_Mysql】查询当前年份的sql
1.本年份 SELECT DATE_FORMAT(NOW(), '%Y'); 2.本月份(显示数字) SELECT DATE_FORMAT(NOW(), '%m'); 3.本月份(显示英文) SELE ...
- sql语句查询各门课程平均分的最大值
解法一: select courseno,stuno,avg(score) '平均分最高值'--这里是求平均,后面的条件是过滤最大值的 from tablename group by courseno ...