2013
12-21

# Count the grid

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).

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.

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

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

0
6

PICK定理：S=a/2+b-1; S为多边形面积；a为多边形边上的点； b为多边形内的点；
a为边上的点可以由GCD(x1-x0,y1-y0)求得点数；

#include<iostream>
using namespace std;
#include<math.h>
struct point
{
int x,y;
}p[3];
struct node
{
int fx,fy;
}m[3];
int gcd(int a,int b)
{
if(b==0) return a;
return gcd(b,a%b);
}
int ABS(int x)
{
return x>0?x:-x;
}
int AREA(point p1,point p2,point p0)
{
return ABS((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x));
}
int main()
{
int sum,area,ans;
while(scanf("%d%d%d%d%d%d",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y)!=EOF)
{
if(p[0].x==0&&p[0].y==0&&p[1].x==0&&p[1].y==0&&p[2].x==0&&p[2].y==0) break;
m[0].fx=ABS(p[0].x-p[1].x);    m[0].fy=ABS(p[0].y-p[1].y);
m[1].fx=ABS(p[1].x-p[2].x);    m[1].fy=ABS(p[1].y-p[2].y);
m[2].fx=ABS(p[0].x-p[2].x);    m[2].fy=ABS(p[0].y-p[2].y);
sum=gcd(m[0].fx,m[0].fy)+gcd(m[1].fx,m[1].fy)+gcd(m[2].fx,m[2].fy);//求a
area=AREA(p[1],p[2],p[0]);//求S
ans=(area-sum+2)/2;
printf("%d\n",ans);
}
}

1. #include <cstdio>
#include <algorithm>

struct LWPair{
int l,w;
};

int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=5000, MAXVAL=10000;
LWPair sticks[MAXSIZE];
int store[MAXSIZE];
int ncase, nstick, length,width, tmp, time, i,j;
if(scanf("%d",&ncase)!=1) return -1;
while(ncase– && scanf("%d",&nstick)==1) {
for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
for(time=-1,i=0;i<nstick;++i) {
tmp=sticks .w;
for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
if(j==time) { store[++time]=tmp; }
else { store[j+1]=tmp; }
}
printf("%dn",time+1);
}
return 0;
}