poj 1265 Area(pick定理)

poj 1265 Area

http://poj.org/problem?id=1265

pick定理+叉积+欧几里德(gcd)

题意:有一个单位长度为1的方格组成的区域,告诉你一个人在x,y轴上的一动距离,问这个人行走路线上的点的个数,最终围成区域的点个数,及面积。

方法:显然用pick定理,首先通过叉积计算面积,通过最大公约数求边上的点的个数,区域内部点即可求出

难点:最大公约数求经过的点的个数。

pick定理:(通过点求面积)

在一个平面直角坐标系内,以整点为顶点的简单多边形(任两边不交叉),它内部整点数为a,它的边上(包括顶点)的整点数为b,则它的面积S = a+b/2-1;

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cstdio>
 6 #include <cstring>
 7 #include <cmath>
 8 #include <stack>
 9 #include <queue>
10 #include <functional>
11 #include <vector>
12 #include <map>
13 using namespace std;
14 #define M 0x0f0f0f0f
15 #define min(a,b) (a>b?b:a)
16 #define max(a,b) (a>b?a:b)
17 //priority_queue<int> pq;
18 //queue<int >q;
19 int gcd(int a ,int b)
20 {
21     if(a<0)
22         a=-a;
23     if(a>0)
24         b=-b;
25     return b==0?a:gcd(b,a%b);
26 }
27 double area(int x1,int y1, int x2, int y2)
28 {
29     return x1*y2-x2*y1;
30 }
31 int main()
32 {
33     int n,m,x[110],y[110],i,j,dx,dy;
34     int on,in;
35     double are;
36     scanf("%d",&n);
37     for(i=1; i<=n; i++)
38     {
39         scanf("%d",&m);
40         x[0]=0;
41         y[0]=0;
42         are=0;
43         on=0;
44         for(j=1; j<=m; j++)
45         {
46             scanf("%d%d",&dx,&dy);
47             x[j]=x[j-1]+dx;
48             y[j]=y[j-1]+dy;
49             are+=area(x[j-1],y[j-1],x[j],y[j]);
50             on+=gcd(dx,dy);
51         }
52         in=(are-on+2)/2;
53         printf("Scenario #%d:\n%d %d %.1f\n\n",i,in,on,are/2.0);
54     }
55     return 0;
56 }
View Code
原文地址:https://www.cnblogs.com/bibier/p/3881440.html