hdu5128(2014广州区域赛B) 矩形相交相关
题意就是选无相交点的两个矩形,使两矩形面积和最大==
我用hdu2056的矩形相交面积做,还是比较好写的,2056的代码:
1 #include<stdio.h> 2 #include<math.h> 3 #include<algorithm> 4 using namespace std; 5 int main() 6 { 7 double x1,x2,x3,x4,y1,y2,y3,y4; 8 while (~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)) 9 { 10 if (x1>x2) swap(x1,x2); if (y1>y2) swap(y1,y2); 11 if (x3>x4) swap(x3,x4); if (y3>y4) swap(y3,y4); 12 if (x3>=x2||y3>=y2||x1>=x4||y1>=y4) {printf("0.00\n"); continue; } 13 if (x2>x4) swap(x2,x4); if (y2>y4) swap(y2,y4); 14 if (x1>x3) swap(x1,x3); if (y1>y3) swap(y1,y3); 15 printf("%0.2lf\n",fabs((y2-y3)*(x2-x3))); 16 } 17 }
然后用到本题上面,要改一下不能有交点:
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 struct jv{ 6 int x1,y1,x2,y2; 7 }matri[100005]; 8 struct dian{ 9 int x,y; 10 }t[5]; 11 int x[35],y[35]; 12 int cmp(dian n1,dian n2) 13 { 14 if (n1.x<n2.x||(n1.x==n2.x&&n1.y<n2.y)) return 1; 15 return 0; 16 } 17 int cal(int i) 18 { 19 return (matri[i].y2-matri[i].y1)*(matri[i].x2-matri[i].x1); 20 } 21 int judge(int i,int j) 22 { 23 int tmp,x1,x2,x3,x4,y1,y2,y3,y4; 24 x1=matri[i].x1; y1=matri[i].y1; 25 x2=matri[i].x2; y2=matri[i].y2; 26 x3=matri[j].x1; y3=matri[j].y1; 27 x4=matri[j].x2; y4=matri[j].y2; 28 if (x3>x2||y3>y2||x1>x4||y1>y4) return cal(i)+cal(j); 29 if (x3==x1||x3==x2||y3==y1||y3==y2 30 ||x4==x1||x4==x2||y4==y1||y4==y2) return 0; 31 if (x2>x4) swap(x2,x4); if (y2>y4) swap(y2,y4); 32 if (x1>x3) swap(x1,x3); if (y1>y3) swap(y1,y3); 33 tmp=(y2-y3)*(x2-x3); 34 if (tmp<cal(i)&&tmp<cal(j)) return 0; 35 if (tmp==cal(i)) return cal(j); 36 if (tmp==cal(j)) return cal(i); 37 } 38 int main() 39 { 40 int n,i,j,k,p,cnt,ans; 41 while (~scanf("%d",&n)&&n) 42 { 43 for (i=1;i<=n;i++) 44 scanf("%d%d",&x[i],&y[i]); 45 cnt=ans=0; 46 for (i=1;i<=n-3;i++) 47 for (j=i+1;j<=n-2;j++) 48 for (k=j+1;k<=n-1;k++) 49 for (p=k+1;p<=n;p++) 50 { 51 t[1].x=x[i]; t[1].y=y[i]; 52 t[2].x=x[j]; t[2].y=y[j]; 53 t[3].x=x[k]; t[3].y=y[k]; 54 t[4].x=x[p]; t[4].y=y[p]; 55 sort(t+1,t+5,cmp); 56 if (t[1].x!=t[2].x) continue; 57 if (t[3].x!=t[4].x) continue; 58 if (t[1].y!=t[3].y) continue; 59 if (t[2].y!=t[4].y) continue; 60 matri[++cnt].x1=t[1].x; 61 matri[cnt].y1=t[1].y; 62 matri[cnt].x2=t[4].x; 63 matri[cnt].y2=t[4].y; 64 } 65 for (i=1;i<cnt;i++) 66 for (j=i+1;j<=cnt;j++) 67 ans=max(ans,judge(i,j)); 68 if (ans==0) printf("imp\n"); 69 else printf("%d\n",ans); 70 } 71 }