HDU 3853 LOOPS (expected dp)

Topic portal

Meaning of the title: give you a n ∗ mn*mnm matrix, you start at(1, 1) (1,1)(1,1 ) point, you have to go to(n, m) (n, m)(n,m ) point. You have three possibilities at each point.

  1. Stay in place, the possibility is a [i] [j] [0] a[i][j][0]a[i][j][0]
  2. Go one step to the right, the possibility is a [i] [j] [1] a[i][j][1]a[i][j][1]
  3. Go one step down, the possibility is a [i] [j] [2] a[i][j][2]a[i][j][2]

To ensure the legitimacy of the input, the stamina spent for each step is 2. How much stamina do you expect to spend to reach the end?

Idea: It is easy to think of f [i] [j] f[i][j]f [ i ] [ j ] means when you are at(i, j) (i, j)(i,j ) , the expected value of physical strength required to reach the end. Then there is:
f [i] [j] = a [i] [j] [0] ∗ f [i] [j] + a [i] [j] [1] ∗ f [i] [j + 1 ] + a [i] [j] [2] ∗ f [i + 1] [j] + 2 f[i][j]=a[i][j][0]*f[i][j] +a[i][j][1]*f[i][j+1]+a[i][j][2]*f[i+1][j]+2f[i][j]=a[i][j][0]f[i][j]+a[i][j][1]f[i][j+1]+a[i][j][2]f[i+1][j]+2,整理一下就有: f [ i ] [ j ] = a [ i ] [ j ] [ 1 ] ∗ f [ i ] [ j + 1 ] + a [ i ] [ j ] [ 2 ] ∗ f [ i + 1 ] [ j ] + 2 1 − a [ i ] [ j ] [ 0 ] f[i][j]=\frac{a[i][j][1]*f[i][j+1]+a[i][j][2]*f[i+1][j]+2}{1-a[i][j][0]} f[i][j]=1a[i][j][0]a[i][j][1]f[i][j+1]+a[i][j][2]f[i+1][j]+2,
But because if a[i][j][0]=1, you can’t go out at this point, so you need to make a special judgment on this point ( f [i] [j] = 0 f[i ][j]=0f[i][j]=0 , that is, the cost of this point is not calculated).

Code:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define lowbit(x) x&-x
#define pii pair<int,int>
#define ull unsigned long long
#define pdd pair<double,double>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(),(x).end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read()
{
    
    
    int x=0,f=1;
    char ch=gc();
    while(ch<'0'||ch>'9')
    {
    
    
        if(ch=='-')
            f=-1;
        ch=gc();
    }
    while(ch>='0'&&ch<='9')
    {
    
    
        x=x*10+ch-'0';
        ch=gc();
    }
    return x*f;
}
using namespace std;
const int N=2e5+666;
const int inf=0x3f3f3f3f;
const int mod=998244353;
const double eps=1e-7;
const double PI=acos(-1);

double a[1111][1111][3],f[1111][1111];
int solve()
{
    
    
    int n,m;
    while(scanf("%lld%lld",&n,&m)!=EOF)
    {
    
    
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                for(int k=0; k<3; k++)
                    scanf("%lf",&a[i][j][k]);
        memset(f,0,sizeof f);
        for(int i=n;i>=1;i--)
        {
    
    
            for(int j=m;j>=1;j--)
            {
    
    
                if((i==n&&j==m)||a[i][j][0]==1.0)
                    continue;
                f[i][j]=a[i][j][1]*f[i][j+1]+a[i][j][2]*f[i+1][j]+2;
                f[i][j]/=(1.0-a[i][j][0]);
            }
        }
        printf("%.3f\n",f[1][1]);
    }
}
signed main()
{
    
    
    solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/Joker_He/article/details/110746069