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.以 ...
随机推荐
- 转:《IIC时序》
I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线产生于在80年代,最初为音频和视频设备开发,如今主 ...
- 专家解读Linux操作系统内核中的GCC特性
专家解读Linux操作系统内核中的GCC特性 Linux内核使用GNU Compiler Collection (GCC)套件的几个特殊功能.这些功能包括提供快捷方式和简化以及向编译器提供优化提示 ...
- 最新game
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...
- 转:USB主机控制器(Host Controller)--深入理解
1. 主机控制器(Host Controller) • UHCI: Universal Host Controller Interface (通用主机控制接口, USB1.0/1.1) • ...
- [置顶] 2014年八大最热门IT技能
根据Computerworld网站组织的年度预测调查,众多IT专业人士在2014年所面临的整体就业形势与今年基本持平——今年有33%的企业有计划增加IT部门的员工数量,而未来一年则有32%的企业有此打 ...
- bzoj3208
乍一看感觉好神,仔细一看数据范围…… 什么水题啊,直接暴力就可以了…… ..,..] of longint; v:..,..] of boolean; i,j,k,a1,a2,b1,b ...
- jquery 学习第一课之start
1.$选取符 ( $ == jQuery ) (1) $("div").addClass("special");选取本页面中的所有<div>元素,然 ...
- Count Primes ——LeetCode
Description: Count the number of prime numbers less than a non-negative number, n. 题目大意:给一个int,返回小于它 ...
- ScrollView自动滑到底部
// 自动滑动到底部 mScrollView.post(new Runnable() { @Override public void run() { mScrollView.fullScroll(Sc ...
- Azkaban2配置过程
Azkaban2配置过程 azkaban2所需环境:jdk1.6.ant.jetty.hadoop.ssl证书 通过http://azkaban.github.io/azkaban2/download ...