POJ 1265 Area POJ 2954 Triangle Pick定理
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 -3Sample Output
Scenario #1:
0 4 1.0 Scenario #2:
12 16 19.0Source
#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定理的更多相关文章
- POJ 2954 Triangle (pick 定理)
题目大意:给出三个点的坐标,问在这三个点坐标里面的整数坐标点有多少个(不包含边上的) 匹克定理:I = (A-E) / 2 + 1; A: 表示多边形面积 I : 表示多边形内部的点的个数 E: 表示 ...
- poj 1265 Area 面积+多边形内点数
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5861 Accepted: 2612 Description ...
- poj 1265 Area (Pick定理+求面积)
链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1265 Area (Pick定理 & 多边形面积)
题目链接:POJ 1265 Problem Description Being well known for its highly innovative products, Merck would d ...
- poj 1265 Area(Pick定理)
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5666 Accepted: 2533 Description ...
- poj 1265 Area(pick定理)
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4373 Accepted: 1983 Description Bein ...
- [poj 1265]Area[Pick定理][三角剖分]
题意: 给出机器人移动的向量, 计算包围区域的内部整点, 边上整点, 面积. 思路: 面积是用三角剖分, 边上整点与GCD有关, 内部整点套用Pick定理. S = I + E / 2 - 1 I 为 ...
- POJ 1265 Area (pick定理)
题目大意:已知机器人行走步数及每一步的坐标变化量,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:叉积求面积,pick定理求点. pick定理:面积=内部点数+边上点数/2-1 ...
- poj 1265 Area( pick 定理 )
题目:http://poj.org/problem?id=1265 题意:已知机器人行走步数及每一步的坐标 变化量 ,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:1.以 ...
随机推荐
- html5 DeviceOrientation来实现手机网站上的摇一摇功能
原文地址:http://www.cootm.com/?p=706 从网上转载看到的,感觉不错,就转过来了,特此感谢 cnblogs 的 幸福2胖纸的码农生活,直接转载了,不要介意!呵呵 以下是转载内容 ...
- C题 - A+B for Input-Output Practice (II)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...
- Java中接口与实例化
一.问题引入 前两天学代理模式的时候想到的,接口可不可以new呢? 接口是特殊的抽象类,接口的方法都默认为 public abstract 的... 抽象的方法不 ...
- mysql处理字符串的两个绝招:substring_index,concat
mysql处理字符串的两个绝招:substring_index,concat 最近老是碰到要处理数据库中字符串的处理,发现用来用去也就是这两个函数: 1.substring_index(str,del ...
- jquery禁用右键、文本选择功能、刷新
//禁用右键.文本选择功能.刷新 $(document).bind(“contextmenu”,function(){return false;}); $(document).bind(“select ...
- 数据结构(左偏树):HDU 1512 Monkey King
Monkey King Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 一招解决IE7无法访问https网页
很多人都遇到过这种情况: 自己的IE访问不了https的网页了,如果你百度的话,有人会告诉你注册一堆的dll文件,或者更改IE设置啦什么的.上午,我也遇到这个问题,这些方法都不管用.请教了高手,将方法 ...
- flex与C# Socket通信
原文地址:http://blog.csdn.net/LX10752p/archive/2011/04/27/6366526.aspx Socket 通信没什么好说,一个服务端,多个客户端,很容易搭建环 ...
- 《A First Course in Mathematical Modeling》-chaper1-差分方程建模
从今天开始笔者将通过这个专栏可是对“数学建模”的学习.其实对于“数学建模”自身的内涵或者意义并不需要太多的阐释,下图简洁明了的阐释了数学建模的意义. 其实数学建模本身可以看成换一种角度去解读数学,将我 ...
- 《Mathematical Olympiad——组合数学》——操作和游戏
这篇文章,我们开始对奥数中有关操作和游戏的问题进行分析和讨论,其实在信息学竞赛中涉及到的一些博弈问题(分析必胜策略)的问题(例如巴什博弈.尼姆博弈),本质上来讲,就是组合数学当中的组合游戏,并不是真正 ...