Triangle

 

题目连接:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&contestId=282&id=3502

Triangle

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 8            Accepted: 3

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 x1y1 = 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定理做。

代码如下:

 

View Code
#include<stdio.h>
struct Point
{
int x,y;
};
double mult(Point a, Point b, Point c) //叉乘
{
return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int abs(int a)
{
return a>0 ? a : -a;
}
int gcd(int a, int b)
{
int r=b;
while (r)
{
r
=a%b;
a
=b;
b
=r;
}
return a;
}
int main()
{
Point p[
3];
while (1)
{
int i;
bool flag=false;
for (i=0; i<3; i++)
{
scanf(
"%d%d", &p[i].x, &p[i].y);
if (p[i].x || p[i].y) flag=true;
}
if (!flag) break;
double on=0;
for (i=0; i<3; i++) //计算三角形边上的点(包括顶点)
{
int dx=p[i].x-p[(i+1)%3].x;
int dy=p[i].y-p[(i+1)%3].y;
on
+=gcd(abs(dx), abs(dy));
}
//printf("on: %d\n", on);
double s=abs(mult(p[0],p[1],p[2])); //printf("%lf\n", s);
printf("%.0lf\n", s/2-on/2+1); //s=in+on/2-1;(pick定理)
}
return 0;
}

代码中求三角形边上的点的个数算法的讲解:

如上图所示,三角形ABC,点A(0,0),B(9,0),C(9,3),AB的长度为9,BC的长度为3;9和3的最大公约数是3,也就是说三角形ABC缩小为原来的2/3(即三角形AB'C'),或者1/3(即三角形AB''C'')时,点B'(6,0),C'(6,1)的坐标仍是整数,因为AB,BC长度的最大公约数是3;同理点B''(3,0),C''(3,1),也是整数。从上面的情况可以看出来,如果边AB,BC的最大公约数是n的话,AB中间整数点的个数就是n-1;

posted on 2011-04-09 16:07  tzc_yujunyong  阅读(394)  评论(1编辑  收藏  举报