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.以 ...
随机推荐
- github 分支 合并
Git如何进行分支管理? 1.创建分支 创建分支很简单:git branch <分支名> 2.切换分支 git checkout <分支名&g ...
- DataTable举例
// clrTest1.cpp: 主项目文件. #include "stdafx.h" using namespace System; using namespace System ...
- 转:PHP开发框架流行度排名:Laravel居首
原文来自于:http://www.sitepoint.com/best-php-frameworks-2014/ Update: If you’d like to take part in the n ...
- Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants?
Kaggle Bike Sharing Demand Prediction – How I got in top 5 percentile of participants? Introduction ...
- 对依赖倒置原则(DIP)及Ioc、DI、Ioc容器的一些理解(转)
所谓依赖倒置原则(Dependence Inversion Principle)就是要依赖于抽象,不要依赖于具体.简单的说就是要求对抽象进行编程,不要对实现进行编程,这样就降低了客户与实现模块间的耦合 ...
- java学习面向对象之设计模式之单例模式
就像上一节当中我们讲到的数组工具集一样,如果我们把他看作一个类,来应用,不阻止他new函数的话,这个类我们在整个过程当中我们只是用他来当一个工具.假如每次用都要new一下产生一个新对象的话,就会显得整 ...
- POJ 3694 Network(无向图求桥+重边处理+LCA)
题目大意: 给你一个无向图,然后再给你一个Q代表有Q次询问,每一次加一条边之后还有几座桥.在这里要对重边进行处理. 每次加入一条边之后,在这条搜索树上两个点的公共祖先都上所有点的桥都没了. 这里重边的 ...
- (转载)mysql查询一天,查询一周,查询一个月的数据
(转载)http://www.cnblogs.com/likwo/archive/2010/04/16/1713282.html 查询一天: select * from table where to_ ...
- 数据结构(树套树):ZJOI 2013 K大数查询
有几个点卡常数…… 发现若第一维为位置,第二维为大小,那么修改时第一维修改区间,查询时第一维查询区间,必须挂标记.而这种情况下标记很抽象,而且Push_down不是O(1)的,并不可行. 那要怎么做呢 ...
- Unique Binary Search Trees——LeetCode
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...