HDU 5128 暴力

HDU 5128
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5128
题意:
给一些点,问任意选四个点组成矩形1,再选不同的四个点组成矩形2。问在合法情况(不相交,无公共边、点)两者覆盖的平面面积和是多少
思路:
大暴力,当作大模拟写就行。注意一个矩形完全覆盖另一个矩形的情况是合法的,只算大矩形到的面积。
源码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
const int MAXN = 50 + 5;
const int MAXM = 200 + 5;
struct Point
{
    int x, y;
    Point(){}
    Point(int _x, int _y){x = _x, y = _y;}
}point[MAXN];
int gra[MAXM][MAXM];
bool cmp(Point a, Point b)
{
    if(a.x != b.x)  return a.x < b.x;
    else return a.y < b.y;
}
vector<int>col[MAXM];
int cur[MAXM];
int n;
int X1[5], Y1[5], X2[5], Y2[5];
//void check()
//{
//    printf("*******//////checking////////*********\n");
//    for(int i = 1 ; i <= 4 ; i++)
//        printf("x1[%d] = %d, y1[%d] = %d\n", i, x1[i], i, y1[i]);
//    for(int i = 1 ; i <= 4 ; i++)
//        printf("x2[%d] = %d, y2[%d] = %d\n", i, x2[i], i, y2[i]);
//    printf("*******//////checking////////*********\n");
//    system("pause");
//}
int cal()
{
//    check();
    if(X1[1] > X2[2] || X1[2] < X2[2] || Y1[3] < Y2[1] || Y1[1] > Y1[3]){
        return (X2[4] - X2[1]) * (Y2[4] - Y2[1]) + (X1[4] - X1[1]) * (Y1[4] - Y1[1]);
    }
    else if(X1[1] < X2[1] && Y1[1] < Y2[1] && X1[4] > X2[4] && Y1[4] > Y2[4]){
        return (X1[4] - X1[1]) * (Y1[4] - Y1[1]);
    }
    else if(X1[1] > X2[1] && Y1[1] > Y2[1] && X1[4] < X2[4] && Y1[4] < Y2[4]){
        return (X2[4] - X2[1]) * (Y2[4] - Y2[1]);
    }
    else
        return -1;
}
int solve1(int x, int y, int cnt)
{
    int ans = -1;
    int ne = cnt;
    while(ne <= n && point[ne].x == x)  ne++;
    if(ne > n)  return -1;
//    printf("ne = %d, n = %d, cnt = %d\n", ne, n, cnt);
    for(int i = cnt + 1 ; i <= n ; i++){
        if(point[i].x != x)
            break;
        for(int j = 0 ; j < (int)col[y].size() ; j++){
            int now = col[y][j];
            if(point[now].x <= x || now <= i)   continue;
            X1[1] = x, Y1[1] = y;
            X1[2] = point[now].x, Y1[2] = point[now].y;
            X1[3] = point[i].x, Y1[3] = point[i].y;
            X1[4] = point[now].x, Y1[4] = point[i].y;
//            for(int k = 1 ; k <= 4 ; k++){
//                printf("x1[%d] = %d, y1[%d] = %d\n", k, x1[k], k, y1[k]);
//            }
//            system("pause");
            if(gra[X1[4]][Y1[4]] != 0){
//                printf("hahah\n");
                for(int lv = i + 1 ; lv <= n ; lv++){
                    int tx = point[lv].x, ty = point[lv].y;
                    for(int t1 = lv + 1 ; t1 <= n ; t1++){
                        if(point[t1].x != tx){
//                            printf("first\n");
                            break;
                        }
                        for(int t2 = 0 ; t2 < (int)col[ty].size() ; t2++){
//                            printf("fsafsa\n");
                            if(point[col[ty][t2]].x <= tx)    continue;
                            int tnow = col[ty][t2];
                            X2[1] = tx, Y2[1] = ty;
                            X2[2] = point[t1].x, Y2[2] = point[t1].y;
                            X2[3] = point[tnow].x, Y2[3] = point[tnow].y;
                            X2[4] = point[tnow].x, Y2[4] = point[t1].y;
//                            for(int k = 1 ; k <= 4 ; k++){
//                                printf("x2[%d] = %d, y2[%d] = %d\n", k, x2[k], k, y2[k]);
//                            }
//                            printf("second\n");
                            if(gra[X2[4]][Y2[4]] == 0)  continue;
                            ans = max(ans, cal());
                        }
                    }
                }
            }

        }
    }
    return ans;
}
int main()
{
//    freopen("HDU 5128.in", "r", stdin);
    while(scanf("%d", &n) != EOF && n){
        for(int i = 1 ; i <= n ; i++){
            scanf("%d%d", &point[i].x, &point[i].y);
        }
        sort(point + 1, point + n + 1, cmp);
        memset(gra, 0, sizeof(gra));
        for(int i = 0 ; i < MAXM ; i++)
            col[i].clear(), cur[i] = 0;
        for(int i = 1 ; i <= n ; i++){
            gra[point[i].x][point[i].y] = i;
            col[point[i].y].push_back(i);
        }
        int ans = -1;
        for(int i = 1 ; i <= n ; i++){
            int x = point[i].x;
            int y = point[i].y;
//            cur[x]++;
            ans = max(ans, solve1(x, y, i));
        }
        if(ans == -1)
            printf("imp\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值