zoukankan      html  css  js  c++  java
  • ZOJ 3822 Domination

    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
    ",DP(0,0,0));
        }
    }
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    android 的通知管理
    java 反射机制
    java基础知识梳理
    spring 知识梳理
    Orange's_1_win7下搭建环境
    编写安全代码:死循环
    我的kindle书单
    [更新Github地址]python学习,自己写了个简单聊天工具mychat
    给VIM和Terminal配色:Solarized
    Hive学习之路 (八)Hive中文乱码
  • 原文地址:https://www.cnblogs.com/hlmark/p/4025523.html
Copyright © 2011-2022 走看看