HDU5128 The E-pang Palace(计算几何,枚举)

来源:互联网 发布:忻州网络第一传媒 编辑:程序博客网 时间:2024/05/18 01:04

题目:

The E-pang Palace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 3468    Accepted Submission(s): 2049


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 you 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
80 01 00 11 10 21 20 31 380 02 00 22 21 23 21 33 30
 

Sample Output
2imp
 

Source
2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大)
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6022 6021 6020 6019 6018 
 

Statistic | Submit | Discuss | Note
思路:

给了n个点,然后你在其中找出两个最大的矩形,算出面积,但是选出的矩形要满足一些条件,不能接触和重叠

在这里我们枚举它的对角线,通过这种方式来枚举矩形。

套用一下题解:

由于n只有4到30这么大,所以直接暴力求解就可以了。
一、首先我们要判断存不存在矩形,如何判断?
       这里有个小技巧,通过确定两个点的对角线来判断能不能构成矩形。
如图:


        我们先判断A,B这两点所连的直线是不是水平或者竖直,如果是就跳过。如果是则判断C,D这两个点是不是都存在。如果存在则能构成矩形,如果不能,就不能构成。
然后我们在确定第二个矩形。
二、第二个矩形的确定。
       第二个矩形(假设是EFGH)的确定首先要排除点A,B,C,D这四个点,并且EFGH这四个点不能有任何一个在ABCD内(包括边与定点)。(如题图)
       但是这里有一种特殊情况。如图:

    我们确定了ABCD,但是EFGH包含了ABCD,所以我们要反过来判断ABCD是不是在EFGH内。如果在就取EFGH的面积,如果不在就取ABCD+AEFHG的面积

代码:

#include <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 10000007#define debug() puts("what the fuck!!!")#define N 50#define ll long longusing namespace std;struct point//坐标点{    int x,y;} g[N];struct ju//矩形{    point a,b;//矩形a一定在矩形b的右下方    int area;} zz[N*N];int map[300][300];bool cmp(point a,point b){    if(a.x==b.x)        return a.y<b.y;    return a.x<b.x;}int judge(int i,int j){    //确定是都相离    if(zz[i].a.x>zz[j].b.x) return 1;    if(zz[i].a.y>zz[j].b.y) return 1;    if(zz[j].a.x>zz[i].b.x) return 1;    if(zz[j].a.y>zz[i].b.y) return 1;    //是否有i包含j    if(zz[i].a.x<zz[j].a.x&&zz[i].a.y<zz[j].a.y&&zz[i].b.x>zz[j].b.x&&zz[i].b.y>zz[j].b.y)        return 2;    else        return 0;}int main(){    int n;    while(scanf("%d",&n)&&n)    {        mem(map,0);        for(int i=0; i<n; i++)        {            scanf("%d%d",&g[i].x,&g[i].y);            map[g[i].x][g[i].y]=1;        }        sort(g,g+n,cmp);        int len=0;        for(int i=0; i<n; i++)            for(int j=i+1; j<n; j++)            {                if(g[j].x>g[i].x&&g[j].y>g[i].y)//判断两个点能不能构成矩形                {                    if(map[g[i].x][g[j].y]&&map[g[j].x][g[i].y])//判断这两个点的组合是否存在                    {                        zz[len].a.x=g[i].x;                        zz[len].a.y=g[i].y;                        zz[len].b.x=g[j].x;                        zz[len].b.y=g[j].y;                        zz[len++].area=(g[j].x-g[i].x)*(g[j].y-g[i].y);                    }                }            }        int maxx=0;        for(int i=0; i<len; i++)            for(int j=i+1; j<len; j++)            {                if(judge(i,j)==1)                    maxx=max(maxx,zz[i].area+zz[j].area);                else if(judge(i,j)==2)                    maxx=max(maxx,zz[i].area);            }        if(maxx)            printf("%d\n",maxx);        else            puts("imp");    }    return 0;}



0 0
原创粉丝点击