poj 1265 Area (Pick定理+求面积)
链接:http://poj.org/problem?id=1265
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4969 | Accepted: 2231 |
Description
Figure 1: Example area.
You are hired to write a program that calculates the area occupied by the new facility from the movements of a robot along its walls. You can assume that this area is a polygon with corners on a rectangular grid. However, your boss insists that you use a formula he is so proud to have found somewhere. The formula relates the number I of grid points inside the polygon, the number E of grid points on the edges, and the total area A of the polygon. Unfortunately, you have lost the sheet on which he had written down that simple formula for you, so your first task is to find the formula yourself.
Input
For each scenario, you are given the number m, 3 <= m < 100, of movements of the robot in the first line. The following m lines contain pairs 揹x dy�of integers, separated by a single blank, satisfying .-100 <= dx, dy <= 100 and (dx, dy) != (0, 0). Such a pair means that the robot moves on to a grid point dx units to the right and dy units upwards on the grid (with respect to the current position). You can assume that the curve along which the robot moves is closed and that it does not intersect or even touch itself except for the start and end points. The robot moves anti-clockwise around the building, so the area to be calculated lies to the left of the curve. It is known in advance that the whole polygon would fit into a square on the grid with a side length of 100 units.
Output
Sample Input
- 2
- 4
- 1 0
- 0 1
- -1 0
- 0 -1
- 7
- 5 0
- 1 3
- -2 2
- -1 0
- 0 -3
- -3 1
- 0 -3
Sample Output
- Scenario #1:
- 0 4 1.0
- Scenario #2:
- 12 16 19.0
Source
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Pick定理的证明推荐个网址:episte.math.ntu.edu.tw/articles/sm/sm_25_10_1/page2.html
Area=i + b/2 - 1
i为内点 b为边上点
还有点在多边形边上运用GCD的证明,确实不会,希望大神看到给我解释一下
超时代码:
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- #include <iostream>
- #include <algorithm>
- #define eps 1e-8
- #define MAXX 210
- using namespace std;
- typedef struct point
- {
- double x;
- double y;
- }point;
- typedef struct line
- {
- point st;
- point ed;
- }line;
- bool dy(double x,double y)
- {
- return x>y+eps;
- }
- bool xy(double x,double y)
- {
- return x<y-eps;
- }
- bool dyd(double x,double y)
- {
- return x>y-eps;
- }
- bool xyd(double x,double y)
- {
- return x<y+eps;
- }
- bool dd(double x,double y)
- {
- return fabs(x-y)<eps;
- }
- double crossProduct(point a,point b,point c)//ac -> ab
- {
- return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
- }
- double dist(point a,point b)
- {
- return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
- }
- bool onSegment_1(point a,point b,point c)
- {
- double maxx=max(a.x,b.x);
- double minx=min(a.x,b.x);
- double maxy=max(a.y,b.y);
- double miny=min(a.y,b.y);
- if(dd(crossProduct(a,b,c),0.0) && xyd(c.x,maxx) && dyd(c.x,minx)
- && xyd(c.y,maxy) && dyd(c.y,miny))
- return true;
- return false;
- }
- bool segIntersect_1(point p1,point p2,point p3,point p4)
- {
- double d1=crossProduct(p3,p4,p1);
- double d2=crossProduct(p3,p4,p2);
- double d3=crossProduct(p1,p2,p3);
- double d4=crossProduct(p1,p2,p4);
- if(xy(d1*d2,0.0) && xy(d3*d4,0.0))
- return true;
- if(dd(d1,0.0) && onSegment_1(p3,p4,p1))
- return true;
- if(dd(d2,0.0) && onSegment_1(p3,p4,p2))
- return true;
- if(dd(d3,0.0) && onSegment_1(p1,p2,p3))
- return true;
- if(dd(d4,0.0) && onSegment_1(p1,p2,p4))
- return true;
- return false;
- }
- point p[MAXX];
- line li[MAXX];
- int n;
- bool inPolygon_1(point pot)
- {
- int count=;
- line l;
- l.st=pot;
- l.ed.x=1e10;
- l.ed.y=pot.y;
- p[n]=p[];
- for(int i=; i<n; i++)
- {
- if( onSegment_1(p[i],p[i+],pot ))
- return true;
- if(!dd(p[i].y,p[i+].y))
- {
- int tmp=-;
- if(onSegment_1(l.st,l.ed,p[i]))
- tmp=i;
- else if(onSegment_1(l.st,l.ed,p[i+]))
- tmp=i+;
- if(tmp != - && dd(p[tmp].y,max(p[i].y,p[i+].y)))
- count++;
- else if(tmp == - && segIntersect_1(p[i],p[i+],l.st,l.ed))
- count++;
- }
- }
- if(count % ==)
- return true;
- return false;
- }
- bool inPolygon_2(point pot)
- {
- int count=;
- line l;
- l.st=pot;
- l.ed.x=1e10;
- l.ed.y=pot.y;
- p[n]=p[];
- for(int i=; i<n; i++)
- {
- if( onSegment_1(p[i],p[i+],pot ))
- return false;
- if(!dd(p[i].y,p[i+].y))
- {
- int tmp=-;
- if(onSegment_1(l.st,l.ed,p[i]))
- tmp=i;
- else if(onSegment_1(l.st,l.ed,p[i+]))
- tmp=i+;
- if(tmp != - && dd(p[tmp].y,max(p[i].y,p[i+].y)))
- count++;
- else if(tmp == - && segIntersect_1(p[i],p[i+],l.st,l.ed))
- count++;
- }
- }
- if(count % ==)
- return true;
- return false;
- }
- double Area(int n)
- {
- if(n<)return ;
- int i;
- double ret=0.0;
- for(i=; i<=n; i++)
- {
- ret+=(crossProduct(p[],p[i-],p[i]));
- }
- return fabs(ret)/2.0;
- }
- int main()
- {
- int m,i,j;
- int ttmp=;
- scanf("%d",&m);
- while(m--)
- {
- scanf("%d",&n);
- p[].x=;p[].y=;
- double x,y;
- double maxx=-,maxy=-,minx=,miny=;
- for(i=; i<=n; i++)
- {
- scanf("%lf%lf",&x,&y);
- p[i].x=p[i-].x+x;
- p[i].y=p[i-].y+y;//printf("%lf %lf^^",p[i].x,p[i].y);
- }
- for(i=; i<=n; i++)
- {
- maxx=maxx>p[i].x?maxx:p[i].x;
- maxy=maxy>p[i].y?maxy:p[i].y;
- minx=minx<p[i].x?minx:p[i].x;
- miny=miny<p[i].y?miny:p[i].y;
- }//printf("%lf %lf^^%lf %lf**",minx,maxx,miny,maxy);
- int in=,edge=,sum=;
- point cas;
- for(i=minx; i<=maxx; i++)
- {
- for(j=miny; j<=maxy; j++)
- {
- cas.x=i;cas.y=j;
- if(inPolygon_1(cas))
- {
- sum++;
- }
- if(inPolygon_2(cas))
- {
- in++;
- }
- }
- }
- double area=Area(n);
- edge=sum-in;
- printf("Scenario #%d:\n",ttmp++);
- printf("%d %d %.1lf\n",in,edge,area);
- }
- return ;
- }
Pick定理运用
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- #include <iostream>
- #include <algorithm>
- #define eps 1e-8
- #define MAXX 210
- typedef struct point
- {
- double x;
- double y;
- }point;
- int gcd(int a,int b)
- {
- return b ? gcd(b,a%b) : a;
- }
- double crossProduct(point a,point b,point c)
- {
- return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
- }
- point p[MAXX];
- int onBorder(int n)
- {
- int sum=;
- for(int i=; i<n; i++)
- {
- sum+=gcd(abs((int)(p[i].x-p[i+].x)),abs((int)(p[i].y-p[i+].y)));
- }
- return sum;
- }
- double area(int n)
- {
- double ans=0.0;
- for(int i=; i<=n; i++)
- {
- ans+=crossProduct(p[],p[i-],p[i]);
- }
- return fabs(ans)/2.0;
- }
- int main()
- {
- int n,m,i,j;
- scanf("%d",&n);
- int cas=;
- while(n--)
- {
- scanf("%d",&m);
- p[].x=;p[].y=;
- double x,y;
- for(i=; i<=m; i++)
- {
- scanf("%lf%lf",&x,&y);
- p[i].x=p[i-].x+x;
- p[i].y=p[i-].y+y;
- }
- double are=area(m);
- int edge=onBorder(m);
- printf("Scenario #%d:\n",cas++);
- printf("%d %d %.1lf\n\n",(int)are+-edge/,edge,are);
- }
- return ;
- }
poj 1265 Area (Pick定理+求面积)的更多相关文章
- POJ 1265 Area (Pick定理 & 多边形面积)
题目链接:POJ 1265 Problem Description Being well known for its highly innovative products, Merck would d ...
- poj 1265 Area(pick定理)
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4373 Accepted: 1983 Description Bein ...
- [poj 1265]Area[Pick定理][三角剖分]
题意: 给出机器人移动的向量, 计算包围区域的内部整点, 边上整点, 面积. 思路: 面积是用三角剖分, 边上整点与GCD有关, 内部整点套用Pick定理. S = I + E / 2 - 1 I 为 ...
- Area - POJ 1265(pick定理求格点数+求多边形面积)
题目大意:以原点为起点然后每次增加一个x,y的值,求出来最后在多边形边上的点有多少个,内部的点有多少个,多边形的面积是多少. 分析: 1.以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其 ...
- poj 1265 Area( pick 定理 )
题目:http://poj.org/problem?id=1265 题意:已知机器人行走步数及每一步的坐标 变化量 ,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:1.以 ...
- POJ1265——Area(Pick定理+多边形面积)
Area DescriptionBeing well known for its highly innovative products, Merck would definitely be a goo ...
- poj 1654 Area (多边形求面积)
链接:http://poj.org/problem?id=1654 Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- POJ 1265 Area (pick定理)
题目大意:已知机器人行走步数及每一步的坐标变化量,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:叉积求面积,pick定理求点. pick定理:面积=内部点数+边上点数/2-1 ...
- poj 1265 Area 面积+多边形内点数
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5861 Accepted: 2612 Description ...
随机推荐
- Delphi 的各版本定义,用于预编译参数中,避免忘记备忘之
DELPHI的版本宏: VER80 - Delphi 1 VER90 - Delphi 2 VER100 - Delphi 3 VER120 - Delphi 4 VER130 - Delphi 5 ...
- android 中 listview 设置自动匹配高度
1.布局文件 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:l ...
- game of life
class Solution { public: void gameOfLife(vector<vector<int>>& board) { queue<int& ...
- new和malloc的区别
1.malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符 2.new出来的指针是直接带类型信息的,而malloc返回的都是void*指针. 3.new 建立的是一个 ...
- struts2多线程数据乱窜问题
转自:http://love398146779.iteye.com/blog/1781680 1.struts2为每个线程提供一个action实例,多线程访问时不会出现问题.当使用spring管理st ...
- 如何在VS2013中新建WindowsService定时任务
http://jingyan.baidu.com/article/cd4c2979e9330d756f6e6070.html 很多人都想做定时任务,但是没有不知道如何下手,现在就用WindowsSer ...
- 解析八大O2O典范:他们都做了什么?
随着无线技术的发展二维码的发展以及智能手机的普及,零售的解决方案不仅在在一台电脑上解决,可以从线上到线下,为消费者贯通线上线下的购物体验.人人都爱O2O,可做得好的O2O案例却并不多.要解决利益分配. ...
- Webstrom快捷键大全
20:32:59 Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者 ) Shift+F6 重构-重命名 Ctrl+X ...
- poj2269 Friends
计算表达式. 只有3种运算符:*,+,- , *优先级高于后两者,后两者优先级相同. 有两种符号:{},(). 利用递归和堆栈即可解决. 首先遇到左括号开始入栈直到遇到右括号,遇到右括号时对括号内的数 ...
- hdu1016 Prime Ring Problem
dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...