题目传送门
题意: 给你一个 n ∗ m n*m n∗m的距阵,你开始在 ( 1 , 1 ) (1,1) (1,1)点,你要走到 ( n , m ) (n,m) (n,m)点。你在每个点有三种可能性。
- 原地不动,可能性为 a [ i ] [ j ] [ 0 ] a[i][j][0] a[i][j][0]
- 向右走一步,可能性为 a [ i ] [ j ] [ 1 ] a[i][j][1] a[i][j][1]
- 向下走一步,可能性为 a [ i ] [ j ] [ 2 ] a[i][j][2] a[i][j][2]
保证输入的合法性,每次走一步花费的体力值为2,问你走到终点期望花费多少体力?
思路: 很容易想到
f
[
i
]
[
j
]
f[i][j]
f[i][j]表示当你在
(
i
,
j
)
(i,j)
(i,j)时,走到终点所需要体力的期望值。那么就有:
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]+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]+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]=1−a[i][j][0]a[i][j][1]∗f[i][j+1]+a[i][j][2]∗f[i+1][j]+2、
但是因为如果a[i][j][0]=1的时候,走到这个点就走不出去了,所以需要对这个点特判一下(
f
[
i
]
[
j
]
=
0
f[i][j]=0
f[i][j]=0,即不计算这个点的代价)。
代码:
#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;
}