HDOJ 5128 The E-pang Palace 暴力枚举+计算几何

枚举出每一个矩形,判断相交

如果是回字型则输出大的矩形的面积..........

The E-pang Palace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)

Total Submission(s): 73    Accepted Submission(s): 26

Problem Description

E-pang Palace was built in Qin dynasty by Emperor Qin Shihuang in Xianyang, Shanxi Province. It was the largest palace ever built by human. It was so large and so magnificent that after many years of construction, it still was not completed. Building the great
wall, E-pang Palace and Qin Shihuang‘s tomb cost so much labor and human lives that people rose to fight against Qin Shihuang‘s regime.

Xiang Yu and Liu Bang were two rebel leaders at that time. Liu Bang captured Xianyang -- the capital of Qin. Xiang Yu was very angry about this, and he commanded his army to march to Xianyang. Xiang Yu was the bravest and the strongest warrior at that time,
and his army was much more than Liu Bang‘s. So Liu Bang was frighten and retreated from Xianyang, leaving all treasures in the grand E-pang Palace untouched. When Xiang Yu took Xianyang, he burned E-pang Palce. The fire lasted for more than three months, renouncing
the end of Qin dynasty.

Several years later, Liu Bang defeated Xiangyu and became the first emperor of Han dynasty. He went back to E-pang Palace but saw only some pillars left. Zhang Liang and Xiao He were Liu Bang‘s two most important ministers, so Liu Bang wanted to give them some
awards. Liu Bang told them: "You guys can make two rectangular fences in E-pang Palace, then the land inside the fences will belongs to you. But the corners of the rectangles must be the pillars left on the ground, and two fences can‘t cross or touch each
other."

To simplify the problem, E-pang Palace can be consider as a plane, and pillars can be considered as points on the plane. The fences you make are rectangles, and you MUST make two rectangles. Please note that the rectangles you make must be parallel to the coordinate
axes.

The figures below shows 3 situations which are not qualified(Thick dots stands for pillars):

Zhang Liang and Xiao He wanted the total area of their land in E-pang Palace to be maximum. Please bring your computer and go back to Han dynasty to help them so that you may change the history.

Input

There are no more than 15 test case.

For each test case:

The first line is an integer N, meaning that there are N pillars left in E-pang Palace(4 <=N <= 30).

Then N lines follow. Each line contains two integers x and y (0 <= x,y <= 200), indicating a pillar‘s coordinate. No two pillars has the same coordinate.

The input ends by N = 0.

Output

For each test case, print the maximum total area of land Zhang Liang and Xiao He could get. If it was impossible for them to build two qualified fences, print "imp".

Sample Input

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

Sample Output

2
imp

Source

2014ACM/ICPC亚洲区广州站-重现赛(感谢华工和北大)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>

using namespace std;

const int maxn = 31;

int n,x[maxn],y[maxn];
bool vis[201][201];

struct Point
{
	int x,y;
};

struct MATRIX
{
	Point p[4];
	int area;
};

vector<MATRIX> mat;

bool inside(int& kind,Point p,MATRIX m)
{
	int sx=m.p[0].x,dx=m.p[2].x;
	int sy=m.p[0].y,dy=m.p[2].y;

	if( (p.x>sx&&p.x<dx) && (p.y>sy&&p.y<dy) ) kind=1;
	if( (p.x>=sx&&p.x<=dx) && (p.y>=sy&&p.y<=dy) )
		return true;
	return false;
}

int check(int a,int b)
{
	MATRIX A=mat[a],B=mat[b];

	int k0=0,k1=0,k2=0,k3=0;
	bool ok0=inside(k0,A.p[0],B);
	bool ok1=inside(k1,A.p[1],B);
	bool ok2=inside(k2,A.p[2],B);
	bool ok3=inside(k3,A.p[3],B);

	/// maybe A all in side B
	if(ok0||ok1||ok2||ok3)
	{
		if(k0==1&&k1==1&&k2==1&&k3==1)
			return B.area;
		else return -999;
	}

	k0=0,k1=0,k2=0,k3=0;
	ok0=inside(k0,B.p[0],A);
	ok1=inside(k1,B.p[1],A);
	ok2=inside(k2,B.p[2],A);
	ok3=inside(k3,B.p[3],A);

	/// maybe B all in side A

	if(ok0||ok1||ok2||ok3)
	{
		if(k0==1&&k1==1&&k2==1&&k3==1)
			return A.area;
		else return -999;
	}

	/// A out of B and B out of A

	if(ok0==false&&ok1==false&&ok2==false&&ok3==false)
	{
		return A.area+B.area;
	}
	return -999;
}

