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;
}


發佈了149 篇原創文章 · 獲贊 16 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章