poj2954 Triangle

地址:http://poj.org/problem?id=2954

题目:

Triangle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6006   Accepted: 2576

Description

lattice point is an ordered pair (xy) where x and y are both integers. Given the coordinates of the vertices of a triangle (which happen to be lattice points), you are to count the number of lattice points which lie completely inside of the triangle (points on the edges or vertices of the triangle do not count).

Input

The input test file will contain multiple test cases. Each input test case consists of six integers x1y1x2y2x3, and y3, where (x1y1), (x2y2), and (x3y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1y1x2y2x3y3 ≤ 15000. The end-of-file is marked by a test case with x1 =  y1 = x2 = y2 = x3y3 = 0 and should not be processed.

Output

For each input case, the program should print the number of internal lattice points on a single line.

Sample Input

0 0 1 0 0 1
0 0 5 0 0 5
0 0 0 0 0 0

Sample Output

0
6

Source

 
思路:
  pick定理:对于格点多边形(所有的顶点均在格点上的多边形),其面积公式 2*S = 2*a + b - 2 (其中b为在边上的格点数,a为在多边形内部的格点数)
  端点在格点上的线段穿过的格点数:gcd(abs(pe.x-ps.x),abs(pe.y-ps.y))+1
  也就是说在线段ps,pe上,除了起点ps外经过的格点数。
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 #define MP make_pair
 8 #define PB push_back
 9 typedef long long LL;
10 typedef pair<int,int> PII;
11 const double eps=1e-8;
12 const double pi=acos(-1.0);
13 const int K=1e6+7;
14 const int mod=1e9+7;
15 
16 int x[5],y[5];
17 
18 int main(void)
19 {
20     while(1)
21     {
22         int ff=0;
23         for(int i=0;i<3;i++)
24             scanf("%d%d",x+i,y+i),ff+=!y[i]&&!x[i];
25         if(ff==3)break;
26         int cnt=0,s=(x[1]-x[0])*(y[2]-y[0])-(x[2]-x[0])*(y[1]-y[0]);
27         for(int i=0;i<3;i++)
28             cnt+=__gcd(abs(x[(i+1)%3]-x[i]),abs(y[(i+1)%3]-y[i]));
29         printf("%d\n",(abs(s)+2-cnt)/2);
30     }
31     return 0;
32 }

 

posted @ 2017-10-08 17:20  weeping  阅读(205)  评论(0编辑  收藏  举报