Poj--3788(判点在多边形内)
2014-12-07 01:10:39
思路:由于这题保证多边形凸,呼呼,偷懒下用三角面积和等于多边形来判断...具体做法?!,枚举点即可 hhhh
1 /************************************************************************* 2 > File Name: 3788.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sat 06 Dec 2014 01:18:19 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 const double eps = 1e-6; 27 28 int T,tag,n,xmin,xmax,ymin,ymax; 29 double d[60],starea; 30 int ans[100000][3],cnt; 31 32 struct point{ 33 int x,y; 34 }p[60]; 35 36 double Cal_dis(point a,point b){ 37 return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); 38 } 39 40 double Cal_area(point st,point a,point b){ 41 int x1 = a.x - st.x; 42 int y1 = a.y - st.y; 43 int x2 = b.x - st.x; 44 int y2 = b.y - st.y; 45 return fabs(1.0 * x1 * y2 - x2 * y1) * 0.5; 46 } 47 48 bool Judge(int x,int y){ 49 double area = 0.0; 50 point t; 51 t.x = x; 52 t.y = y; 53 for(int i = 1; i <= n; ++i){ 54 double tmp; 55 if(i == n) tmp = Cal_area(t,p[n],p[1]); 56 else tmp = Cal_area(t,p[i],p[i + 1]); 57 if(tmp < eps) 58 return false; 59 area += tmp; 60 } 61 if(fabs(area - starea) < eps) 62 return true; 63 return false; 64 } 65 66 int main(){ 67 scanf("%d",&T); 68 while(T--){ 69 cnt = 0; 70 starea = 0.0; 71 xmax = ymax = -INF; 72 xmin = ymin = INF; 73 scanf("%d%d",&tag,&n); 74 for(int i = 1; i <= n; ++i){ 75 scanf("%d%d",&p[i].x,&p[i].y); 76 xmax = max(xmax,p[i].x); 77 xmin = min(xmin,p[i].x); 78 ymin = min(ymin,p[i].y); 79 } 80 ymax = p[1].y; 81 for(int i = 2; i < n; ++i){ 82 starea += Cal_area(p[1],p[i],p[i + 1]); 83 } 84 for(int j = ymax; j >= ymin; --j){ 85 int i,l = -1,r = -1; 86 for(i = xmin; i <= xmax; ++i){ 87 if(Judge(i,j)){ 88 if(l == -1) l = i; 89 r = i; 90 } 91 } 92 if(l != -1){ 93 ans[++cnt][0] = j; 94 ans[cnt][1] = l; 95 ans[cnt][2] = r; 96 } 97 } 98 printf("%d %d\n",tag,cnt); 99 for(int i = 1; i <= cnt; ++i){ 100 printf("%d %d %d\n",ans[i][0],ans[i][1],ans[i][2]); 101 } 102 } 103 return 0; 104 }