//Jakub Nowak XIV LO Wroclaw
#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define bit(x) __builtin_popcount(x)
const int N = 20;
int n, m;//1<=n,m<=8, 1<=k<=nm-1; k<=8
ld S=0;
bool T1[N][N], T2[N][N];
ld DP[N][2^10][20];//DP[kolumna][maska][ile]
bool mozna() {
int c1=0, c2=0;
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) {
if((i+j)%2) continue;
c1+=T1[i][j];
c2+=T2[i][j];
}
return c1==c2;
}
int pot(int a, int b) {
int w=1;
for(int i=1; i<=b; i++) w*=a;
return w;
}
int ile_dodac(int mask, int mask2) {
int w=4*bit(mask);
for(int i=1; i<pot(2,(n-1)); i*=2) w-=2*(bool(mask&i)&&bool(mask&(2*i)));
w-=2*bit(mask&mask2);
return w;
}
void policz_S() {
DP[0][0][0]=0;
for(int kolumna=1; kolumna<=m; kolumna++) {
for(int mask=0; mask<pot(2,n); mask++) {
for(int mask2=0; mask2<pot(2,n); mask2++) {
for(int ile=0; ile<=8; ile++) {
if(ile+bit(mask)>8) continue;
DP[kolumna][mask][ile+bit(mask)]+=DP[kolumna-1][mask2][ile]+ile_dodac(mask, mask2);
}
}
}
}
int l_O=0;
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) l_O+=T2[i][j];
for(int mask=0; mask<pot(2,n); mask++) {
S+=DP[m][mask][l_O];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) {
char c;
cin>>c;
T1[i][j]=(c=='O');
}
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) {
char c;
cin>>c;
T2[i][j]=(c=='O');
}
if(!mozna()) {
cout<<0;
return 0;
}
policz_S();
long long moz=0;
for(int i=1; i<=n; i++) for(int j=1; j<m; j++) {
moz+=(T2[i][j]^T2[i][j+1]);
}
for(int i=1; i<n; i++) for(int j=1; j<=m; j++) {
moz+=(T2[i][j]^T2[i+1][j]);
}
ld wynik=moz;
wynik/=S;
cout<<fixed<<setprecision(15)<<wynik;
//cout<<"\n"<<wynik;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | //Jakub Nowak XIV LO Wroclaw #include<bits/stdc++.h> using namespace std; #define ld long double #define bit(x) __builtin_popcount(x) const int N = 20; int n, m;//1<=n,m<=8, 1<=k<=nm-1; k<=8 ld S=0; bool T1[N][N], T2[N][N]; ld DP[N][2^10][20];//DP[kolumna][maska][ile] bool mozna() { int c1=0, c2=0; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { if((i+j)%2) continue; c1+=T1[i][j]; c2+=T2[i][j]; } return c1==c2; } int pot(int a, int b) { int w=1; for(int i=1; i<=b; i++) w*=a; return w; } int ile_dodac(int mask, int mask2) { int w=4*bit(mask); for(int i=1; i<pot(2,(n-1)); i*=2) w-=2*(bool(mask&i)&&bool(mask&(2*i))); w-=2*bit(mask&mask2); return w; } void policz_S() { DP[0][0][0]=0; for(int kolumna=1; kolumna<=m; kolumna++) { for(int mask=0; mask<pot(2,n); mask++) { for(int mask2=0; mask2<pot(2,n); mask2++) { for(int ile=0; ile<=8; ile++) { if(ile+bit(mask)>8) continue; DP[kolumna][mask][ile+bit(mask)]+=DP[kolumna-1][mask2][ile]+ile_dodac(mask, mask2); } } } } int l_O=0; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) l_O+=T2[i][j]; for(int mask=0; mask<pot(2,n); mask++) { S+=DP[m][mask][l_O]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>m; for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { char c; cin>>c; T1[i][j]=(c=='O'); } for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) { char c; cin>>c; T2[i][j]=(c=='O'); } if(!mozna()) { cout<<0; return 0; } policz_S(); long long moz=0; for(int i=1; i<=n; i++) for(int j=1; j<m; j++) { moz+=(T2[i][j]^T2[i][j+1]); } for(int i=1; i<n; i++) for(int j=1; j<=m; j++) { moz+=(T2[i][j]^T2[i+1][j]); } ld wynik=moz; wynik/=S; cout<<fixed<<setprecision(15)<<wynik; //cout<<"\n"<<wynik; } |
English