您的位置:首页 > 其它

hdu 5128 The E-pang Palace(计算几何,暴力枚举)

2017-04-27 20:39 507 查看

The E-pang Palace

Problem Description

E-pang Palace was built in Qin dynasty by Emperor Qin Shihuang in Xianyang, Shanxi Province. It was the largest palace ever built by human. It was so large and so magnificent that after many years of construction, it still was not completed. Building the great wall, E-pang Palace and Qin Shihuang’s tomb cost so much labor and human lives that people rose to fight against Qin Shihuang’s regime.

Xiang Yu and Liu Bang were two rebel leaders at that time. Liu Bang captured Xianyang – the capital of Qin. Xiang Yu was very angry about this, and he commanded his army to march to Xianyang. Xiang Yu was the bravest and the strongest warrior at that time, and his army was much more than Liu Bang’s. So Liu Bang was frighten and retreated from Xianyang, leaving all treasures in the grand E-pang Palace untouched. When Xiang Yu took Xianyang, he burned E-pang Palce. The fire lasted for more than three months, renouncing the end of Qin dynasty.

Several years later, Liu Bang defeated Xiangyu and became the first emperor of Han dynasty. He went back to E-pang Palace but saw only some pillars left. Zhang Liang and Xiao He were Liu Bang’s two most important ministers, so Liu Bang wanted to give them some awards. Liu Bang told them: “You guys can make two rectangular fences in E-pang Palace, then the land inside the fences will belongs to you. But the corners of the rectangles must be the pillars left on the ground, and two fences can’t cross or touch each other.”

To simplify the problem, E-pang Palace can be consider as a plane, and pillars can be considered as points on the plane. The fences you make are rectangles, and you MUST make two rectangles. Please note that the rectangles you make must be parallel to the coordinate axes.

The figures below shows 3 situations which are not qualified(Thick dots stands for pillars):



Zhang Liang and Xiao He wanted the total area of their land in E-pang Palace to be maximum. Please bring your computer and go back to Han dynasty to help them so that y
4000
ou may change the history.

Input

There are no more than 15 test case.

For each test case:

The first line is an integer N, meaning that there are N pillars left in E-pang Palace(4 <=N <= 30).

Then N lines follow. Each line contains two integers x and y (0 <= x,y <= 200), indicating a pillar’s coordinate. No two pillars has the same coordinate.

The input ends by N = 0.

Output

For each test case, print the maximum total area of land Zhang Liang and Xiao He could get. If it was impossible for them to build two qualified fences, print “imp”.

Sample Input

8

0 0

1 0

0 1

1 1

0 2

1 2

0 3

1 3

8

0 0

2 0

0 2

2 2

1 2

3 2

1 3

3 3

0

Sample Output

2

imp

分析:因为是一个矩形嘛,所以可以通过他们的左下和右上顶点记录来这个矩形,最后直接枚举所有矩形即可

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 202
struct point
{
int x,y;
} q[maxn];

struct rectangular
{
point p1,p2;
int area;
} p[maxn*2];
bool mp[maxn][maxn];
int n;

bool cmp(point a,point b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}

int judge(rectangular a,rectangular b)
{
if(a.p2.x<b.p1.x||a.p2.y<b.p1.y)
return 1;
if(b.p2.x<a.p1.x||b.p2.y<a.p1.y)
return 1;
if(a.p1.x<b.p1.x&&a.p1.y<b.p1.y&&a.p2.x>b.p2.x&&a.p2.y>b.p2.y)//判断嵌套
return 2;
if(b.p1.x<a.p1.x&&b.p1.y<a.p1.y&&b.p2.x>a.p2.x&&b.p2.y>a.p2.y)//判断嵌套
return 3;
return 0;
}

int main()
{
while(~scanf("%d",&n),n)
{
memset(mp,false,sizeof(mp));
for(int i=0; i<n; ++i)
{
scanf("%d%d",&q[i].x,&q[i].y);
mp[q[i].x][q[i].y]=true;
}
sort(q,q+n,cmp);
int cnt=0;
for(int i=0; i<n; ++i)
for(int j=i+1; j<n; ++j)
{
if(q[j].x>q[i].x&&q[j].y>q[i].y)
if(mp[q[i].x][q[j].y]&&mp[q[j].x][q[i].y])//存储矩形
{
p[cnt].p1.x=q[i].x,p[cnt].p1.y=q[i].y;
p[cnt].p2.x=q[j].x,p[cnt].p2.y=q[j].y;
p[cnt++].area=(q[j].x-q[i].x)*(q[j].y-q[i].y);
}
}
int ans=0;
for(int i=0; i<cnt; ++i)
for(int j=i+1; j<cnt; ++j)//枚举所有矩形
{
if(judge(p[i],p[j])==1)
ans=max(ans,p[i].area+p[j].area);
else if(judge(p[i],p[j])==2)
ans=max(ans,p[i].area);
else if(judge(p[i],p[j])==3)
ans=max(ans,p[j].area);
}
if(ans)
printf("%d\n",ans);
else
printf("imp\n");
}
return 0;
}


总结:我是先枚举所有的点判断其是否能形成两个矩形,然后再判断这两个矩形是否符合条件,结果TLE了。。。

而且写的时候代码超长,bug巨多。。。

所以枚举之前应该先分析一下题目,看怎样枚举才能使得时间最少,这样会比一上去就开始盲目地写速度快很多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: