题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128
题面:

8 0 0 1 0 0 1 1 1 0 2 1 2 0 3 1 3 8 0 0 2 0 0 2 2 2 1 2 3 2 1 3 3 3 0
2 imp
比较暴力就好,有点类似上周BC的一道题的做法,直接枚举两个对角线点,然后看另外两个点是否存在,存在的话,继续和另一个矩形判断相对位置关系。但是要小心一个矩形包含在另外一个矩形中,也是可以的,面积只算大的那个。详细看代码。
代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
struct point
{
int x,y;
}store[35];
bool map[210][210];
int max(int a,int b)
{
return a>b?a:b;
}
int min(int a,int b)
{
return a<b?a:b;
}
int judge(int a,int b,int c,int d)
{
int res;
//以下分别为两个矩形左下和右上位置坐标
int lx1,lx2,ly1,ly2,rx1,rx2,ry1,ry2;
lx1=min(store[a].x,store[b].x);
ly1=min(store[a].y,store[b].y);
lx2=min(store[c].x,store[d].x);
ly2=min(store[c].y,store[d].y);
rx1=max(store[a].x,store[b].x);
ry1=max(store[a].y,store[b].y);
rx2=max(store[c].x,store[d].x);
ry2=max(store[c].y,store[d].y);
//是同一个矩形
if(lx1==lx2&&ly1==ly2&&rx1==rx2&&ry1==ry2)
return 0;
//一个完全在另外一个上面或者右边,那么就是两个矩形加
if((ry1<ly2)||(ry2<ly1)||(lx1>rx2)||(lx2>rx1))
{
res=(rx1-lx1)*(ry1-ly1)+(rx2-lx2)*(ry2-ly2);
return res;
}
//如果一个完全包含在另外一个里面
if(lx2<lx1&&ly2<ly1&&rx1<rx2&&ry1<ry2)
{
res=(rx2-lx2)*(ry2-ly2);
return res;
}
//如果一个完全包含在另外一个里面
if(lx1<lx2&&ly1<ly2&&rx2<rx1&&ry2<ry1)
{
res=(rx1-lx1)*(ry1-ly1);
return res;
}
//否则即为相交
else
return 0;
}
int main()
{
int n,a,b,c,d,e,f,g,h;
int area;
while(scanf("%d",&n)&&n)
{
area=0;
memset(map,0,sizeof(map));
//读入
for(int i=0;i<n;i++)
{
scanf("%d%d",&store[i].x,&store[i].y);
//将地图上相应的点标记为有
map[store[i].x][store[i].y]=1;
}
//没8个点,就肯定不行
if(n<8)
{
printf("imp\n");
continue;
}
else
{
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
//因为枚举的是对角线,所以在一条边上就跳过
if((store[i].x==store[j].x)||(store[i].y==store[j].y))
continue;
else
{
a=store[i].x;
b=store[i].y;
c=store[j].x;
d=store[j].y;
//如果除了对角线之外,另外两点存在
if(map[c][b]&&map[a][d])
{
for(int k=0;k<n-1;k++)
{
//不与原来两点重复
if((k!=i)&&(k!=j))
{
for(int m=k+1;m<n;m++)
{
//不与原来两点重复
if((m!=i)&&(m!=j))
{
//只枚举对角线
if((store[k].x==store[m].x)||(store[k].y==store[m].y))
continue;
else
{
e=store[k].x;
f=store[k].y;
g=store[m].x;
h=store[m].y;
//如果除了对角线之外,另外两点存在,这时才判断两个矩形关系
if(map[g][f]&&map[e][h])
{
area=max(area,judge(i,j,k,m));
}
}
}
}
}
}
}
else
continue;
}
}
}
if(area==0)
printf("imp\n");
else
printf("%d\n",area);
}
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 5218 The E-pang Palace (简单几何—2014广州现场赛)
原文地址:http://blog.csdn.net/david_jett/article/details/47607015