简单几何(判断矩形的位置) UVALive 7070 The E-pang Palace(14广州B)

 

题目传送门

题意:给了一些点,问组成两个不相交的矩形的面积和最大

分析:暴力枚举,先找出可以组成矩形的两点并保存起来(vis数组很好),然后写个函数判断四个点是否在另一个矩形内部。当时没有保存矩形,用for来找矩形,结果写糊涂了忘记判断回形的情况。。。

 

/************************************************
* Author        :Running_Time
* Created Time  :2015/11/6 星期五 17:00:44
* File Name     :B_2.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
struct Point    {
    int x, y;
    Point () {}
    Point (int x, int y) : x (x), y (y) {}
    bool operator < (const Point &r) const {
        if (x == r.x)   return y < r.y;
        else    return x < r.x;
    }
}p[33];
struct Matrix   {
    Point a, b;
    Matrix () {}
    Matrix (Point a, Point b) : a (a), b (b) {}
};
vector<Matrix> mat;
bool vis[N][N];

int inside(Point p, Point a, Point b)  {
    if (p.x >= a.x && p.x <= b.x
        && p.y <= a.y && p.y >= b.y)    {
        if (p.x > a.x && p.x < b.x
            && p.y < a.y && p.y > b.y)  return -1;
        else    return 1;
    }
    else    return 0;
}

int area_mat(int i) {
    return (mat[i].b.x - mat[i].a.x) * (mat[i].a.y - mat[i].b.y);
}

int judge(int i, int j) {
    Point ic = Point (mat[i].a.x, mat[i].b.y),
          id = Point (mat[i].b.x, mat[i].a.y);
    int res1 = inside (mat[i].a, mat[j].a, mat[j].b);
    int res2 = inside (mat[i].b, mat[j].a, mat[j].b);
    int res3 = inside (ic, mat[j].a, mat[j].b);
    int res4 = inside (id, mat[j].a, mat[j].b);
    if (!res1 && !res2 && !res3 && !res4)   return 0;
    else if (res1 == -1 && res2 == -1 && res3 == -1 && res4 == -1)   return -1;
    else    return 1;
}

int main(void)    {
    int n;
    while (scanf ("%d", &n) == 1)   {
        if (!n) break;
        mat.clear ();
        memset (vis, false, sizeof (vis));
        for (int i=0; i<n; ++i)   {
            scanf ("%d%d", &p[i].x, &p[i].y);
            vis[p[i].x][p[i].y] = true;
        }
        sort (p, p+n);
        for (int i=0; i<n; ++i) {
            int x1 = p[i].x, y1 = p[i].y;
            for (int j=i+1; j<n; ++j)   {
                int x2 = p[j].x, y2 = p[j].y;
                if (x1 >= x2 || y1 <= y2)   continue;
                if (!vis[x1][y2] || !vis[x2][y1])   continue;
                mat.push_back (Matrix (Point (x1, y1), Point (x2, y2)));
            }
        }
        int ans = 0;
        for (int i=0; i<mat.size (); ++i)   {
            for (int j=i+1; j<mat.size (); ++j) {
                int res1 = judge (i, j);
                int res2 = judge (j, i);
                if (!res1 && !res2) {
                    ans = max (ans, area_mat (i) + area_mat (j));
                }
                if (res1 == -1 || res2 == -1)   {
                    ans = max (ans, max (area_mat (i), area_mat (j)));
                }
            }
        }
        
        if (ans == 0)   puts ("imp");
        else    printf ("%d\n", ans);
    }

   //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";

    return 0;
}

 

这个很挫的代码放在这留个念。。。

/************************************************
* Author        :Running_Time
* Created Time  :2015/10/14 星期三 14:59:33
* File Name     :B.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

const int N = 33;
const int M = 210;
const int INF = 0x3f3f3f3f;
struct Point    {
    int x, y;
    bool operator < (const Point &r) const  {
        if (x == r.x)   return y < r.y;
        else    return x < r.x;
    }
}p[N];
vector<int> vx[M], vy[M];

int cal(int la, int lb) {
    if (la < 0) la = -la;
    if (lb < 0) lb = -lb;
    return la * lb;
}

int main(void)    {
    int n;
    while (scanf ("%d", &n) == 1)   {
        if (!n) break;
        for (int i=0; i<=200; ++i)  {
            vx[i].clear (); vy[i].clear ();
        }
        for (int i=1; i<=n; ++i)    {
            scanf ("%d%d", &p[i].x, &p[i].y);
        }
        sort (p+1, p+1+n);
        for (int i=1; i<=n; ++i)    {
            vx[p[i].x].push_back (i);   vy[p[i].y].push_back (i);
        }
        int ans = 0;
        bool flag = false;
        for (int i=1; i<=n; ++i)    {           //i one
            int x = p[i].x, y = p[i].y;
            if (vx[x].size () <= 1 || vy[y].size () <= 1)   continue;
            for (int j=0; j<vx[x].size (); ++j) {       //j two
                int jj = vx[x][j];
                if (jj == i || p[jj].y <= y)  continue;
                int jy = p[jj].y;
                for (int k=0; k<vy[y].size (); ++k) {       //k three
                    int kk = vy[y][k];
                    if (kk == i || p[kk].x <= x)    continue;
                    int kx = p[kk].x;
                    if (vx[kx].size () <= 1)    continue;
                    for (int l=0; l<vx[kx].size (); ++l)    {       //l four
                        int ll = vx[kx][l];
                        if (ll == kk || p[ll].y <= y)   continue;
                        if (p[ll].y == jy)  {                            //find the first rectangle
                            
                            if (vy[jy].size () >= 4)    {
                                for (int r=0; r<vy[jy].size (); ++r)    {
                                    int rr = vy[jy][r];
                                    if (rr == ll || rr == jj)   continue;
                                    for (int r3=0; r3<vy[jy].size (); ++r3) {
                                        int r4 = vy[jy][r3];
                                        if (r4 == ll || r4 == jj || r4 == rr)   continue;
                                        int xx = p[rr].x, kkx = p[r4].x;
                                        if (x <= xx && xx <= kx)    continue;
                                        if (x <= kkx && kkx <= kx)  continue;
                                        if (vx[xx].size () <= 1 || vx[kkx].size () <= 1)    continue;
                                        for (int o=0; o<vx[xx].size (); ++o)    {
                                            int oo = vx[xx][o];
                                            if (oo == rr || p[oo].y <= jy)  continue;
                                            int jjy = p[oo].y;
                                            for (int u=0; u<vx[kkx].size (); ++u)   {
                                                int uu = vx[kkx][u];
                                                if (uu == r4 || p[uu].y != jjy) continue;
                                                flag = true;
                                                ans = max (ans, cal (kx - x, jy - y) + cal (kkx - xx, jjy - jy));
                                            }
                                        }
                                    }
                                }
                            }

                            for (int yy=jy+1; yy<=200; ++yy)    {
                                if (vy[yy].size () <= 1)    continue;
                                for (int yi=0; yi<vy[yy].size (); ++yi) {
                                    int ii = vy[yy][yi];
                                    int xx = p[ii].x, yy = p[ii].y;     //to find the second rectangle
                                    for (int j3=0; j3<vx[xx].size (); ++j3)  {
                                        int j4 = vx[xx][j3];
                                        if (j4 == ii || p[j4].y <= yy)  continue;
                                        int jjy = p[j4].y;
                                        for (int k3=0; k3<vy[yy].size (); ++k3) {
                                            int k4 = vy[yy][k3];
                                            if (k4 == ii)  continue;
                                            int kkx = p[k4].x;
                                            for (int l3=0; l3<vx[kkx].size (); ++l3)    {
                                                int l4 = vx[kkx][l3];
                                                if (l4 == k4 || p[l4].y != jjy)   continue;
                                                flag = true;
                                                ans = max (ans, cal (kx - x, jy - y) + cal (kkx - xx, jjy - yy));
                                            }
                                        }
                                    }
                                }
                            }
                            

                            if (vx[kx].size () >= 4)    {
                                for (int r=0; r<vx[kx].size (); ++r)    {
                                    int rr = vx[kx][r];
                                    if (rr == kk || rr == ll)   continue;
                                    for (int r3=0; r3<vy[jy].size (); ++r3) {
                                        int r4 = vx[kx][r3];
                                        if (r4 == kk || r4 == ll || r4 == rr)   continue;
                                        int yy = p[rr].y, jjy = p[r4].y;
                                        if (yy > jjy)   swap (yy, jjy);
                                        if (y <= yy && yy <= jy)    continue;
                                        if (y <= jjy && jjy <= jy)  continue;
                                        if (vy[yy].size () <= 1 || vy[jjy].size () <= 1)    continue;
                                        for (int o=0; o<vy[yy].size (); ++o)    {
                                            int oo = vy[yy][o];
                                            if (oo == rr || p[oo].x <= kx)  continue;
                                            int kkx = p[oo].x;
                                            for (int u=0; u<vy[jjy].size (); ++u)   {
                                                int uu = vy[jjy][u];
                                                if (uu == r4 || p[uu].x != kkx) continue;
                                                flag = true;
                                                ans = max (ans, cal (kx - x, jy - y) + cal (kkx - kx, jjy - jy));
                                            }
                                        }
                                    }
                                }
                            }

                            for (int x5=kx+1; x5<=200; ++x5)   {
                                if (vx[x5].size () <= 1)    continue;
                                for (int xi=0; xi<vx[x5].size (); ++xi) {
                                    int ii = vx[x5][xi];
                                    int xx = p[ii].x, yy = p[ii].y;     //to find the second rectangle
                                    for (int j3=0; j3<vx[x5].size (); ++j3) {
                                        int jj = vx[x5][j3];
                                        if (jj == ii || p[jj].y <= yy)  continue;
                                        int jjy = p[jj].y;
                                        if (vy[yy].size () <= 1)    continue;
                                        for (int k3=0; k3<vy[yy].size (); ++k3) {
                                            int k4 = vy[yy][k3];
                                            if (k4 == ii || p[k4].x <= xx)  continue;
                                            int kkx = p[k4].x;
                                            for (int l3=0; l3<vx[kkx].size (); ++l3)    {
                                                int l4 = vx[kkx][l3];
                                                if (l4 == k4 || p[l4].y != jjy) continue;
                                                flag  = true;
                                                ans = max (ans, cal (kx - x, jy - y) + cal (kkx - xx, jjy - yy));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if (flag)    {
            printf ("%d\n", ans);
        }
        else    puts ("imp");
    }

    return 0;
}

  

posted @ 2015-11-06 17:49  Running_Time  阅读(192)  评论(0编辑  收藏  举报