Chance to Encounter a Girl ZOJ - 2271(概率Dp)

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

題意:n*n的格子,女孩再(n/2,n/2),男孩在(-1,n/2),女孩每天位移的方向不確定且位移一格,男孩每天向右位移一格,問在男孩沒有離開方格之前,遇見女孩的概率是多少?



#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include<set>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int mod=1000000007;
const int N=1e2+12;
double dp[N][N][N];

int main()
{
    int n,m;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<=n;i++)
            for(int j=1;j<=n;j++)
                for(int k=1;k<=n;k++)
                    dp[j][k][i]=0;
        dp[n/2+1][n/2+1][0]=1;//女孩開始所在放個概率爲一

        double ans=0;
        for(int i=0;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                for(int k=1;k<=n;k++)
                {   int p=4;
                    if(j==1||j==n)
                        p--;
                    if(k==1||k==n)
                        p--;
                    if(j!=n)
                        dp[j+1][k][i+1]+=dp[j][k][i]/p;
                    if(j!=1)
                        dp[j-1][k][i+1]+=dp[j][k][i]/p;
                    if(k!=n)
                        dp[j][k+1][i+1]+=dp[j][k][i]/p;
                    if(k!=1)
                        dp[j][k-1][i+1]+=dp[j][k][i]/p;
                }
            }
            ans+=dp[n/2+1][i+1][i+1];
            dp[n/2+1][i+1][i+1]=0.0;
        }
        
        printf("%.4lf\n",ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章