Cacti Lottery--------ACM-ICPC 2018 徐州赛区网络预赛

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lpeaceminusone/article/details/82747457

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:

Sum Reward
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 100T≤100) 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∣(A−B)∣<1e−5 .

本题答案不唯一,符合要求的答案均正确

样例输入复制

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

样例输出复制

10000
4313.16666667
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 0x3f3f3f
using namespace std;
#define mod 1000000007
const int maxn=1000115;
map<int,pair<int,int> >m1,m2;
int m3[100];
int g[11][11];
int vis[11],sum[10];
int cnt1,cnt2,k=0;
double ans;
void init(){
m3[6]=10000;
m3[7]=  36;
m3[8]=	720;
m3[9]=	360;
m3[10]=	80;
m3[11]=	252;
m3[12]=	108;
m3[13]=	72;
m3[14]=	54;
m3[15]=	180;
m3[16]=	72;
m3[17]=	180;
m3[18]=	119;
m3[19]=	36;
m3[20]=	360;
m3[21]=	1080;
m3[22]=	144;
m3[23]=	1800;
m3[24]=	3600;
}
double A(int n,int m){
    double res=1.0;
    for(int i=1;i<=m;i++)
        res*=(n-i+1);
    return res;
}
void dfs1(int x){
    if(x>cnt2){
        int l=0;
        for(int i=0;i<3;i++){
           sum[l++]+=m3[(g[i][0]+g[i][1]+g[i][2])];
        }
        for(int j=0;j<3;j++){
          sum[l++]+=m3[(g[0][j]+g[1][j]+g[2][j])];
        }
        sum[l++]+=m3[(g[0][0]+g[1][1]+g[2][2])];
        sum[l++]+=m3[(g[0][2]+g[1][1]+g[2][0])];

        return ;
    }
    for(int i=1;i<=9;i++){
        if(vis[i]==1)continue;
        vis[i]=1;
        g[m2[x].first][m2[x].second]=i;
        dfs1(x+1);
        vis[i]=0;
    }
}
void dfs(int x){

    if(x>cnt1){
         mem(sum,0);
        dfs1(1);
        int maxn=0;
        for(int i=0;i<8;i++){
         maxn=max(maxn,sum[i]);
        }ans+=(double)maxn/A(cnt2,cnt2);
        return ;
    }
    for(int i=1;i<=9;i++){
        if(vis[i]==1)continue;
        vis[i]=1;
        g[m1[x].first][m1[x].second]=i;
        dfs(x+1);
        vis[i]=0;
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
            mem(vis,0);
            mem(g,0);
            init();
        char s[20];
        cnt1=cnt2=0;
        ans=0;
     for(int i=0;i<3;i++){
        scanf("%s",s);
        for(int j=0;j<strlen(s);j++){
            if(s[j]>='0'&&s[j]<='9'){vis[s[j]-'0']=1;g[i][j]=s[j]-'0';}
            if(s[j]=='*'){ m1[++cnt1]=make_pair(i,j);g[i][j]=-1;}
            if(s[j]=='#'){m2[++cnt2]=make_pair(i,j);g[i][j]=-1;}
            }
        }
        //cout<<cnt1<<" "<<cnt2<<endl;
        dfs(1);
        //cout<<ans<<endl;
        ans/=A(cnt1+cnt2,cnt1);
        printf("%.8lf\n",ans);
    }
}
扫描二维码关注公众号,回复: 3302410 查看本文章

猜你喜欢

转载自blog.csdn.net/lpeaceminusone/article/details/82747457