zoukankan      html  css  js  c++  java
  • POJ 2954 Triangle(计算几何Pick定理)

    Triangle

    Time Limit: 1000MS

     

    Memory Limit: 65536K

    Total Submissions: 3627

     

    Accepted: 1586

    Description

    lattice point is an ordered pair (xy) 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 x1y1x2y2x3, and y3, where (x1y1), (x2y2), and (x3y3) are the coordinates of vertices of the triangle. All triangles in the input will be non-degenerate (will have positive area), and −15000 ≤ x1y1x2y2x3y3 ≤ 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定理可以求出,多边形的面积、内部的点、及边上的点的关系满足area = in + on / 2 - 1,在内部的点的个数in = area - on / 2 + 1;题目和POJ1265相同, 下面写了两种求面积的代码

    代码如下:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    int on, in, area;
    struct Point
    {
        double x;
        double y;
        int flag;
    }p[4];
    int Gcd(int a, int b)//求最大公约数
    {
        int temp;
        if (a < b)
        {
            temp = a;
            a = b;
            b = temp;
        }
        if (b == 0)
        {
            return a;
        }
        return Gcd(b, a % b);
    }
    int Abs(int a)//求绝对值
    {
        if (a < 0)
        {
            return -a;
        }
        return a;
    }
    /*double Multi(Point p1, Point p2, Point p3)//叉乘
    {
        return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
    }*/
    double Cross(Point p1, Point p2)//叉积
    {
        return p1.x * p2.y - p1.y * p2.x;
    }
    int main()
    {
        int i;
        double xx, yy;
        memset(p, 0, sizeof(p));
        while (scanf("%lf%lf", &p[0].x, &p[0].y)!= EOF)
        {
            if (p[0].x == 0 && p[0].y == 0)
            {
                p[0].flag = 1;
            }
            for (i = 1; i < 3; ++i)
            {
                scanf("%lf%lf", &p[i].x, &p[i].y);
                if (p[i].x == 0 && p[i].y == 0)
                {
                    p[i].flag = 1;
                }
            }
            if (p[0].flag == 1 && p[1].flag == 1 && p[2].flag == 1)
            {
                break;
            }
            on = 0;
            area = 0;
            for (i = 0; i < 3; ++i)
            {
                xx = p[i].x - p[(i + 1) % 3].x;
                yy = p[i].y - p[(i + 1) % 3].y;
                on += Gcd(Abs(xx), Abs(yy));
                area += Cross(p[i], p[(i + 1) % 3]);
            }
            area = Abs(area) / 2;
            //area = Abs(Multi(p[0], p[1], p[2])) / 2;
            in = (int)(area - on / 2 + 1);//利用Pick定理
            printf("%d\n", in);
        }
        return 0;
    }
  • 相关阅读:
    蓝桥杯——一步之遥,扩展gcd的应用
    质数唯一分解定理应用——多个数的最小公约数和最大公倍数
    蓝桥杯训练 ——天平称重
    蓝桥杯 奇怪的捐赠——进制的运用
    Kubernetes-基本概念
    Docker-基本概念
    虚拟化向容器化发展
    Kubernetes-服务连接和暴露(endpoints资源)
    Kubernetes-NodePort
    Kubernetes-Load Balancer
  • 原文地址:https://www.cnblogs.com/lidaojian/p/2486491.html
Copyright © 2011-2022 走看看