1. 程式人生 > >2018 徐州網絡賽 C

2018 徐州網絡賽 C

ons swe ini als oid play sizeof nio 題目

  • 262144K

Morgana is playing a game called cacti lottery. In this game, morgana has a 3 \times 33×3 grid graph, and the graph is filled with 11 ~ 99 , each number appears only once. The game is interesting, he doesn‘t know some numbers, but there are also some numbers he knows but don‘t want to tell you.

Now he should choose three grids, these three grids should be in the same column or in the same row or in the diagonal line. Only after this choice, can he know all numbers in the grid graph. Then he sums the three numbers in the grids he chooses, get the reward as follows:

SumReward
6 10000
7 36
8 720
9 360
10 80
11 252
12 108
13 72
14 54
15 180
16 72
17 180
18 119
19 36
20 360
21 1080
22 144
23 1800
24 3600

Then he wants you to predict the expected reward he will get if he is clever enough in the condition that he doesn‘t want to tell you something he knows.

("He is clever enough" means he will choose the max expected reward row or column or dianonal in the condition that he knows some numbers. And you know that he knows some number, but you don‘t know what they are exactly. So you should predict his expected reward in your opinion. )

Input

First line contains one integers TT (T \le 100T100) represents the number of test cases.

Then each cases contains three lines, giving the 3 \times 33×3 grid graph. ‘*‘ means Morgana knows but doesn‘t want to tell you, ‘#‘ means Morgana doesn‘t know, ‘0‘ ~ ‘9‘ means the public number that Morgana and you both know.

Output

TT lines, output the answer. If the answer is AA, and your answer is BB. Your answer will be considered as correct if and only if |(A-B)| < 1e-5(AB)<1e5 .

本題答案不唯一,符合要求的答案均正確

樣例輸入

2
123
***
###
12*
45#
78#

樣例輸出

10000
4313.16666667

題目來源

ACM-ICPC 2018 徐州賽區網絡預賽

枚舉 * 的情況,對於每種情況搜索 # 的情況,答案和的平均值即為期望。

代碼如下:

技術分享圖片
#include <bits/stdc++.h>
using namespace std;
const int maxn=25;
typedef long long ll;
int score[25]={0,0,0,0,0,0,10000,36,720,360,80,252,108,72,54,180,72,180,119,36,360,1080,144,1800,3600};
int G[8][3]={
    {0,1,2},{3,4,5},{6,7,8},{0,3,6},{1,4,7},{2,5,8},{0,4,8},{2,4,6}
};
char mp[4][4];
int fac[maxn],p[maxn],g[maxn],pn=0,pg[maxn],pgg[maxn];
bool vis[maxn];
int cnt,q;
double res[maxn]; 
double ans;
void check(){
    for (int i=0; i<8; i++) res[i]=0;
    int a=0,b=0,c=0;
    for (int i=1; i<=9; i++){
        if(vis[i]) continue;
        pgg[c++]=i;
    }
    do{
        a=0,b=0;
        for (int i=0; i<pn; i++){
            if(p[i]>0) g[i]=p[i];
            else if(p[i]==0) g[i]=pg[a++];
            else g[i]=pgg[b++]; 
        }
        for (int d,i=0; i<8; i++){
            d=0;
            for (int j=0; j<3; j++) d+=g[G[i][j]];
            res[i]+=score[d];
        }
    }while(next_permutation(pgg,pgg+c));
    double d=0;
    for (int i=0; i<8; i++){
        res[i]=1.0*res[i]/fac[c];
        d=max(d,res[i]);
    }
    ans+=d;
}
void dfs(int pos){
    if(pos==cnt){
        q++;
        check();
        return;
    }
    for (int i=1; i<=9; i++){
        if(vis[i]) continue;
        vis[i]=1;
        pg[pos]=i;
        dfs(pos+1);
        vis[i]=false;
    }
}
int main(){
    // freopen("in.txt","r",stdin);
    fac[0]=1;
    for (int i=1; i<=9; i++) 
        fac[i]=fac[i-1]*i;
    int _;
    for (scanf("%d",&_); _; _--){
        cnt=q=0,ans=0;
        memset(vis,false,sizeof(vis));
        pn=0;
        for (int i=0; i<3; i++){
            scanf("%s",mp[i]);
            for (int j=0; j<3; j++){
                if(mp[i][j]==*) cnt++,p[pn++]=0;
                else if(mp[i][j]==#) p[pn++]=-1;
                else p[pn++]=mp[i][j]-0,vis[mp[i][j]-0]=1; 
            }
        }
        dfs(0);
        ans=1.0*ans/q;
        printf("%.7f\n",ans);
    }     
    return 0;
}
View Code

2018 徐州網絡賽 C