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

给出多个点,问你能否找到两个四条边都平行坐标轴的矩形

且这两个矩形不相交

所能找到输出对应的最大面积,否则输出imp

这个题看起来很难,但其实还是蛮容易的(如果不考虑坑的话)

我的做法是暴力找出所有的矩形保存起来,再暴力求解

这个题有个坑就是两个矩形可能会形成回字形

这种情况对应的面积应该是外面大矩形的面积

代码如下:

/* ***********************************************

Author        :yinhua

Email         :yinwoods@163.com

File Name     :c.cpp

Created Time  :2014年12月03日 星期三 12时28分41秒

************************************************ */

#include <vector>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 10010
#define LL long long
using namespace std;

bool vis[MAXN][MAXN];

struct DOT {
    int x, y;
    DOT() {}
    DOT(int _x, int _y) {
        x = _x; y = _y;
    }
}dot[MAXN];


struct Matrix {
    DOT up, down; 
    Matrix() { }
    Matrix(DOT a, DOT b) {
        up = a;
        down = b;
    }
}maxrix[MAXN];

int area(Matrix m) {
    return (m.down.x-m.up.x)*(m.up.y-m.down.y);
}
vector<Matrix> vec;

bool cmp(DOT a, DOT b) {
    if(a.x < b.x)
        return true;
    if(a.x > b.x)
        return false;
    return a.y <= b.y;
}

int son_judge(DOT t, Matrix m) {
    int x1 = m.up.x;
    int y1 = m.up.y;
    int x2 = m.down.x;
    int y2 = m.down.y;
    if(t.x>=x1 && t.x<=x2 && t.y<=y1 && t.y>=y2) {
        if(t.x>x1 && t.x<x2 && t.y<y1 && t.y>y2)
            return 1;
        return 0;
    }
    return -1;
}

int judge(int i, int j) {
    DOT t1 = DOT(vec[i].up.x, vec[i].down.y);
    DOT t2 = DOT(vec[i].down.x, vec[i].up.y);

    int ok1 = son_judge(vec[i].up, vec[j]);//判断点是否在矩形内部
    int ok2 = son_judge(vec[i].down, vec[j]);
    int ok3 = son_judge(t1, vec[j]);
    int ok4 = son_judge(t2, vec[j]);
    if(ok1==1&&ok2==1&&ok3==1&&ok4==1)//构成回字形
        return 1;
    if(ok1==-1&&ok2==-1&&ok3==-1&&ok4==-1)//四个点全在其外部
        return -1;
    return 0;
}

int main() {

    int n;
    while(scanf("%d", &n) && n) {
        if(n < 8) {
            puts("imp");
            continue;
        }
        vec.clear();
        memset(vis, 0, sizeof(vis));
        for(int i=0; i<n; ++i) {
            scanf("%d%d", &dot[i].x, &dot[i].y);
            vis[dot[i].x][dot[i].y] = true;
        }
        sort(dot, dot+n, cmp);
        int x1, x2, y1, y2;
        for(int i=0; i<n; ++i) {
            x1 = dot[i].x;
            y1 = dot[i].y;
            for(int j=i+1; j<n; ++j) {
                x2 = dot[j].x;
                y2 = dot[j].y;
                if(x2<=x1 || y2>=y1) continue;
                if(vis[x2][y1] && vis[x1][y2]) {
                    vec.push_back(Matrix(dot[i], dot[j]));
                }
            }
        }
        int ans = 0;
        for(int i=0; i<vec.size(); ++i) {
            //printf("%d %d %d %d\t", vec[i].up.x, vec[i].up.y, vec[i].down.x, vec[i].down.y);
            for(int j=i+1; j<vec.size(); ++j) {
                int tmp1 = judge(i, j);
                int tmp2 = judge(j, i);
                if(tmp1 == 1 || tmp2 == 1) {
                    ans = max(ans, max(area(vec[i]), area(vec[j])));
                } else if(tmp1 == -1 && tmp2 == -1) {
                    /*
                    printf("%d %d %d %d\n", vec[i].up.x, vec[i].up.y, vec[i].down.x, vec[i].down.y);
                    printf("%d %d %d %d\n", vec[j].up.x, vec[j].up.y, vec[j].down.x, vec[j].down.y);
                    */
                    ans = max(ans, area(vec[i])+area(vec[j]));
                }
            }
        }
        if(ans == 0)
            puts("imp");
        else printf("%d\n", ans);
    }
    return 0;
}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值