poj2954

Pick公式:平面上以格子点为顶点的简单多边形,如果边上的点数为on,内部的点数为in,则它的面积为area=on/2+in-1
利用gcd求每个边上的点数。

View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;

struct Point
{
int x, y;
}point[3];

void input()
{
for (int i = 0; i < 3; i++)
scanf("%d%d", &point[i].x, &point[i].y);
int ret = 0;
for (int i = 0; i < 3; i++)
ret |= point[i].x | point[i].y;
if (!ret)
exit(0);
}

int xmult(Point a, Point b, Point c)
{
return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}

int gcd(int x, int y)
{
if (!x || !y)
return x > y ? x : y;
for (int t; t = x % y; x = y, y = t);
return y;
}

void work()
{
int area = abs(xmult(point[0], point[1], point[2]));
int on = 0;
for (int i = 0; i < 3; i++)
on += gcd(abs(point[i % 3].x - point[(i + 1)%3].x), abs(point[i % 3].y - point[(i + 1)%3].y));
int in = (area - on) / 2 + 1;
printf("%d\n", in);
}

int main()
{
//freopen("t.txt", "r", stdin);
while (1)
{
input();
work();
}
return 0;
}

你可能感兴趣的:(poj)