本文主要是介绍uva218( Moth Eradication),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
求凸包,然后顺时针输出。
218 - Moth Eradication
Time limit: 3.000 seconds Moth Eradication
Moth Eradication |
Entomologists in the Northeast have set out traps to determine the influx of Jolliet moths into the area. They plan to study eradication programs that have some potential to control the spread of the moth population.
The study calls for organizing the traps in which moths have been caught into compact regions, which will then be used to test each eradication program. A region is defined as the polygon with the minimum length perimeter that can enclose all traps within that region. For example, the traps (represented by dots) of a particular region and its associated polygon are illustrated below.

You must write a program that can take as input the locations of traps in a region and output the locations of traps that lie on the perimeter of the region as well as the length of the perimeter.
Input
The input file will contain records of data for several regions. The first line of each record contains the number (an integer) of traps for that region. Subsequent lines of the record contain 2 real numbers that are the x- and y-coordinates of the trap locations. Data within a single record will not be duplicated. End of input is indicated by a region with 0 traps.
Output
Output for a single region is displayed on at least 3 lines:
One blank line must separate output from consecutive input records.
Sample Input
3 1 2 4 10 5 12.3 6 0 0 1 1 3.1 1.3 3 4.5 6 2.1 2 -3.2 7 1 0.5 5 0 4 1.5 3 -0.2 2.5 -1.5 0 0 2 2 0
Sample Output
Region #1: (1.0,2.0)-(4.0,10.0)-(5.0,12.3)-(1.0,2.0) Perimeter length = 22.10Region #2: (0.0,0.0)-(3.0,4.5)-(6.0,2.1)-(2.0,-3.2)-(0.0,0.0) Perimeter length = 19.66Region #3: (0.0,0.0)-(2.0,2.0)-(4.0,1.5)-(5.0,0.0)-(2.5,-1.5)-(0.0,0.0) Perimeter length = 12.52
粘一下小媛的正确代码:http://blog.csdn.net/zxy_snow/article/details/6535122
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 120000;
const double eps = 1e-6;
bool dy(double x,double y) { return x > y + eps;} // x > y
bool xy(double x,double y) { return x < y - eps;} // x < y
bool dyd(double x,double y) { return x > y - eps;} // x >= y
bool xyd(double x,double y) { return x < y + eps;} // x <= y
bool dd(double x,double y) { return fabs( x - y ) < eps;} // x == y
struct point{ double x,y; };
point c[MAX];
double disp2p(point a,point b)
{return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向
{return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool cmp(point a,point b) // 排序
{ double len = crossProduct(c[0],a,b); if( dd(len,0.0) ) return xy(disp2p(c[0],a),disp2p(c[0],b)); return xy(len,0.0);
}
int stk[MAX];
int top;
void Graham(int n)
{int tmp = 0; for(int i=1; i<n; i++)if( xy(c[i].x,c[tmp].x) || dd(c[i].x,c[tmp].x) && xy(c[i].y,c[tmp].y) )tmp = i;swap(c[0],c[tmp]);sort(c+1,c+n,cmp);stk[0] = 0; stk[1] = 1;top = 1;for(int i=2; i<n; i++){while( xyd( crossProduct(c[stk[top]],c[stk[top-1]],c[i]), 0.0 ) && top >= 1 )top--;stk[++top] = i;}double sum = 0.0;for(int i=0; i<=top; i++)sum += disp2p(c[stk[i]],c[stk[(i+1)%(top+1)]]);printf("(%.1lf,%.1lf)-",c[stk[0]].x,c[stk[0]].y);for(int i=top; i>0; i--)printf("(%.1lf,%.1lf)-",c[stk[i]].x,c[stk[i]].y);printf("(%.1lf,%.1lf)\n",c[stk[0]].x,c[stk[0]].y);printf("Perimeter length = %.2lf\n",sum);
}
int main()
{int n;int ncases = 1;while( ~scanf("%d",&n) && n ){for(int i=0; i<n; i++)scanf("%lf%lf",&c[i].x,&c[i].y);printf("Region #%d:\n",ncases++);Graham(n);}
return 0;
}
我的代码一直WA,没找到那里出问题了。。。先放着,过两天看。
//#include <iostream>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
#define eps 1e-6
//using namespace std; struct point
{ double x, y;
}pp;
point p[100005];
int stack[100005];
int top;
double dis(point a, point b)
{ return ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double multi(point b, point c, point a)
{ return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
void swap(point p[], int s, int t)
{ point tmp; tmp = p[s]; p[s] = p[t]; p[t] = tmp;
}
int cmp(const void *a, const void *b)
{ point *c = (point *)a; point *d = (point *)b; double k = multi(*c, *d, pp); if(k < 0) return 1; else if(k == 0 && dis(*c, pp) >= dis(*d, pp)) return 1; else return -1;
}
void Graham(point p[], int n, int stack[], int &top)
{ int i, u; u = 0; for(i = 1;i < n;i++){ if(p[i].x == p[u].x && p[i].y < p[u].y) u = i; else if(p[i].x < p[u].x) u = i; } swap(p, 0, u); pp = p[0]; qsort(p + 1, n - 1, sizeof(p[0]), cmp); stack[0] = 0; stack[1] = 1; top = 1; for(i = 2;i < n;i++){ while(multi(p[i], p[stack[top]], p[stack[top - 1]]) > -eps){ if(top == 0) break; top--; } top++; stack[top] = i; } /*for(int i=0;i<=top;i++)cout<< p[stack[i]].x<<" "<<p[stack[i]].y<<endl;*/
}
int main()
{ int t, i, j, n; double perim; t=1;while(~scanf("%d",&n)&&n){for(j = 0;j < n;j++)scanf("%lf%lf",&p[j].x,&p[j].y); Graham(p, n, stack, top); // cout<<top;/*cout<<endl;for(int i=0;i<=top;i++)printf("%.1lf %.1lf\n",p[stack[i]].x,p[stack[i]].y);*/perim=0.0;p[stack[top+1]]=p[stack[0]];for(j = 0;j <= top ;j++)perim +=sqrt(dis(p[stack[j]], p[stack[j + 1]])); printf("Region #%d:\n",t++);printf("(%.1lf,%.1lf)-",p[stack[0]].x,p[stack[0]].y);for(int i=top; i>0; i--)printf("(%.1lf,%.1lf)-",p[stack[i]].x,p[stack[i]].y);printf("(%.1lf,%.1lf)\n",p[stack[0]].x,p[stack[0]].y);printf("Perimeter length =%.2lf\n",perim ); printf("\n");}return 0;
}
这篇关于uva218( Moth Eradication)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!