题解:P17072 [ICPC 2017 Shenyang R] Legends of the Three Kingdoms

· · 题解

注意到获胜概率只和当前血量以及由谁进攻有关,考虑 dp。

记 p_{h_0,h_1,h_2,h_3,x,y} 表示当前由第 x 人行动、四人的血量分别为 h_0,h_1,h_2,h_3 时第 y 阵营获胜的概率。转移时考虑分别把不是自己阵营的 h 减一,这就是所有能到达的状态,从所有让自己阵营概率最大的方案转移。注意死人不能攻击以及不能攻击死人。当 h_x 为 0 时直接从 p_{h_0,h_1,h_2,h_3,x+1 \bmod 4,y} 转移即可,会有环,x 这维从大到小走两次即可,具体见代码。

初始情况就是题目中规定的几种获胜情况,此时获胜阵营的概率为 1,其他为 0。

先预处理 p 数组,然后每次询问可以 O(1) 回答,总时间复杂度 O(V^nnm+T),其中 V,n,m 分别为血量值域、人数、阵营数。

:::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;
}

:::