题解:P17072 [ICPC 2017 Shenyang R] Legends of the Three Kingdoms
Grammar_hbw · · 题解
注意到获胜概率只和当前血量以及由谁进攻有关,考虑 dp。
记
初始情况就是题目中规定的几种获胜情况,此时获胜阵营的概率为
先预处理
:::info[Code Time]
#include <bits/stdc++.h>
using namespace std;
const int N=40;
double p[N*N*N*N*4+1][3];
bool vis[N*N*N*N*4+1];
const int b[4]={4*N*N*N,4*N*N,4*N,4},g[4]={0,1,0,2};
inline int f(int h0,int h1,int h2,int h3,int x){return x+h3*b[3]+h2*b[2]+h1*b[1]+h0*b[0];} //把所有 h 状压,方便处理。
const int hbw=f(N-1,N-1,N-1,N-1,3),qwq=hbw/4;
int main(){
cin.tie(0)->sync_with_stdio(0);
for(int i=1;i<=qwq;i++){
for(int x=7;~x;x--){ // x 这一维扫两遍,用 vis 标记已经有的。
int u=x%4;
int h[4],v=i*4+u;
if(vis[v]) continue;
for(int j=0;j<4;j++) h[j]=(v/b[j])%N;
if(!h[0]){
if(h[3]&&(!h[1]&&!h[2])) p[v][2]=1,p[v][0]=p[v][1]=0,vis[v]=1;
else p[v][1]=1,p[v][0]=p[v][2]=0,vis[v]=1;
}else if(!h[1]&&!h[3]) {p[v][0]=1,p[v][1]=p[v][2]=0,vis[v]=1;}
else if(!h[u]){if(vis[i*4+((u+1)%4)])p[v][0]=p[i*4+((u+1)%4)][0],p[v][1]=p[i*4+((u+1)%4)][1],p[v][2]=p[i*4+((u+1)%4)][2],vis[v]=1;} //处理初始情况以及当前玩家已阵亡的情况。
else{
vector<int> res; //储存所有可以转移到的状态。
double mx=-1;
for(int s=0;s<4;s++){ //要攻击谁?
if(g[s]==g[u]) continue;
if(!h[s]) continue; //不能攻击死人。
int to=(v-b[s])/4*4+((u+1)%4);
if(p[to][g[u]]>mx) mx=p[to][g[u]],res.clear();
if(p[to][g[u]]==mx) res.push_back(to);
}
int c=res.size();
for(int s=0;s<3;s++){
for(auto j:res) p[v][s]+=p[j][s];
p[v][s]/=c;
}
vis[v]=1;
}
}
}
int t;
cin>>t;
while(t-->0){
int h1,h2,h3,h4;
cin>>h1>>h2>>h3>>h4;
int res=f(h1,h3,h2,h4,0); //注意行动顺序和输入顺序不一致。
printf("%.6lf %.6lf %.6lf\n",p[res][0],p[res][1],p[res][2]); //保留 6 位小数。
}
return 0;
}
:::