int main()
{
	while(scanf("%d",&n)!=EOF&&n)
	{
		///init
		memset(vis,0,sizeof(vis));
		mat.clear();

		for(int i=0;i<n;i++)
		{
			cin>>x[i]>>y[i];
			vis[x[i]][y[i]]=true;
		}
		/// find all SQR
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				if(x[i]==x[j]) continue;
				if(y[i]==y[j]) continue;
				int sx=min(x[i],x[j]),dx=max(x[i],x[j]);
				int sy=min(y[i],y[j]),dy=max(y[i],y[j]);
				if(vis[sx][sy]&&vis[dx][sy]&&vis[sx][dy]&&vis[dx][dy])
				{
					/// this is a sqrt
					MATRIX m;
					m.p[0].x=sx,m.p[0].y=sy;
					m.p[1].x=dx,m.p[1].y=sy;
					m.p[2].x=dx,m.p[2].y=dy;
					m.p[3].x=sx,m.p[3].y=dy;
					m.area=(dx-sx)*(dy-sy);
					mat.push_back(m);
				}
			}
		}

		int ans=-1;
		int sz=mat.size();
		for(int i=0;i<sz;i++)
		{
			for(int j=i+1;j<sz;j++)
			{
				/// 判断 4 矩形相交
				ans=max(ans,check(i,j));
			}
		}

		if(ans<0) puts("imp");
		else printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-10-10 23:17:43

HDOJ 5128 The E-pang Palace 暴力枚举+计算几何的相关文章

hdoj 4932 Miaomiao&#39;s Geometry 【暴力枚举】

题意:在一条直线上有n个点,取一长度差为x的区间, 规定点必须是区间的端点, 让你找出来最大的x 策略:rt 分析可得:两个相邻点之间的区间要么是两个点的差,要么就是两个点的差的一半,那我们就简单枚举一下就好了 排好序之后再枚举 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define M 200 using namespace std; do

HDU - 5128The E-pang Palace+暴力枚举,计算几何

第一次写计算几何,ac,感动. 不过感觉自己的代码还可以美化一下. 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意: 在一个坐标系中,有n个点,从中找到两个互不touch,互不cross的两个矩形(边要和坐标轴平行),使得面积最大. 思路: 枚举每个点,n的四次方,这题有个坑点是“回型矩阵”,是真的坑,然后discuss区里说的十字形矩阵是骗人的. 每次对枚举的四个点进行check,如果两个矩阵的四个点相互不在对方的矩阵中,那么这组就是

hdoj 4932 Miaomiao&amp;#39;s Geometry 【暴力枚举】

题意:在一条直线上有n个点.取一长度差为x的区间. 规定点必须是区间的端点. 让你找出来最大的x 策略:rt 分析可得:两个相邻点之间的区间要么是两个点的差,要么就是两个点的差的一半,那我们就简单枚举一下就好了 排好序之后再枚举 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define M 200 using namespace std; do

HDU 5128 The E-pang Palace(暴力瞎搞)

题目大意:给你n个点,让你组成矩形,然后如果有两个矩形不相交的话就计算他们的面积,求最大的两个矩形的面积并.注意的是回字型的嵌套,面积的并是最大的矩形的面积. 解题思路:暴力,枚举出来矩形,然后再暴力枚举两个矩形判断是否相交,是否为回字型. The E-pang Palace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 288 

HDOJ 5305 Friends 暴力枚举

暴力枚举边的状态..... Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 120    Accepted Submission(s): 39 Problem Description There are n people and m pairs of friends. For every pair of friends,

hdu5616 暴力枚举

2017-08-25 20:08:54 writer:pprp 题目简述: ? HDU 5616? n个砝码,可以放在天平左右两侧或不放? m次询问,每次询问是否可以测出给定重量? 1 ≤ n ≤ 20? 1 ≤ m ≤ 100 这道题采用枚举的思路的话实现起来还是有点困难的, 要实现的功能是对每个砝码进行处理,加到左边, 加到右边,或者是不加 看了大神的代码,感觉很巧妙, 设置了两个标记数组 vis1[2005], vis2[2005] 一个vis1用来记录当前已经可以实现的重量 另一个vis

hdu4282A very hard mathematic problem 暴力枚举

//给出k //找x,y,z使得x^z+y^z+x*y*z = k //x,y,z都为正整数x<y,z>1问有多少种方法 //当z = 2时,可以看到左边是一个完全平方 //而当z>=3时,可以暴力枚举x,y //由于k<2^31所以x<2^(31/3)枚举复杂度可以过 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> using name

hdu 5247 找连续数【暴力枚举】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5247 分析:这道题是2015百度之星初赛1的2题,当时没看这道题 是队友看的,比完以后也做了一下,思路大体都是一样的,就是 暴力枚举,因为k<=1000,那么我们可以每一点x为起点跑[x,x+999] 这段区间,把每得到一段连续的子区间[x,?],则num[len]++(len=size([x,?])); 这样就可以了,最后num数组里就是对应的答案 献上代码: #include<stdio.h&

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm