1. 程式人生 > >poj2954 Triangle

poj2954 Triangle

using 內部 -o cal red pan ron turn ++

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

題目:

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

Description

A lattice point is an ordered pair (x, y) 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 x1, y1, x2, y2, x3, and y3, where (x1, y1), (x2, y2), and (x3, y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x

1, y1, x2, y2, x3, y3 ≤ 15000. The end-of-file is marked by a test case with x1 = y1 = x2 = y2 = x3= y3 = 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

Stanford Local 2004 思路:   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 }

poj2954 Triangle