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.
![]() 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 |
提示
题意:
一个机器人在墙上爬来爬去,我们把它走过的轨迹放到直角坐标系里,起点为(0,0),之后给出dx,dy(-100<=dx,dy<=100)表示坐标的变化,请求出围在多边形里点的数量,边缘点的数量以及多边形的面积。
思路:
这里只是需要记住知识点就可以AC了。
1:边缘的点可以通过gcd(abs(x1-x2),abs(y1-y2))求出,题目直接给出了dx,dy就比较方便了。
2:多边形的面积。
将多边形进行三角形剖分,计算每个三角形的面积,加和即可得到多边形的面积。具体来说,三角形OPiPi+1的面积为:Si=(OPi XOPi+1)/2(注:OPi,OPi+1为向量)
这里给出向量积的代码:
int f(struct point a,struct point b)
{
return a.x*b.y-a.y*b.x;
}
多边形(P1,P2...Pn)的面积为:S=1/2∑(OPi X OPi+1)
这里要注意,向量积求面积会有负数的情况,所以要记得取绝对值。
3: 皮克定理:给定坐标均是整点的简单多边形,设其面积为S,内部整点数为a,边界上整点数为b,那么他们满足关系S=a+b/2-1。示例程序
Source Code
Problem: 1265 Code Length: 753B
Memory: 392K Time: 16MS
Language: GCC Result: Accepted
#include <stdio.h>
struct
{
int x,y;
}p[100];
int gcd(int a,int b)
{
int t;
while(b!=0)
{
t=a%b;
a=b;
b=t;
}
return a;
}
int f(int x,int y,int x1,int y1)
{
return x*y1-y*x1;
}
int main()
{
int t,i,i1,n,dx,dy,b,s;
scanf("%d",&t);
for(i=1;t>=i;i++)
{
p[0].x=0;
p[0].y=0;
b=0;
s=0;
scanf("%d",&n);
for(i1=1;n>=i1;i1++)
{
scanf("%d %d",&dx,&dy);
p[i1].x=p[i1-1].x+dx;
p[i1].y=p[i1-1].y+dy;
b=b+gcd(abs(dx),abs(dy));
s=s+f(p[i1].x,p[i1].y,p[i1-1].x,p[i1-1].y);
}
printf("Scenario #%d:\n%d %d %.1f\n\n",i,(abs(s)-b+2)/2,b,abs(s)*1.0/2);
}
return 0;
}