链接:http://poj.org/problem?id=1265

Area
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4969   Accepted: 2231

Description

Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new research and development facility the company has installed the latest system of surveillance robots patrolling the area. These robots move along the walls of the facility and report suspicious observations to the central security office. The only flaw in the system a competitor抯 agent could find is the fact that the robots radio their movements unencrypted. Not being able to find out more, the agent wants to use that information to calculate the exact size of the area occupied by the new facility. It is public knowledge that all the corners of the building are situated on a rectangular grid and that only straight walls are used. Figure 1 shows the course of a robot around an example area.

 
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

The first line contains the number of scenarios. 
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

The output for every scenario begins with a line containing 揝cenario #i:� where i is the number of the scenario starting at 1. Then print a single line containing I, E, and A, the area A rounded to one digit after the decimal point. Separate the three numbers by two single blanks. Terminate the output for the scenario with a blank line.

Sample Input

  1. 2
  2. 4
  3. 1 0
  4. 0 1
  5. -1 0
  6. 0 -1
  7. 7
  8. 5 0
  9. 1 3
  10. -2 2
  11. -1 0
  12. 0 -3
  13. -3 1
  14. 0 -3

Sample Output

  1. Scenario #1:
  2. 0 4 1.0
  3.  
  4. Scenario #2:
  5. 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的证明,确实不会,希望大神看到给我解释一下

