hdu

http://acm.hdu.edu.cn/showproblem.php?pid=5128

给出n个点,求n个点组成两个矩形的最大面积.

矩形必须平行x轴,并且不能相交,但是小矩形在大矩形内部是可以的,面积就为大矩形的面积.

我是枚举一条对角线,然后去找另外两个点是否在坐标中存在这样就可以确定一个矩形,

同理可以枚举出另外有一个矩形,但是要注意坐标不能重复,

判断矩形相交的话,只要判断一个矩形的4个点是否有在另一个矩形的范围内即可.

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<set>
  4 #include<iostream>
  5 #include<algorithm>
  6 using namespace std;
  7 struct point
  8 {
  9     int x,y;
 10     bool operator < (const point a) const
 11     {
 12         return x==a.x?y<a.y:x<a.x;
 13     }
 14 }p[35],q1,q2,q3,q4;
 15 
 16 int check(point a,point b,point c) //注意区分是在矩形内部还是在矩形边上
 17 {
 18     if(a.x>=b.x&&a.x<=c.x&&a.y>=c.y&&a.y<=b.y)
 19     {
 20         if(a.x>b.x&&a.x<c.x&&a.y>c.y&&a.y<b.y) return 1;
 21         return 0;
 22     }
 23     return -1;
 24 }
 25 int main()
 26 {
 27     //freopen("a.txt","r",stdin);
 28     int n,area;
 29     while(~scanf("%d",&n)&&n)
 30     {
 31         if(n<8) {printf("imp
");continue;}
 32         set<point>s;
 33        // set<point>::iterator it;
 34         int maxn=0,cnt=0;
 35         for(int i=1;i<=n;i++)
 36         {
 37             scanf("%d%d",&p[i].x,&p[i].y);
 38             s.insert(p[i]);
 39         }
 40         sort(p+1,p+n+1);
 41         /*for(it=s.begin();it!=s.end();it++)
 42         {
 43            point t=*it;
 44            printf("%d %d
",t.x,t.y);
 45         }*/
 46         for(int i=1;i<=n;i++)
 47         {
 48             for(int j=i+1;j<=n;j++)
 49             {
 50                 if(p[i].x<p[j].x&&p[i].y<p[j].y) //枚举出第一个矩形
 51                 {
 52                     q1.x=p[i].x;q1.y=p[j].y;
 53                     if(!s.count(q1))continue;
 54                     q2.x=p[j].x;q2.y=p[i].y;
 55                     if(!s.count(q2))continue;
 56                     for(int l=1;l<=n;l++) 
 57                     {
 58                         for(int k=l+1;k<=n;k++)
 59                         {
 60                             if(p[l].x<p[k].x&&p[l].y<p[k].y)//第二个矩形
 61                             {
 62                                 q3.x=p[l].x;q3.y=p[k].y;
 63                                 if(!s.count(q3))continue;
 64                                 q4.x=p[k].x;q4.y=p[l].y;
 65                                 if(!s.count(q4))continue;
 66                                 if(i==l||j==l||i==k||j==k)continue;  //防止坐标重复
 67                                 if(q3.x==p[i].x&&q3.y==p[i].y||q3.x==p[j].x&&q3.x==p[j].y) continue;
 68                                 if(q4.x==p[i].x&&q4.y==p[i].y||q4.x==p[j].x&&q4.x==p[j].y) continue;
 69 
 70                                 int x1=check(p[i],q3,q4);
 71                                 int x2=check(p[j],q3,q4);
 72                                 int x3=check(q1,q3,q4);
 73                                 int x4=check(q2,q3,q4);  //判断在矩形内部还是边界
 74                                 if(x1==1&&x2==1&&x3==1&&x4==1) //内含
 75                                 {
 76                                    // printf("%d %d %d %d
",q1.x,q1.y,q2.x,q2.y);
 77                                     area=abs(q4.x-q3.x)*abs(q3.y-q4.y);
 78                                     if(area>maxn) maxn=area;
 79                                     //printf("%d
",area);
 80                                     cnt=1;
 81                                 }
 82                                 else if(x1==0||x2==0||x3==0||x4==0)continue; //相交
 83                                 else if(x1==-1&&x2==-1&&x3==-1&&x4==-1) //枚举 另一种情况
 84                                 {
 85                                     //printf("%d
",1);
 86                                     int y1=check(p[l],q1,q2);
 87                                     int y2=check(p[k],q1,q2);
 88                                     int y3=check(q3,q1,q2);
 89                                     int y4=check(q4,q1,q2);
 90                                     //printf("%d
",ans);
 91                                   //  printf("%d %d %d %d
",q1.x,q1.y,q2.x,q2.y);
 92                                    // printf("%d %d %d %d
",q3.x,q3.y,q4.x,q4.y);
 93                                     if(y1==0||y2==0||y3==0||y4==0) continue;
 94                                     else if(y1==1&&y2==1&&y3==1&&y4==1)
 95                                     {
 96                                         area=abs(q2.x-q1.x)*abs(q1.y-q2.y);
 97                                         if(area>maxn) maxn=area;
 98                                         cnt=1;
 99                                     }
100                                     else if(y1==-1&&y2==-1&&y3==-1&&y4==-1)
101                                     {
102                                        // printf("%d %d %d %d
",q1.x,q1.y,q2.x,q2.y);
103                                         //printf("%d %d %d %d
",q3.x,q3.y,q4.x,q4.y);
104                                         area=abs(q2.x-q1.x)*abs(q1.y-q2.y)+abs(q4.x-q3.x)*abs(q3.y-q4.y);
105                                       //  printf("%d
",area);
106                                         if(area>maxn) maxn=area;
107                                         cnt=1;
108                                     }
109                                 }
110                             }
111                         }
112                     }
113                 }
114             }
115         }
116         if(!cnt) printf("imp
");
117         else
118         printf("%d
",maxn);
119     }
120     return 0;
121 }
原文地址:https://www.cnblogs.com/nowandforever/p/4720213.html