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

来源:互联网 发布:类似弹个车的软件 编辑:程序博客网 时间:2024/06/02 11:37

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

且这两个矩形不相交

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

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

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

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

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

代码如下:

/* ***********************************************Author        :yinhuaEmail         :yinwoods@163.comFile Name     :c.cppCreated 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 longusing 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;}


0 0
原创粉丝点击