超时代码:

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <algorithm>
  7.  
  8. #define eps 1e-8
  9. #define MAXX 210
  10. using namespace std;
  11.  
  12. typedef struct point
  13. {
  14. double x;
  15. double y;
  16. }point;
  17. typedef struct line
  18. {
  19. point st;
  20. point ed;
  21. }line;
  22.  
  23. bool dy(double x,double y)
  24. {
  25. return x>y+eps;
  26. }
  27. bool xy(double x,double y)
  28. {
  29. return x<y-eps;
  30. }
  31. bool dyd(double x,double y)
  32. {
  33. return x>y-eps;
  34. }
  35. bool xyd(double x,double y)
  36. {
  37. return x<y+eps;
  38. }
  39. bool dd(double x,double y)
  40. {
  41. return fabs(x-y)<eps;
  42. }
  43.  
  44. double crossProduct(point a,point b,point c)//ac -> ab
  45. {
  46. return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
  47. }
  48. double dist(point a,point b)
  49. {
  50. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
  51. }
  52.  
  53. bool onSegment_1(point a,point b,point c)
  54. {
  55. double maxx=max(a.x,b.x);
  56. double minx=min(a.x,b.x);
  57. double maxy=max(a.y,b.y);
  58. double miny=min(a.y,b.y);
  59.  
  60. if(dd(crossProduct(a,b,c),0.0) && xyd(c.x,maxx) && dyd(c.x,minx)
  61. && xyd(c.y,maxy) && dyd(c.y,miny))
  62. return true;
  63. return false;
  64. }
  65.  
  66. bool segIntersect_1(point p1,point p2,point p3,point p4)
  67. {
  68. double d1=crossProduct(p3,p4,p1);
  69. double d2=crossProduct(p3,p4,p2);
  70. double d3=crossProduct(p1,p2,p3);
  71. double d4=crossProduct(p1,p2,p4);
  72.  
  73. if(xy(d1*d2,0.0) && xy(d3*d4,0.0))
  74. return true;
  75. if(dd(d1,0.0) && onSegment_1(p3,p4,p1))
  76. return true;
  77. if(dd(d2,0.0) && onSegment_1(p3,p4,p2))
  78. return true;
  79. if(dd(d3,0.0) && onSegment_1(p1,p2,p3))
  80. return true;
  81. if(dd(d4,0.0) && onSegment_1(p1,p2,p4))
  82. return true;
  83. return false;
  84. }
  85.  
  86. point p[MAXX];
  87. line li[MAXX];
  88. int n;
  89.  
  90. bool inPolygon_1(point pot)
  91. {
  92. int count=;
  93. line l;
  94. l.st=pot;
  95. l.ed.x=1e10;
  96. l.ed.y=pot.y;
  97. p[n]=p[];
  98. for(int i=; i<n; i++)
  99. {
  100. if( onSegment_1(p[i],p[i+],pot ))
  101. return true;
  102. if(!dd(p[i].y,p[i+].y))
  103. {
  104. int tmp=-;
  105. if(onSegment_1(l.st,l.ed,p[i]))
  106. tmp=i;
  107. else if(onSegment_1(l.st,l.ed,p[i+]))
  108. tmp=i+;
  109. if(tmp != - && dd(p[tmp].y,max(p[i].y,p[i+].y)))
  110. count++;
  111. else if(tmp == - && segIntersect_1(p[i],p[i+],l.st,l.ed))
  112. count++;
  113. }
  114. }
  115. if(count % ==)
  116. return true;
  117. return false;
  118. }
  119.  
  120. bool inPolygon_2(point pot)
  121. {
  122. int count=;
  123. line l;
  124. l.st=pot;
  125. l.ed.x=1e10;
  126. l.ed.y=pot.y;
  127. p[n]=p[];
  128. for(int i=; i<n; i++)
  129. {
  130. if( onSegment_1(p[i],p[i+],pot ))
  131. return false;
  132. if(!dd(p[i].y,p[i+].y))
  133. {
  134. int tmp=-;
  135. if(onSegment_1(l.st,l.ed,p[i]))
  136. tmp=i;
  137. else if(onSegment_1(l.st,l.ed,p[i+]))
  138. tmp=i+;
  139. if(tmp != - && dd(p[tmp].y,max(p[i].y,p[i+].y)))
  140. count++;
  141. else if(tmp == - && segIntersect_1(p[i],p[i+],l.st,l.ed))
  142. count++;
  143. }
  144. }
  145. if(count % ==)
  146. return true;
  147. return false;
  148. }
  149.  
  150. double Area(int n)
  151. {
  152. if(n<)return ;
  153. int i;
  154. double ret=0.0;
  155. for(i=; i<=n; i++)
  156. {
  157. ret+=(crossProduct(p[],p[i-],p[i]));
  158. }
  159. return fabs(ret)/2.0;
  160. }
  161.  
  162. int main()
  163. {
  164. int m,i,j;
  165. int ttmp=;
  166. scanf("%d",&m);
  167. while(m--)
  168. {
  169. scanf("%d",&n);
  170. p[].x=;p[].y=;
  171. double x,y;
  172. double maxx=-,maxy=-,minx=,miny=;
  173. for(i=; i<=n; i++)
  174. {
  175. scanf("%lf%lf",&x,&y);
  176. p[i].x=p[i-].x+x;
  177. p[i].y=p[i-].y+y;//printf("%lf %lf^^",p[i].x,p[i].y);
  178. }
  179. for(i=; i<=n; i++)
  180. {
  181. maxx=maxx>p[i].x?maxx:p[i].x;
  182. maxy=maxy>p[i].y?maxy:p[i].y;
  183. minx=minx<p[i].x?minx:p[i].x;
  184. miny=miny<p[i].y?miny:p[i].y;
  185. }//printf("%lf %lf^^%lf %lf**",minx,maxx,miny,maxy);
  186. int in=,edge=,sum=;
  187. point cas;
  188. for(i=minx; i<=maxx; i++)
  189. {
  190. for(j=miny; j<=maxy; j++)
  191. {
  192. cas.x=i;cas.y=j;
  193. if(inPolygon_1(cas))
  194. {
  195. sum++;
  196. }
  197. if(inPolygon_2(cas))
  198. {
  199. in++;
  200. }
  201. }
  202. }
  203. double area=Area(n);
  204. edge=sum-in;
  205. printf("Scenario #%d:\n",ttmp++);
  206. printf("%d %d %.1lf\n",in,edge,area);
  207. }
  208. return ;
  209. }

Pick定理运用

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <iostream>
  6. #include <algorithm>
  7.  
  8. #define eps 1e-8
  9. #define MAXX 210
  10.  
  11. typedef struct point
  12. {
  13. double x;
  14. double y;
  15. }point;
  16.  
  17. int gcd(int a,int b)
  18. {
  19. return b ? gcd(b,a%b) : a;
  20. }
  21.  
  22. double crossProduct(point a,point b,point c)
  23. {
  24. return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
  25. }
  26.  
  27. point p[MAXX];
  28. int onBorder(int n)
  29. {
  30. int sum=;
  31. for(int i=; i<n; i++)
  32. {
  33. sum+=gcd(abs((int)(p[i].x-p[i+].x)),abs((int)(p[i].y-p[i+].y)));
  34. }
  35. return sum;
  36. }
  37.  
  38. double area(int n)
  39. {
  40. double ans=0.0;
  41. for(int i=; i<=n; i++)
  42. {
  43. ans+=crossProduct(p[],p[i-],p[i]);
  44. }
  45. return fabs(ans)/2.0;
  46. }
  47.  
  48. int main()
  49. {
  50. int n,m,i,j;
  51. scanf("%d",&n);
  52. int cas=;
  53. while(n--)
  54. {
  55. scanf("%d",&m);
  56. p[].x=;p[].y=;
  57. double x,y;
  58. for(i=; i<=m; i++)
  59. {
  60. scanf("%lf%lf",&x,&y);
  61. p[i].x=p[i-].x+x;
  62. p[i].y=p[i-].y+y;
  63. }
  64. double are=area(m);
  65. int edge=onBorder(m);
  66. printf("Scenario #%d:\n",cas++);
  67. printf("%d %d %.1lf\n\n",(int)are+-edge/,edge,are);
  68. }
  69. return ;
  70. }

