您的位置:首页 > 其它

poj2954 Triangle

2013-08-27 19:51 232 查看
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 ≤ x1, 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


题目大意:就是简单的求一个三角形内的点的个数,其中不包含边上的点。

题解:

需要知道一个叫PICK的定理:

三角形面积s 三角形内部点 n 三角形边上的点 w

n + w/2 - 1 = s

知道三角形顶点求面积很好做,代码里用了最笨的海伦公式

然后求边上的整点数(就是线段上的最大公约数)

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <queue>
#include <cstdio>
#include <cmath>
#include <string>
#include <stack>
using namespace std;
struct point {
int x,y;
};

int gcd(int m,int n) {
if(n==0) return m;
return gcd(n,m%n);
}

int main() {
point a[5];
while(1) {
int sum=0;
int i;
for(i=1; i<=3; i++) {
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].x==0&&a[i].y==0)
sum++;
}
if(sum==3)
break;
if((a[2].y-a[1].y)*(a[3].x-a[2].x)==(a[3].y-a[2].y)*(a[2].x-a[1].x)) {
printf("0\n");
continue;
}
int ss=abs( (a[2].x-a[1].x)*(a[3].y-a[1].y)-(a[3].x-a[1].x)*(a[2].y-a[1].y) );
int b=gcd(abs(a[2].x-a[1].x),abs(a[2].y-a[1].y))+gcd(abs(a[3].x-a[2].x),abs(a[3].y-a[2].y))+gcd(abs(a[1].x-a[3].x),abs(a[1].y-a[3].y));
printf("%d\n",(ss-b)/2+1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息