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.
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. 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 |
[Submit] [Go Back] [Status] [Discuss]
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator
这道题让你求的是多边形边界,内部有几个点以及多边形的面积。要用到皮克公式
Pick公式:平面上以格子点为顶点的简单多边形的 面积 = 边上的点数/2+内部的点数-1。
任意一个多边形的面积等于 :按顺序 求相邻两个点与原点组成的向量的叉积之和/2(可以利用三角形求证,需要取绝对值 二维叉积公式:x1y2-x2y1)。这里面我们可以把原点设置为0。0点。
以格子点为顶点的线段,其覆盖的点的个数为GCD(dx,dy),dxdy分别为线段横向坐标差值和纵向差值。如果dx或dy为0,则覆盖的点数为dy或dx(此时平行于坐标轴)。
注意这道题里面输入的是dx,dy不是每一个点的坐标,而是沿着x轴,y轴方向走了多少步。所以要想办法把每一个点的坐标求出来,可以通过每一步走步和前一个点想加来求出点的坐标。
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=10005;
int n;
struct Line{
int x,y;
}line[MAXN];
int det(Line l1,Line l2){
return l1.x*l2.y-l2.x*l1.y;
}
int gcd(int a,int b){
if(b==0) return a;
else return gcd(b,a%b);
}
double poloygonArea(Line* l,int n){
double area;
for(int i=2;i<=n;i++){
area=area+det(l[i],l[i-1]);
}
return fabs(area/2);
}
int main(){
int t;
cin>>t;
int kase=0;
while(t--){
cin>>n;
double ans=0;//计算多边形内部的格点数
for(int i=1;i<=n;i++){
scanf("%d%d",&line[i].x,&line[i].y);
ans=ans+gcd(abs(line[i].x),abs(line[i].y));
line[0].x=0;
line[0].y=0;
line[i].x=line[i].x+line[i-1].x;
line[i].y=line[i].y+line[i-1].y;
}
double area=poloygonArea(line,n);
double edge=area+1-ans/2;
printf("Scenario #%d:\n",++kase);
printf("%d %d %.1lf\n",(int)edge,(int)ans,area);
printf("\n");
}
return 0;
}