ZOJ-3822-Domination【概率dp】【2014牡丹江赛区】

ZOJ-3822-Domination

                Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge

Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What’s more, he bought a large decorative chessboard with N rows and M columns.

Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard was dominated by the chess pieces. That means there is at least one chess piece in every row. Also, there is at least one chess piece in every column.

“That’s interesting!” Edward said. He wants to know the expectation number of days to make an empty chessboard of N × M dominated. Please write a program to help him.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There are only two integers N and M (1 <= N, M <= 50).

Output

For each test case, output the expectation number of days.

Any solution with a relative or absolute error of at most 10-8 will be accepted.

Sample Input

2
1 3
2 2
Sample Output

3.000000000000
2.666666666667

题目链接:ZOJ 3822

题目大意:有一个n行n列的棋盘,每次可以放一个棋子,问把每行每列都至少有一个棋子的期望。

题目思路:概率dp,dp[i][j][k]。i表示放了i行,j表示放了j列,k代表用了k个棋子。
dp[i][j][k] = 原始状态的概率 * 选到当前这样状态的概率。
我们可以知道,每放一个棋子会出现4种情况,

一、没有增加一行一列,dp[i][j][k] += dp[i][j][k - 1] * (i * j - k + 1) / (sum - k + 1);
二、增加了一行,dp[i][j][k] += dp[i - 1][j][k - 1] * (n- i + 1) * j / (sum - k + 1);
三、增加了一列,dp[i][j][k] += dp[i][j - 1][k - 1] * (m - j + 1) * i / (sum - k + 1);
四、增加了一行一列 dp[i][j][k] += dp[i - 1][j - 1][k - 1] * (n - i + 1) * (m - j + 1) / (sum - k + 1);

例外需要注意,由于题目要求第k个棋子刚好填满n行m列,故概率为dp[n][m][k] - dp[n][m][k - 1];

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
double dp[60][60][3600] = {0};
int main(){
    int t;
    cin >> t;   
    while(t--)
    {
        int n,m;
        cin >> n >> m;
        int sum = n * m;
        for (int k = 0; k <= sum; k++)
            for (int i = 0; i <= n; i++)
                for (int j = 0; j <= m; j++)    dp[i][j][k] = 0;
        dp[0][0][0] = 1;
        for (int k = 1; k <= sum; k++)
        {
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= m; j++)
                {
                    double ret = sum - k + 1;
                    dp[i][j][k] += dp[i][j][k - 1] * (i * j - k + 1) / ret;
                    dp[i][j][k] += dp[i][j - 1][k - 1] * (m - j + 1) * i / ret;
                    dp[i][j][k] += dp[i - 1][j][k - 1] * (n- i + 1) * j / ret;
                    dp[i][j][k] += dp[i - 1][j - 1][k - 1] * (n - i + 1) * (m - j + 1) / ret;
                }
            }
        }
        double ans = 0;
        for (int k = 1; k <= sum; k++)
            ans += (dp[n][m][k] - dp[n][m][k - 1]) * k;
        printf("%.10f\n",ans);
    }
    return 0;
}

你可能感兴趣的:(dp)