poj 1379 模拟退火算法的应用

学习自http://blog.csdn.net/dooder_daodao/article/details/6336879

 

View Code
 1 #include<stdio.h>
2 #include<math.h>
3 #include<stdlib.h>
4 #include<time.h>
5 const double inf = 1e10;
6 const double pi = acos(-1.0);
7 const int Rp = 4;
8 const int shift = 60;
9 struct point {
10 double x,y;
11 void goto_rand_dir(double key)
12 {
13 double d=2*pi*(double)rand()/RAND_MAX;
14 x+=key*sin(d);
15 y+=key*cos(d);
16 }
17 void Get_Rand_Point(int a,int b)
18 {
19 x=rand()%a+1;
20 y=rand()%b+1;
21 }
22 }p[1010],randp[Rp];
23 double Dis(point a,point b)
24 {
25 return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
26 }
27 double dis[Rp];
28 int main()
29 {
30 int t,i,j,k,x,y,m;
31 scanf("%d",&t);
32 srand(time(NULL));
33 while(t--)
34 {
35 scanf("%d%d%d",&x,&y,&m);
36 for(i=0;i<m;i++)
37 {
38 scanf("%lf%lf",&p[i].x,&p[i].y);
39 }
40 double tmp;
41 for(i=0;i<Rp;i++)
42 {
43 dis[i]=inf;
44 randp[i].Get_Rand_Point(x,y);
45 for(j=0;j<m;j++)
46 {
47 tmp=Dis(randp[i],p[j]);
48 if(tmp<dis[i])
49 dis[i]=tmp;
50 }
51 }
52 double key=sqrt(1.0*(x*x+y*y))/2;
53 while(key>=0.01)
54 {
55 for(i=0;i<Rp;i++)
56 {
57 for(j=0;j<shift;j++)
58 {
59 point cc=randp[i];
60 cc.goto_rand_dir(key);
61 if(cc.x<0||cc.y<0||cc.x>x||cc.y>y) continue;
62 tmp=inf;
63 for(k=0;k<m;k++)
64 {
65 double di=Dis(cc,p[k]);
66 if(di<tmp)
67 tmp=di;
68 }
69 if(tmp>dis[i])
70 {
71 dis[i]=tmp;
72 randp[i]=cc;
73 }
74 }
75 }
76 key=key*0.8;
77 }
78 for(i=k=0;i<Rp;i++)
79 if(dis[i]>dis[k])
80 k=i;
81 printf("The safest point is (%.1lf, %.1lf).\n",randp[k].x,randp[k].y);
82 }
83 return 0;
84 }



posted @ 2011-10-31 15:39  Because Of You  Views(739)  Comments(0Edit  收藏  举报