1418 - Trees on My Island ( Peake's theorem )
来源:互联网 发布:五金进入货软件 编辑:程序博客网 时间:2024/06/04 19:48
I have bought an island where I want to plant trees in rowsand columns. So, the trees will form a rectangular grid and each of them can bethought of having integer coordinates by taking a suitable grid point as theorigin.
But, the problem is that the island itself is notrectangular. So, I have identified a simple polygonal area inside the islandwith vertices on the grid points and have decided to plant trees on grid pointslying strictly inside the polygon.
Figure: A sample ofmy island
For example, in the above figure, the green circles form thepolygon, and the blue circles show the position of the trees.
Now, I seek your help for calculating the number of treesthat can be planted on my island.
Input
Input starts with an integer T (≤ 100),denoting the number of test cases.
Each case starts with a line containing an integer N (3≤ N ≤ 10000) denoting the number of vertices of the polygon.Each of the nextN lines contains two integers xi yi(-106 ≤ xi, yi ≤ 106)denoting the co-ordinate of a vertex. The vertices will be given in clockwiseor anti-clockwise order. And they will form a simple polygon.
Output
For each case, print the case number and the total number oftrees that can be planted inside the polygon.
题解:
求点阵多边形内整点个数, 由皮克定理:
一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,S表示多边形的面积。 -- 百度百科
多边形面积 S 由叉积容易求出,两条边上的点数,也是欧几里得算法的应用。故有皮克定理,a很容易求出。
/*********************************************** * Author: fisty * Created Time: 2015-10-11 16:15:25 * File Name : lightoj1418.cpp *********************************************** */#include <iostream>#include <cstring>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <algorithm>using namespace std;#define Debug(x) cout << #x << " " << x <<endl#define Memset(x, a) memset(x, a, sizeof(x))const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int> P;#define FOR(i, a, b) for(int i = a;i < b; i++)#define lson l, m, k<<1#define rson m+1, r, k<<1|1#define MAX_N 11000 int t, n;struct node{ LL x, y; }p[MAX_N]; LL gcd(LL a, LL b){ return b == (LL)0 ? a : gcd(b, a % b);}LL cross(struct node e1, struct node e2, struct node e3){ return (e2.x - e1.x) * (e3.y - e1.y) - (e3.x - e1.x ) * (e2.y - e1.y);}int main() { //freopen("in.cpp", "r", stdin); //cin.tie(0); //ios::sync_with_stdio(false); scanf("%d", &t); int cnt = 1; while(t--){ scanf("%d", &n); for(int i = 0;i < n; i++){ scanf("%lld%lld", &p[i].x, &p[i].y); } p[n] = p[0]; LL count = 0; for(int i = 0;i < n; i++){ LL a = abs(p[i+1].x - p[i].x); LL b = abs(p[i+1].y - p[i].y); LL c = gcd(a, b); count += c; } LL area = 0; for(int i = 1;i < n; i++){ area += cross(p[0], p[i], p[i+1]); } printf("Case %d: %lld\n",cnt++, abs(area / 2) + 1 - count / 2); } return 0;}
- 1418 - Trees on My Island ( Peake's theorem )
- LightOJ 1418 - Trees on My Island (Pick`s theorem)
- LightOj 1418 Trees on My Island (Pick定理)
- UVA 10088 Trees on My Island
- UVa 10088 - Trees on My Island
- UVa Problem 10088 Trees on My Island (岛上的树)
- UVA 10088 Trees on My Island(求整顶点多边形内部整点数)
- Dirichlet's Theorem on Arithmetic Progressions (poj 3009 水题)
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions(水~)
- lightoj1418Trees on My Island【pick公式】
- POJ3006 Dirichlet's Theorem on Arithmetic Progressions
- poj3006Dirichlet's Theorem on Arithmetic Progressions
- POJ3006:Dirichlet's Theorem on Arithmetic Progressions
- Dirichlet's Theorem on Arithmetic Progressions
- Dirichlet's Theorem on Arithmetic Progressions
- Dirichlet's Theorem on Arithmetic Progressions
- poj3006 Dirichlet's Theorem on Arithmetic Progressions
- Dirichlet's Theorem on Arithmetic Progressions
- xml之schema例子
- hdu 5500 Reorder the Books(贪心)
- Myeclipse中SVN的使用
- 扫描线 求挖空矩形合并面积 hdu3265 Posters
- poj 1195 Mobile phones(二维树状数组)
- 1418 - Trees on My Island ( Peake's theorem )
- 构建android UI 学习心得,摘要
- HDU 5119 Happy Matt Friends (01背包)
- 扫描线 矩形内围起的星星亮度总和最大大 poj2482 Stars in Your Window
- Sql存储过程中判断某个数据表的某一行的某列的值是否为NUll
- lintcode-将表达式转换为逆波兰表达式-370
- 第二阶段计划
- 算法研发工作中对于MPI和Spark的一些困惑?
- 轻量化ViewController的几个小技巧