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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值