ZOJ Problem Set - 2271

Chance to Encounter a Girl

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Long long ago, there was a small but beautiful country, in which lived a very smart girl. The girl was very interested in travelling, every day she travelled from one town to another. Her sense of direction is so bad that she always forgot the road which she had came from and randomly chose a town in the country to move on. She never went out of the country.

You, a very cool man, go back to the past using a time machine and travelled through the country. If you could meet the girl, love would happen and the story would have a happy end.

So, what's the probability of a happy end?

The country consists of n * n blocks (n = 2*k - 1; k = 2,...,50), each block is a town. Every day, the girl can move up, left, down or right to a neighbor town, and you, the cool man, must move right until you meet the girl or get outside the country.

Assume the left bottom town is (0, 0). Initially, the girl is in the town (n/2, n/2) and you are in (-1, n/2).

The following figure is a sample of country (3 * 3), G is the girl's inital position and Y is yours.

        +-+-+-+
        | | | |
      +-+-+-+-+
      |Y| |G| |
      +-+-+-+-+
        | | | |
        +-+-+-+


Input

There are multiple test cases(less than 30). For each case there is only one line containing an integer n (n = 2 * k - 1; 1 < k <= 50). Proceed until the end of file.


Output

For each test case you should print the possibility of a happy end. Round to four decimal digits.


Sample Input

3


Sample Output

0.6667

概率DP

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 const int N=100;
 7 double per[N][N],dp[N][N];
 8 double result[N];
 9 int n;
10 int bound(int i,int j)
11 {//统计可以到达坐标(i,j)的坐标的个数
12     int res=0;
13     if(i+1<n)
14         res++;
15     if(i>0)
16         res++;
17     if(j+1<n)
18         res++;
19     if(j>0)
20         res++;
21     return res;
22 }
23 void traval()
24 {
25     memset(dp,0,sizeof(dp));
26     for(int i=0;i<n;i++){
27         for(int j=0;j<n;j++){
28             if(per[i][j]!=0)
29             {
30                 double temp=per[i][j]/bound(i,j);
31                 if(i+1<n)
32                 dp[i+1][j]+=temp;
33                 if(i>0)
34                 dp[i-1][j]+=temp;
35                 if(j+1<n)
36                 dp[i][j+1]+=temp;
37                 if(j>0)
38                 dp[i][j-1]+=temp;
39             }
40         }
41     }
42     memcpy(per,dp,sizeof(dp));
43 }
44 void fun()
45 {
46     for(int i=2;i<=50;i++)
47     {
48         n=2*i-1;
49         memset(per,0,sizeof(per));
50         per[n/2][n/2]=1.0;
51         double res=0;
52         for(int i=0;i<n;i++)//男孩旅行的每一天
53         {
54             traval();
55             res+=per[i][n/2];//累加相遇的概率
56             per[i][n/2]=0.0;//记录该点已经走过
57         }
58         result[n]=res;
59     }
60 }
61 int main()
62 {
63     fun();
64     while(scanf("%d",&n)!=EOF)
65     {
66         printf("%.4lf\n",result[n]);
67     }
68     return 0;
69 }


猜你喜欢

转载自www.cnblogs.com/Hello-LiuLiu/p/9094675.html