Pokemon Ultra Sun ###K //K
题目链接:https://ac.nowcoder.com/acm/problem/213446
题意:有2个玩家 每个玩家开始的血为hp1 hp2 每次有p的概率对玩家2造成w伤害,有1-p的概率对玩家1造成p点伤害
问有一个玩家生命值归0的期望回合是多少
思路: 期望dp dp[i][j] 代表 玩家1剩i血 玩家2剩j血时还需要多少回合
初始化dp[i][0] 和dp[0][j] 为0 然后一直顺推 直到dp[hp1][hp2] 注意从1 1 开始推, 别从0 0 开始更新了初始化的状态
注意这道题是一定要保留六位小数
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define pb push_back 5 const int maxn =1e6+10; 6 const int mod=1e9+7; 7 double dp[3010][3010]; 8 9 10 int main() 11 { 12 ios::sync_with_stdio(false); 13 cin.tie(0); 14 int t; 15 cin>>t; 16 while(t--) 17 { 18 int hp1,hp2,w; 19 double p; 20 cin>>hp1>>hp2>>w; 21 cin>>p; 22 for(int i=0;i<=hp1;i++) 23 dp[i][0]=0; 24 for(int i=0;i<=hp2;i++) 25 dp[0][i]=0; 26 for(int i=1;i<=hp1;i++) 27 { 28 for(int j=1;j<=hp2;j++) 29 { 30 dp[i][j]=dp[max(i-w,0)][j]*(1.0-p)+dp[i][max(j-w,0)]*(p)+1.0; 31 } 32 } 33 cout<<fixed<<setprecision(6)<<dp[hp1][hp2]<<'\n'; 34 } 35 36 37 38 39 }

浙公网安备 33010602011771号