首页 > 代码库 > zoj 2271 - Chance to Encounter a Girl

zoj 2271 - Chance to Encounter a Girl

题目:平面图上有一个女孩,她初始在(n/2,,n/2),每次可以走到上下左右四个格子中的一个,

            她每次随机的走的动,你从(-1,n/2)向右移动,问你们相遇的概率。

分析:概率dp。事件为阶段,每个点由上一阶段周围的四个点来维护。

            分成角、边、和中间三种计算概率(分别为1/5,1/3,1/4);

            关于概率的求解,如果遇到就结束了,所以向后走就说明之前没有碰到,

            所以不用前面的碰到的概率计算后面的值;

            时间O(N^3*T),在问题的边缘时间,所以打表计算。

说明:(2011-09-19 09:31)。

#include <stdio.h>
#include <string.h>

double answ[ 51 ] = {
    0.0000,0.6667,0.0000,0.4074,0.0000,
    0.3361,0.0000,0.2928,0.0000,0.2629,
    0.0000,0.2407,0.0000,0.2233,0.0000,
    0.2092,0.0000,0.1975,0.0000,0.1875,
    0.0000,0.1789,0.0000,0.1714,0.0000,
    0.1648,0.0000,0.1589,0.0000,0.1536,
    0.0000,0.1487,0.0000,0.1443,0.0000,
    0.1403,0.0000,0.1366,0.0000,0.1332,
    0.0000,0.1300,0.0000,0.1270,0.0000,
    0.1243,0.0000,0.1217,0.0000,0.1192};

/*

double maps[ 100 ][ 100 ][ 100 ];
short  dxdy[ 4 ][ 2 ] = {1,0,0,1,-1,0,0,-1};

void madelist()    // 打表程序 
{
    for ( int n = 1 ; n < 100 ; n += 2 ) {
        double sum = 0.0;
        memset( maps, 0, sizeof( maps ) );
        maps[ 0 ][ n/2 ][ n/2 ] = 1.0;
        for ( int t = 0 ; t < n ; ++ t )
        for ( int i = 0 ; i < n ; ++ i )
        for ( int j = 0 ; j < n ; ++ j )
        for ( int k = 0 ; k < 4 ; ++ k ) {
            int x = i+dxdy[ k ][ 0 ];
            int y = j+dxdy[ k ][ 1 ];
            if ( x > 0 && x < n-1 && y > 0 && y < n-1 )
                maps[ t+1 ][ i ][ j ] += 0.25*maps[ t ][ x ][ y ];
            else if ( ( x == 0 && y == 0 ) || ( x == 0 && y == n-1 ) ||
                    ( x== n-1 && y == 0 ) || ( x == n-1 && y == n-1 ) )
                maps[ t+1 ][ i ][ j ] += 0.5*maps[ t ][ x ][ y ];
            else maps[ t+1 ][ i ][ j ] += 1.0/3*maps[ t ][ x ][ y ];
            if ( i == n/2 && j == t ) {
                sum += maps[ t+1 ][ i ][ j ];
                maps[ t+1 ][ i ][ j ] = 0.0;
            }
        }
        printf("%.4lf,",sum);
    }
}
*/

int main()
{
    int m;
    while ( ~scanf("%d",&m) ) 
        printf("%.4lf\n",answ[ m/2 ]);
    return 0;
}


zoj 2271 - Chance to Encounter a Girl