UVALive 7070 The E-pang Palace(2014广州赛区现场赛)

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:
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

题意:给n个点的坐标,要在这n个点中建立两个矩阵,输出两个矩阵最大的面积,这两个矩阵的边和点不能有重复的(可以一个矩阵里面包含一个矩阵)。

矩阵的判断条件:知道两个点就可以求出另两个点坐标,如左下角坐标为(x1,y1),右上角坐标为(x2,y2),那么左上角坐标求出为(x1,y2),右下角坐标求出为(x2,y1)。

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

const int N=1010;

struct node
{
    int x, y;
}no[N];
struct mode
{
    int x1, y1, x2, y2, s;
}mo[N];
int vis[N][N];

int cmp(const void *a, const void *b)
{
    node *s1 = (node *)a, *s2 = (node *)b;
    if (s1->x != s2->x)
        return s1->x - s2->x;
    return s1->y - s2->y;
}

int Judge(mode a, mode b) ///判断两个矩阵是否满足情况
{
    if (b.x2 < a.x1 || b.x1 > a.x2 || b.y1 > a.y2 || b.y2 < a.y1)
        return 1;
    else if (b.x1 > a.x1 && b.x2 < a.x2 && b.y1 > a.y1 && b.y2 < a.y2)
        return 2;
    return 0;
}

int main ()
{
    int n, i, j, k, ans;

    while (scanf("%d", &n), n)
    {
        k = ans = 0;
        memset(vis, 0, sizeof(vis));

        for (i = 0; i < n; i++)
        {
            scanf("%d%d", &no[i].x, &no[i].y);
            vis[no[i].x][no[i].y] = 1;
        }

        qsort(no, n, sizeof(no[0]), cmp);

        for (i = 0; i < n-1; i++)
        {
            for (j = i+1; j < n; j++)
            {
                if (no[i].x < no[j].x && no[i].y < no[j].y && vis[no[i].x][no[j].y] && vis[no[j].x][no[i].y]) ///排序后判断两点能否组成矩阵
                {
                    mo[k].x1 = no[i].x;
                    mo[k].x2 = no[j].x;
                    mo[k].y1 = no[i].y;
                    mo[k].y2 = no[j].y;
                    mo[k++].s = abs(no[i].x-no[j].x) * abs(no[i].y-no[j].y);
                }
            }
        }

        for (i = 0; i < k; i++)
        {
            for (j = 0; j < k; j++)
            {
                if (Judge(mo[i], mo[j]) == 1)
                    ans = max(ans, mo[i].s+mo[j].s);
                else if (Judge(mo[i], mo[j]) == 2)
                    ans = max(ans, mo[i].s);
            }
        }

        if (ans == 0) printf("imp
");
        else printf("%d
", ans);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/syhandll/p/4860314.html