码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3822 Domination

时间:2014-10-15 03:26:35      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   strong   sp   

 

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 Mcolumns.

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 <= NM <= 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

Author: JIANG, Kai
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

 

 

 

这条题目是14年牡丹江现场赛的一条期望DP。

给出了一个 N*M 的矩阵 , 问到达每一行且每一列都存在棋子的状态的期望 。 

训练的时候这条题是我和小邪一起想的 。 

一开始我很快就想到了 设一个状态 dp[i][j][k] 表示已经有i行有棋子 , j列有棋子 , 已经用了k只棋子。

然后其意义是距离目标 (每一行且每一列都存在棋子)的期望。

 

那么要想出一个转移方程 。

那么这个状态能够转移到的状态就无非4个

dp[i+1][j+1][k+1] , dp[i+1][j][k+1] , dp[i][j+1][k+1],dp[i][j][k+1] .

然后边界和公式都是小邪推的。期望DP写得少的我一开始还是傻乎乎的 。 

其实真的不难,就是一些边界要注意 。

那么对于

 

i*m + j*n - i*j 表示已经覆盖行列的面积 , 那么除了这部分面积以外的部分 ( 即  n*m - ( i*m + j*n - i*j )  ),

可以通过加一只棋推到状态 .   dp[i][j][k] =( n*m - ( i*m + j*n - i*j )  / ( n*m - k)  ) * dp[i+1][j+1][k+1]  ;

 

在已经被覆盖的面积处在某些地方放一只棋子只能使得行数加 1 , 即在列已被覆盖的点上找行未被覆盖的点的数量 ,

画出图后发现 , 对每一列的点 , 除了与已覆盖的行的交点之外都符合 , 即 ( j * n - i * j )  

那么  dp[i][j][k] = ( j * n - i *j )  /( n*m - k)   * dp[i+1][j][k+1] ;

 

那么上述的行只能加1也能推广到列只能加 1 ,即

dp[i][j][k] = ( i * m - i *j  )  / ( n*m - k)   *dp[i][j+1][k+1]  ;

 

最后就是一些 行列都不能加1的点 有多少个呐 。。  无疑就是 i*j - k 个了 。。 

因为加入一只棋 , 必然占领一行一列 , 那么它必然在已被占领的行列的交点当中 。 即

dp[i][j][k] = ( i *j - k )  / ( n*m - k)  * dp[i][j][k+1] ;

 

那么每一个条件能进入的边界都是 分子大于 0 分母大于 0 吧 ~  并且  i < n ,j < m 

最后 dp[i][j][k] ++ ;

当 i >= m && j >=n 的时候达到目标 返回 0

 

 

#include <bits/stdc++.h>
using namespace std;
const int N = 55 ;
bool vis[N][N][N*N];
double dp[N][N][N*N];
int n , m ;
double DP ( int i , int j , int k )
{
    if( i >= n && j >= m ) return 0.0 ;

    if( !vis[i][j][k] ){
        vis[i][j][k] = true ;
        double ans = 0.0 ;
        if( i < n && j < m && ( n*m - ( i*m + j*n - i*j ) ) > 0 ){
            ans +=DP( i + 1 , j + 1 , k + 1 )*( n*m - ( i*m + j*n - i*j ) ) / ( n*m - k) ;
        }
        if( i < n &&( j*n - i*j )  > 0  ){
            ans += DP( i + 1 ,j , k + 1 )*( j*n - i*j )/( n*m - k) ;
        }
        if( j < m &&( i*m - i*j ) > 0  ){
            ans += DP( i ,j + 1 , k + 1 )*( i*m - i*j )/( n*m - k );
        }
        if( i * j - k  > 0 ){
            ans += DP( i , j , k + 1 ) *  ( i * j - k ) /( n*m - k );
        }
        ans += 1.0 ;
        dp[i][j][k] = ans ;
    }
    return dp[i][j][k];
}
int main()
{
    int _;
    cin >> _ ;
    while( _-- ){
        memset( vis , false , sizeof vis ) ;
        cin >> n >> m ;
        printf("%.10lf\n",DP(0,0,0));
    }
}

 

ZOJ 3822 Domination

标签:style   blog   color   io   os   ar   for   strong   sp   

原文地址:http://www.cnblogs.com/hlmark/p/4025523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!