Area
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5227   Accepted: 2342

Description

Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new research and development facility the company has installed the latest system of surveillance robots patrolling the area. These robots move along the walls of the facility and report suspicious observations to the central security office. The only flaw in the system a competitor抯 agent could find is the fact that the robots radio their movements unencrypted. Not being able to find out more, the agent wants to use that information to calculate the exact size of the area occupied by the new facility. It is public knowledge that all the corners of the building are situated on a rectangular grid and that only straight walls are used. Figure 1 shows the course of a robot around an example area.

 
Figure 1: Example area. 
You are hired to write a program that calculates the area occupied by the new facility from the movements of a robot along its walls. You can assume that this area is a polygon with corners on a rectangular grid. However, your boss insists that you use a formula he is so proud to have found somewhere. The formula relates the number I of grid points inside the polygon, the number E of grid points on the edges, and the total area A of the polygon. Unfortunately, you have lost the sheet on which he had written down that simple formula for you, so your first task is to find the formula yourself. 

Input

The first line contains the number of scenarios. 
For each scenario, you are given the number m, 3 <= m < 100, of movements of the robot in the first line. The following m lines contain pairs 揹x dy�of integers, separated by a single blank, satisfying .-100 <= dx, dy <= 100 and (dx, dy) != (0, 0). Such a pair means that the robot moves on to a grid point dx units to the right and dy units upwards on the grid (with respect to the current position). You can assume that the curve along which the robot moves is closed and that it does not intersect or even touch itself except for the start and end points. The robot moves anti-clockwise around the building, so the area to be calculated lies to the left of the curve. It is known in advance that the whole polygon would fit into a square on the grid with a side length of 100 units. 

Output

The output for every scenario begins with a line containing 揝cenario #i:� where i is the number of the scenario starting at 1. Then print a single line containing I, E, and A, the area A rounded to one digit after the decimal point. Separate the three numbers by two single blanks. Terminate the output for the scenario with a blank line.

Sample Input

2
4
1 0
0 1
-1 0
0 -1
7
5 0
1 3
-2 2
-1 0
0 -3
-3 1
0 -3

Sample Output

Scenario #1:
0 4 1.0 Scenario #2:
12 16 19.0

Source

 
题目大意:给定m个点,注意这里不是直接给的坐标,而是给的由前一个点变化多少,所以一定要看清楚题意,都怪自己英语太差,所以,搞了好久都不知道第一个样例怎么出来的。知道这个之后就是将输入来的“坐标”转化为真正的坐标了,第一个点从(0, 0)开始比较好算,一般选择这个。 这个题用到的定理或者说是知识点说一下:
1. Pick定理:给定顶点座标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i、边上格点数目b的关系:A = i + b/2 - 1。
2. GCD(x, y)求一条线穿过的点数:一条线穿过的点格子的点的个数,这句话讲的比较通俗,我看网上有说覆盖的点的个数,刚开始没怎么理解,后来发现意思就是这条线能穿过多少个点,除了起点之外的,就是说假如中间一个点都没穿过,只有两个端点的话,那么它穿过的点就是1,除了起点之外,就1个。其中x是这条线在x轴上的投影,y是在y轴上的投影长度。
3. 叉积的几何意义:多边形的面积=顺序定点的叉积之和
我的代码:
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = ;
struct point{
int x, y;
};
point p[maxn];
int n;
int x_multi(const point p1, const point p2)
{
return (p1.x * p2.y - p2.x * p1.y);
}
int gcd(int a, int b)
{
return b == ? a : gcd(b, a % b);
}
int main()
{
int t;
scanf("%d", &t);
int kase = ;
while (t--)
{
scanf("%d", &n);
p[].x = p[].y = ;
int t1, t2;
for (int i = ; i <= n; i++)
{
scanf("%d %d", &t1, &t2);
p[i].x = p[i - ].x + t1;
p[i].y = p[i - ].y + t2;
}
int area = ;
int outside = ;
for (int i = ; i < n; i++)
{
area += x_multi(p[i], p[i + ]);
outside += gcd(abs(p[i].x - p[i + ].x), abs(p[i].y - p[i + ].y));
}
area += x_multi(p[n], p[]);
outside += gcd(abs(p[n].x - p[].x), abs(p[n].y - p[].y));
if (area < )
area = -area;
int inside = area + - outside;
inside /= ;
printf("Scenario #%d:\n", ++kase);
if (area % == )//如果在边界上的定点个数为偶数,面积就是整数
{
printf("%d %d %d.0\n\n", inside, outside, area / );
}
else//反之就是小数
printf("%d %d %d.5\n\n", inside, outside, area / );
} return ;
}

下面是POJ 2954的代码,原题就不粘了,大意:给三角形的三个点的坐标,求三角形内有多少个点。思路还是Pick定理

AC代码

