Area(Pick定理POJ1256)
2015-08-24 15:40
141 查看
Area
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.
![](http://poj.org/images/1265_1.jpg)
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
Sample Output
Source
Northwestern Europe 2001
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5429 | Accepted: 2436 |
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.
![](http://poj.org/images/1265_1.jpg)
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
Northwestern Europe 2001
#include <set> #include <map> #include <list> #include <stack> #include <cmath> #include <vector> #include <queue> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const double eps = 1e-5; int n,m; struct node { int x; int y; }Point[110]; int GCD(int a,int b)//计算边缘上的点,不计起点(将两个点转化为向量,<strong>a</strong>=n<strong>b,</strong>每个向量都可由从起点起,第一个到达格点的点形成的向量组成,(想想为什么),n就是这个向量的x,y的GCD) { return b==0?a:GCD(b,a%b); } int area(node a,node b)//叉积计算面积 { return a.x*b.y-a.y*b.x; } int main() { int T; int m; int w=1; scanf("%d",&T); while(T--) { scanf("%d",&m); int num=0; int ant=0; double ans=0; for(int i=0;i<m;i++) { scanf("%d %d",&Point[i].x,&Point[i].y); if(i) { Point[i].x+=Point[i-1].x; Point[i].y+=Point[i-1].y; num+=GCD(abs(Point[i].x-Point[i-1].x),abs(Point[i].y-Point[i-1].y)); ans+=area(Point[i-1],Point[i]); } } num+=GCD(abs(Point[n-1].x-Point[0].x),abs(Point[n-1].y-Point[0].y)); ans+=area(Point[m-1],Point[0]); if(ans<0) { ans=-ans; } ant=(int)(ans/2-(double)num/2+1);//pick定理 printf("Scenario #%d:\n",w++); printf("%d %d %.1f\n\n",ant,num,ans/2); } return 0; }
相关文章推荐
- strace命令用法详解
- Autolayout断点调试
- linux下在grub.cfg中配置win7启动引导
- android开发之重写Application类
- 【system】linux 常用命令
- html中出现的script失效
- 运算符优先级和结合性
- 华为机试再回忆--第一题
- Area(Pick定理POJ1256)
- 在table中tr的display:block在firefox,chrome下显示布局错乱问题
- 旋转三维物体(加三维物体上)
- parseSdkContent failed Could not initialize class android.graphics.Typeface
- Strace在Linux下简单使用方法
- webService的基础知识二
- JQuery(三)——操作HTML和CSS内容
- C# 二进制字符串互转
- Algorithms—242.Valid Anagram
- Android自定义ProgressDialog
- java常见的面试题
- android中跨进程通讯的4种方式