poj 1265 Area (Pick定理+求面积)的更多相关文章

  1. POJ 1265 Area (Pick定理 & 多边形面积)

    题目链接:POJ 1265 Problem Description Being well known for its highly innovative products, Merck would d ...

  2. poj 1265 Area(pick定理)

    Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4373 Accepted: 1983 Description Bein ...

  3. [poj 1265]Area[Pick定理][三角剖分]

    题意: 给出机器人移动的向量, 计算包围区域的内部整点, 边上整点, 面积. 思路: 面积是用三角剖分, 边上整点与GCD有关, 内部整点套用Pick定理. S = I + E / 2 - 1 I 为 ...

  4. Area - POJ 1265(pick定理求格点数+求多边形面积)

    题目大意:以原点为起点然后每次增加一个x,y的值,求出来最后在多边形边上的点有多少个,内部的点有多少个,多边形的面积是多少. 分析: 1.以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其 ...

  5. poj 1265 Area( pick 定理 )

    题目:http://poj.org/problem?id=1265 题意:已知机器人行走步数及每一步的坐标   变化量 ,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:1.以 ...

  6. POJ1265——Area(Pick定理+多边形面积)

    Area DescriptionBeing well known for its highly innovative products, Merck would definitely be a goo ...

  7. poj 1654 Area (多边形求面积)

    链接:http://poj.org/problem?id=1654 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  8. POJ 1265 Area (pick定理)

    题目大意:已知机器人行走步数及每一步的坐标变化量,求机器人所走路径围成的多边形的面积.多边形边上和内部的点的数量. 思路:叉积求面积,pick定理求点. pick定理:面积=内部点数+边上点数/2-1 ...

  9. poj 1265 Area 面积+多边形内点数

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5861   Accepted: 2612 Description ...

随机推荐

  1. Delphi 的各版本定义,用于预编译参数中,避免忘记备忘之

    DELPHI的版本宏: VER80 - Delphi 1 VER90 - Delphi 2 VER100 - Delphi 3 VER120 - Delphi 4 VER130 - Delphi 5 ...

  2. android 中 listview 设置自动匹配高度

    1.布局文件 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:l ...

  3. game of life

    class Solution { public: void gameOfLife(vector<vector<int>>& board) { queue<int& ...

  4. new和malloc的区别

    1.malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符 2.new出来的指针是直接带类型信息的,而malloc返回的都是void*指针. 3.new 建立的是一个 ...

  5. struts2多线程数据乱窜问题

    转自:http://love398146779.iteye.com/blog/1781680 1.struts2为每个线程提供一个action实例,多线程访问时不会出现问题.当使用spring管理st ...

  6. 如何在VS2013中新建WindowsService定时任务

    http://jingyan.baidu.com/article/cd4c2979e9330d756f6e6070.html 很多人都想做定时任务,但是没有不知道如何下手,现在就用WindowsSer ...

  7. 解析八大O2O典范:他们都做了什么?

    随着无线技术的发展二维码的发展以及智能手机的普及,零售的解决方案不仅在在一台电脑上解决,可以从线上到线下,为消费者贯通线上线下的购物体验.人人都爱O2O,可做得好的O2O案例却并不多.要解决利益分配. ...

  8. Webstrom快捷键大全

    20:32:59 Ctrl+/ 或 Ctrl+Shift+/          注释(// 或者 ) Shift+F6               重构-重命名 Ctrl+X             ...

  9. poj2269 Friends

    计算表达式. 只有3种运算符:*,+,- , *优先级高于后两者,后两者优先级相同. 有两种符号:{},(). 利用递归和堆栈即可解决. 首先遇到左括号开始入栈直到遇到右括号,遇到右括号时对括号内的数 ...

  10. hdu1016 Prime Ring Problem

    dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...