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. XAMPP配置虚拟主机

    当你在本地进行单个网站建设和测试的时候,你只需要正常的安装一下XAMPP就好了.XAMPP本身是集成了apache.mysql和php的.然而当你本地测试站点一多的话,你就不得不考虑使用多个虚拟主机来 ...

  2. iOS:KVO/KVC 的概述与使用

    iOS:KVO/KVC 的概述与使用       KVO   APP开发技术QQ群:347072638 一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性 ...

  3. MySql可视化工具MySQL Workbench使用教程

    1. MySQL Workbench MySQL Workbench 为数据库管理员.程序开发者和系统规划师提供可视化的Sql开发.数据库建模.以及数据库管理功能. 2.MySQL Workbench ...

  4. 再来,LVS+KEEPALIVED

    记得常规组合哟. 一般同时实现HA+LB. 如果只需要实现一个,那还不如UCARP?双机绑定一个IP作热备. CENTOS6:PACEMAKER+COROSYNC+HAPROXY. OTHER:HEA ...

  5. Delphi 的各种错误信息(中英文)

    ******************************* * 编 译 错 误 信 息 * ******************************* ';' not allowed befo ...

  6. left join 、right join 、inner join和 full join的区别

    内连接      INNER JOIN(等值连接):只显示两个表中联结字段相等的行.这个和用select查询多表是一样的效果,所以很少用到: 外连接:LEFT JOIN :以左表为基础,显示左表中的所 ...

  7. linux setsockopt

     linux setsockopt Socket描述符选项[SOL_SOCKET] #include <sys/socket.h> int setsockopt( int socket, ...

  8. Naive and Silly Muggles (计算几何)

    Naive and Silly Muggles Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  9. -_-#【jsonp】cache

    Cache jQuery’s JSONP Calls <script src="http://upcdn.b0.upaiyun.com/libs/jquery/jquery-2.0.3 ...

  10. Linux Shell编程(5)——shell特殊字符(下)

    {}代码块[花括号]. 这个结构也是一组命令代码块,事实上,它是匿名的函数.然而与一个函数所不同的,在代码块里的变量仍然能被脚本后面的代码访问. bash$ { local a;      a=123 ...