/*************************************************************************
> File Name: poj_2954.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年04月17日 星期五 20时26分14秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio> using namespace std;
struct point{
int x, y;
};
int x_multi(const point p1, const point p2, const point p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
int gcd(int a, int b)
{
return b == ? a : gcd(b, a % b);
}
int main()
{
point p1, p2, p3;
while (~scanf("%d %d %d %d %d %d", &p1.x, &p1.y, &p2.x, &p2.y, &p3.x, &p3.y) && (p1.x || p2.x || p3.x || p1.y || p2.y || p3.y))
{
int area = x_multi(p1, p2, p3);//这里的area是两倍的area,因为面积没有除以2
if (area < )
area = -area;
int on_edge = ;
on_edge += gcd(abs(p1.x - p2.x), abs(p1.y - p2.y));
on_edge += gcd(abs(p1.x - p3.x), abs(p1.y - p3.y));
on_edge += gcd(abs(p2.x - p3.x), abs(p2.y - p3.y));
int inside = area + - on_edge;//这个就是pick公式两边同时×2
inside /= ;
printf("%d\n", inside);
}
return ;
}

POJ 1265 Area POJ 2954 Triangle Pick定理的更多相关文章

  1. POJ 2954 Triangle (pick 定理)

    题目大意:给出三个点的坐标,问在这三个点坐标里面的整数坐标点有多少个(不包含边上的) 匹克定理:I = (A-E) / 2 + 1; A: 表示多边形面积 I : 表示多边形内部的点的个数 E: 表示 ...

  2. poj 1265 Area 面积+多边形内点数

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5861   Accepted: 2612 Description ...

  3. poj 1265 Area (Pick定理+求面积)

    链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  4. POJ 1265 Area (Pick定理 & 多边形面积)

    题目链接:POJ 1265 Problem Description Being well known for its highly innovative products, Merck would d ...

  5. poj 1265 Area(Pick定理)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5666   Accepted: 2533 Description ...

  6. poj 1265 Area(pick定理)

    Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4373 Accepted: 1983 Description Bein ...

  7. [poj 1265]Area[Pick定理][三角剖分]

    题意: 给出机器人移动的向量, 计算包围区域的内部整点, 边上整点, 面积. 思路: 面积是用三角剖分, 边上整点与GCD有关, 内部整点套用Pick定理. S = I + E / 2 - 1 I 为 ...

  8. POJ 1265 Area (pick定理)

    题目大意:已知机器人行走步数及每一步的坐标变化量,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:叉积求面积,pick定理求点. pick定理:面积=内部点数+边上点数/2-1 ...

  9. poj 1265 Area( pick 定理 )

    题目:http://poj.org/problem?id=1265 题意:已知机器人行走步数及每一步的坐标   变化量 ,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:1.以 ...

随机推荐

  1. 使用js判断一个对象是否为空 比如 obj={}

    今天使用到js判断一个对象是否为空,js没有封装好的方法,这里最好的办法就是使用jquery里面的封装好的方法 $.isEmptyObject(obj)

  2. 开源中国iOS客户端学习

    开源中国iOS客户端学习 续写前言 <开源中国iOS客户端学习>续写前系列博客    http://blog.csdn.net/column/details/xfzl-kykhd.html ...

  3. hibernate spring sturts2配置

    <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hi ...

  4. nosql数据库选型

    http://blogread.cn/it/article/6654 今天在书店里翻完了一遍<七天七数据库>.这本书简单介绍了postgreSQL,riak,mongodb,HBase,r ...

  5. github Travis CI 持续集成

    一个项目如何保证代码质量是开发中非常重要的环节,对于开源项目来说更是如此,因为开源项目要面对的是来自不同水平开发者提交的代码.所以围绕开源做持续集成(Continuous Integration)变得 ...

  6. Spring MVC学习总结。

    公司项目用的Spring MVC.顺便学习学习. 其实框架并没有想象中的复杂.尤其对于初学者,总觉得SSH是一些很复杂的东西似的.其实对初学者来说能够用这些框架就足够了.在公司里也是,基本功能会用了就 ...

  7. ZOJ-2008-Invitation Cards(dijkstra)

    题意: 在有向加权图中G(V,E),邮局要从起点S向其他n个节点发送邮件,于是派出n个邮递员,分别到达其他n个地点发送,然后回到起点S,求出所有邮递员所经过的总路程的最小值. 分析: 正向一次dijk ...

  8. linux配置端口转发

    一.使用rinted进行端口转发 将10.50.13.13 80请求转到10.50.13.11 80上 1.安装rinetd $ tar zxf rinetd.tar.gz $ cd rinetd $ ...

  9. (转载)提高mysql插入数据的速度

    (转载)http://blog.csdn.net/bhq2010/article/details/7376352 需要在mysql中插入2000万条记录,用insert语句插入速度很有限,每秒钟几百条 ...

  10. Android中ListView分页加载数据

    public class MainActivity extends Activity { private ListView listView=null; //listview的数据填充器 privat ...