POJ 1379 Run Away 模拟退火

一开始写了一发很快的,发现一会能过一会不能,貌似有点悬,毕竟是随机算法。

后来重写了一发迭代5遍的,基本上把把AC了= =

模拟退火果然是一种不是很靠谱的算法。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;
const int maxn = 1005;
const double eps = 1e-8;
const double r = 0.99;
const int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
const int dy[] = { 1, -1, 0, 0, 1, -1, -1, 1 };

int px[maxn], py[maxn];
int X, Y, M;

double sq(double x) {
	return x * x;
}

double dis(double x, double y) {
	double ret = 1e90;
	for (int i = 0; i < M; i++) {
		ret = min(ret, sqrt(sq(x - px[i]) + sq(y - py[i])));
	}
	return ret;
}

double solve(double &x, double &y) {
	double nowdis = dis(x, y);
	double step = max(X, Y);
	while (step > eps) {
		for (int i = 0; i < 8; i++) {
			double nx = x + dx[i] * step,
				ny = y + dy[i] * step,
				ndis = dis(nx, ny);
			if (nx > X || nx < 0 || ny > Y || ny < 0) continue;
			if (ndis > nowdis) {
				x = nx; y = ny; nowdis = ndis;
			}
		}
		step *= r;
	}
	return nowdis;
}

int main() {
	int T; scanf("%d", &T);
	srand(time(NULL));
	while (T--) {
		scanf("%d%d%d", &X, &Y, &M);
		for (int i = 0; i < M; i++) scanf("%d%d", &px[i], &py[i]);
		double nowdis = 0, x, y;
		for (int i = 0; i < 5; i++) {
			double tx = rand() % (X), ty = rand() % (Y);
			double ret = solve(tx, ty);
			if (ret > nowdis) {
				nowdis = ret;
				x = tx; y = ty;
			}
		}
		printf("The safest point is (%.1f, %.1f).\n",
			x, y);
	}
	return 0;
}

  

时间: 2024-12-28 20:42:53

POJ 1379 Run Away 模拟退火的相关文章

poj 1379 Run Away 模拟退火 难度:1

Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6482   Accepted: 1993 Description 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 complet

POJ 1379 Run Away 【基础模拟退火】

题意:找出一点,距离所有所有点的最短距离最大 二维平面内模拟退火即可,同样这题用最小圆覆盖也是可以的. Source Code: //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h> #include <iostream> #include <fstream> #include <cstring> #include <cm

POJ 1379 Run Away

题意:有n个陷阱,在X,Y范围内要求出一个点使得这个点到陷阱的最小距离最大. 思路:模拟退火,随机撒入40个点,然后模拟退火随机化移动. (这题poj坑爹,加了srand(time(NULL))不能交G++,不加srand(time(NULL))又会WA,交了C++不能用acos(-1),只能用3.1415926代替,真是坑爹.) #include<algorithm> #include<cstdio> #include<cmath> #include<cstri

PKU 1379 Run Away(模拟退火算法)

题目大意:原题链接 给出指定的区域,以及平面内的点集,求出一个该区域内一个点的坐标到点集中所有点的最小距离最大. 解题思路:一开始想到用随机化算法解决,但是不知道如何实现.最后看了题解才知道原来是要用模拟退火算法解决. 不过个人感觉这个算法的实现过程中仍然采用了随机化算法.二者均属于概率算法.  参考链接 Point Goto_Rand_Dir(double key,Point temp)函数中,Point temp必须得定义在参数中,不能定义在函数内部, 否则temp没有初始值,无法进行后面的

POJ 1379 模拟退火算法

求规定平面上一点到已知点的最小距离最大的点. 模拟退火的流程是,随机构造几组解作为初始解空间,每次对当前解空间进行随机扩展,若发现更优解则替换. 进行的次数由参数人为控制,而随机扩展的幅度也是随着次数逐渐减小的. #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<string> #include&

POJ 1379 模拟退火法

Run Away Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5631   Accepted: 1728 Description 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 complet

POJ 1379

模拟退火算法. 随机MAX个点,然后,退火移动,选取距离所有点中最短中最长者即可.理解和我上一篇一样. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <time.h> using namespace std; const int MAXN=1010; const int MA

Run Away 模拟退火

Run Away Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 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

POJ 1379 模拟退火

模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举,那就模拟退火. #include <stdio.h> #include <math.h> #include <algorithm> using namespace std; const int maxn = 1005; int X,Y,M; int Kase; struct