【HDOJ】1109 Run Away

基础模拟退火。

  1 /* poj 1379 */
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <cmath>
  7 #include <algorithm>
  8 using namespace std;
  9
 10 #define MAXN 1005
 11 #define INF     999999
 12 #define MAXM 25
 13
 14 typedef struct {
 15     double x, y;
 16 } Point_t;
 17
 18 const double eps = 1e-3;
 19 const double next = 0.9;
 20 const double PI = acos(-1.0);
 21 double X, Y;
 22 int n;
 23 Point_t points[MAXN];
 24 Point_t rpoints[MAXM];
 25 double rdis[MAXM];
 26
 27 inline bool check(Point_t p) {
 28     return p.x<0 || p.x>=X || p.y<0 || p.y>=Y;
 29 }
 30
 31 double cross(Point_t a, Point_t b, Point_t c) {
 32     return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
 33 }
 34
 35 double Length(Point_t a, Point_t b) {
 36     return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
 37 }
 38
 39 double solve(Point_t p) {
 40     int i, j, k;
 41     double ret = INF;
 42
 43     for (i=0; i<n; ++i) {
 44         ret = min(ret, Length(p, points[i]));
 45     }
 46
 47     return ret;
 48 }
 49
 50 int main() {
 51     int t;
 52     int i, j, k;
 53     Point_t p, pp;
 54     double step, angle;
 55     double ans, tmp;
 56
 57     #ifndef ONLINE_JUDGE
 58         freopen("data.in", "r", stdin);
 59     #endif
 60
 61     scanf("%d", &t);
 62     while (t--) {
 63         scanf("%lf %lf %d", &X, &Y, &n);
 64         for (i=0; i<n; ++i)
 65             scanf("%lf %lf", &points[i].x, &points[i].y);
 66         for (i=0; i<MAXM; ++i) {
 67             rpoints[i].x = (rand()%1000+1)/1000.0*X;
 68             rpoints[i].y = (rand()%1000+1)/1000.0*Y;
 69             rdis[i] = solve(rpoints[i]);
 70         }
 71         step = max(X, Y)/sqrt(1.0*n);
 72         while (step > eps) {
 73             for (i=0; i<MAXM; ++i) {
 74                 p = rpoints[i];
 75                 for (j=0; j<MAXM; ++j) {
 76                     angle = (rand()%1000+1)/1000.*10*PI;
 77                     pp.x = p.x + cos(angle)*step;
 78                     pp.y = p.y + sin(angle)*step;
 79                     if (check(pp))
 80                         continue;
 81                     tmp = solve(pp);
 82                     if (tmp > rdis[i]) {
 83                         rpoints[i] = pp;
 84                         rdis[i] = tmp;
 85                     }
 86                 }
 87             }
 88             step *= next;
 89         }
 90         double ans = -1.0;
 91         k = 0;
 92         for (i=0; i<MAXM; ++i) {
 93             if (rdis[i] > ans) {
 94                 ans = rdis[i];
 95                 k = i;
 96             }
 97         }
 98         printf("The safest point is (%.1lf, %.1lf).\n", rpoints[k].x, rpoints[k].y);
 99     }
100
101     return 0;
102 }
时间: 2024-08-02 23:34:07

【HDOJ】1109 Run Away的相关文章

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

【UVA】10285-Longest Run on a Snowboard(动态规划)

这题出简单了,不需要打印路径. 状态方程dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i+1][j],dp[i][j+1]); 14003395 10285 Longest Run on a Snowboard Accepted C++ 0.026 2014-08-07 11:43:51 枚举每个点进行遍历,使用记忆化搜索.要不会超时. #include<cstdio> #include<cstring> #include<iostream&

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (