链接
https://ac.nowcoder.com/acm/contest/8688/D
题意
A A A 有 h p 1 hp1 hp1 血量, B B B 有 h p 2 hp2 hp2 血量。
每个回合 A A A 会发起一次攻击,每次攻击造成 w w w 伤害,有 p p p 概率击中 B B B, 1 − p 1-p 1−p 概率击中自己。
当有一个人血量少于等于 0 0 0 时,游戏结束。
求回合数的期望。
思路
记 d p [ i ] [ j ] dp[i][j] dp[i][j] 为 A A A 剩 1 1 1 血量 b b b 剩 j j j 血量的回合数期望。
当 i = 0 i=0 i=0 或 j = 0 j=0 j=0 时,期望必为 0 0 0。
倒推, d p [ h p 1 ] [ h p 2 ] dp[hp1][hp2] dp[hp1][hp2] 为答案。
代码
#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
//head
const int N=3005;
DB dp[N][N];
int main() {
//freopen("E:/OneDrive/IO/in.txt","r",stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin>>T;
while(T--) {
int hp1,hp2,w;
DB p;
cin>>hp1>>hp2>>w>>p;
for(int i=1;i<=hp1;i++) {
for(int j=1;j<=hp2;j++) {
dp[i][j]=(dp[max(0,i-w)][j]+1)*(1-p)+(dp[i][max(0,j-w)]+1)*p;
}
}
cout<<fixed<<setprecision(6)<<dp[hp1][hp2]<<'\n';
}
return 0;
}