HOJ 1026 Run Away


Run Away

One of the traps we will encounter in the Pyramid is located in the Large Room. A lot of small holes are drilled into the floor. They look completely harmless at the first sight. But when activated, they start to throw out very hot java, uh ... pardon, lava. Unfortunately, all known paths to the Center Room (where the Sarcophagus is) contain a trigger that activates the trap. The ACM were not able to avoid that. But they have carefully monitored the positions of all the holes. So it is important to find the place in the Large Room that has the maximal distance from all the holes. This place is the safest in the entire room and the archaeologist has to hide there.

Input Specification

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing three integers X, Y, M separated by space. The numbers satisfy conditions: 1 <= X,Y <=10000, 1 <= M <= 1000. The numbers X and Yindicate the dimensions of the Large Room which has a rectangular shape. The number M stands for the number of holes. Then exactly M lines follow, each containing two integer numbers Ui and Vi (0 <= Ui <= X, 0 <= Vi <= Y) indicating the coordinates of one hole. There may be several holes at the same position.

Output Specification

Print exactly one line for each test case. The line should contain the sentence "The safest point is (P, Q). where P and Qare the coordinates of the point in the room that has the maximum distance from the nearest hole, rounded to the nearest number with exactly one digit after the decimal point (0.05 rounds up to 0.1).

Sample Input:
3
1000 50 1
10 10
100 100 4
10 10
10 90
90 10
90 90
3000 3000 4
1200 85
63 2500
2700 2650 
2990 100
Sample Output:
The safest point is (1000.0, 50.0).
The safest point is (50.0, 50.0).
The safest point is (1433.0, 1669.8).
又学了一种新算法:模拟退火算法。是基于概率的一个算法。这种有关概率的算法都很有趣、神奇啊。不用太多的数学知识就能把一个复杂的数学问题解决掉。乍一看此题,无从下手啊,求三角形外心?中垂线?似乎要考虑的方面很多。。。强大的模拟退火上场:在长方形内随机出来T个点,求出每个点距最近的hole的距离。选择一定的长度作为半径delta开始循环: 对于那的T个点的每一个点 以此点为圆心,以delta为半径,随机出B个点,找出这B个点最优的更新当前点 delta收敛直到delta小于某个值
#include<stdio .h>
#include<math .h>
#define N 1010
#define T 15
#define B 25
#define eps 1e-3
#define INF 1e30
struct CPoint
{
	double x,y;
}p[N];
struct Houxuan
{
	double x,y,d;
}q[T],tmp,tt;
double sqr(double x)
{
	return x*x;
}
double dis(double x,double y,double z,double w)
{
	return sqrt(sqr(x-z)+sqr(y-w));
}
double min(double x,double y)
{
	return x>y?y:x;
}
double max(double x,double y)
{
	return x<y ?y:x;
}
int main()
{
	int t,n,i,j,k;
	double width,length,delta,th;
	double pi = acos(-1);
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf %lf %d",&width,&length,&n);
		for(i = 0 ; i < n ; i++)
			scanf("%lf %lf",&p[i].x,&p[i].y);
		for(i = 0 ; i < T ; i++)
		{
			q[i].x = rand()%1001*width/1000;
			q[i].y = rand()%1001*length/1000;
			q[i].d = INF;
			for(j = 0 ; j < n ; j++)
				q[i].d = min(q[i].d,dis(q[i].x,q[i].y,p[j].x,p[j].y));
		}
		delta = max(width,length)/sqrt(n);
		while(delta > eps)
		{
			for(i = 0 ; i < T ; i++)
			{
				tt = q[i];
				for(j = 0 ; j < B ; j++)
				{
					th = rand()%1001*pi*2/1000;
					tmp.x = tt.x + delta*sin(th);
					tmp.y = tt.y + delta*cos(th);
					if(tmp.x > width || tmp.x <0 || tmp.y > length || tmp.y < 0) continue;
					tmp.d = INF;
					for(k = 0 ; k < n ; k++)
						tmp.d = min(tmp.d, dis(tmp.x,tmp.y,p[k].x,p[k].y));
					if(tmp.d > q[i].d)q[i] = tmp;
				}
			}
			delta *= 0.86;
		}
		j = 0;
		for(i = 1 ; i < T ; i++)
			if( q[i].d > q[j].d ) j = i;
		printf("The safest point is (%.1lf, %.1lf).\n",q[j].x,q[j].y);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值