[ZOJ3234 Protect the King]

[关键字]:计算几何 数学

[题目大意]:给出N个点,求最多可以形成多少个同心多边形(就是圈套圈)。

//==========================================================================================

[分析]:因为要想构成最多的同心多边形那每个多边形必定当前可用的所有点中的凸包。然后就求凸包->删边->求凸包->……

[代码]:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN=2010;

struct node
{
int x,y,num;
}p[MAXN],c[MAXN];;
int N,M,ans;
bool b[MAXN];

long long det(node a,node b,node c)
{
return (long long)(b.x-a.x)*(c.y-a.y)-(long long)(c.x-a.x)*(b.y-a.y);
}

void Graham_Scan(node *c,int &M0,int base,node q)
{
while ((M0>base) && (det(c[M0-2],c[M0-1],q)<=0)) M0--;
c[M0]=q;
M0++;
}

bool cmp(node a,node b)
{
if (a.y<b.y || (a.y==b.y && a.x<b.x)) return 1; else return 0;
}

bool Graham(node *p,int N0,int &M0)
{
memset(c,0,sizeof(c));
for (int i=0;i<N0;i++)
if (!b[p[i].num]) Graham_Scan(c,M0,1,p[i]);
/*for (int i=0;i<M0;i++)
printf("%d %d\n",c[i].x,c[i].y);
*/
int M1=M0;
for (int i=N0-1;i>=0;i--)
if (!b[p[i].num]) Graham_Scan(c,M0,M1,p[i]);
for (int i=0;i<M0;i++)
b[c[i].num]=1;
M0--;
if (M0>2) return 1; else return 0;
}

int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while (scanf("%d",&N)!=EOF)
{
for (int i=0;i<N;i++)
scanf("%d%d",&p[i].x,&p[i].y),p[i].num=i;
sort(p,p+N,cmp);
/*for (int i=0;i<N;i++)
printf("%d %d %d\n",p[i].x,p[i].y,p[i].num);
*/
M=ans=0;
memset(b,0,sizeof(b));
while (Graham(p,N,M))
{
/*for (int i=0;i<M;i++)
printf("%d %d\n",c[i].x,c[i].y);
*/
M=0,ans++;
}
printf("%d\n",ans);
}
return 0;
}



你可能感兴趣的:(ZOJ)