cf 398B. Painting The Wall【期望dp】

传送门:http://codeforces.com/problemset/problem/398/B

Description:

User ainta decided to paint a wall. The wall consists of n2 tiles, that are arranged in an n × n table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.

  1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2.
  2. User ainta choose any tile on the wall with uniform probability.
  3. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it.
  4. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1.
 

However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all.

中文:涂墙壁游戏,要求每row每colum都至少一瓦被paint。


Analyse:

期望dp:从终点状态,往前面推;

dp(i,j)=(dp(i,j)+1)*p1+(dp(i-1,j)+1)*p2+(dp(i,j-1)+1)*p3+(dp(i-1,j-1)+1)*p4;

然后移项,dp(i,j)=.........详见CODE。=_=|


CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 0x7fffffff
#define SUP 0x80000000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=2007;

set<int> vr,vc;
double dp[N][N],p;

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        vr.clear(),vc.clear();
        p=1.0/n/n;
        int r,c;
        for(int i=0;i<m;i++){
            scanf("%d%d",&r,&c);
            if(!vr.count(r)) vr.insert(r);
            if(!vc.count(c)) vc.insert(c);
        }

        r=vr.size();
        c=vc.size();
        dp[0][0]=0;
        for(int i=0;i<=n-r;i++){
            for(int j=0;j<=n-c;j++){
                double tmp=0;
                if(i==0&&j==0) continue;
                tmp+=p*(n-i)*(n-j);
                dp[i][j]=1-tmp;
                if(i>0) tmp+=(dp[i-1][j]+1)*p*(n-j)*i;
                if(j>0) tmp+=(dp[i][j-1]+1)*p*(n-i)*j;
                if(i>0&&j>0) tmp+=(dp[i-1][j-1]+1)*p*i*j;
                dp[i][j]=tmp/dp[i][j];

            }
        }

        printf("%.10f\n",dp[n-r][n-c]);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值