Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@peienwu
Created May 20, 2022 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peienwu/26a640b8562fec4324a3e6ea4b28a77b to your computer and use it in GitHub Desktop.
Save peienwu/26a640b8562fec4324a3e6ea4b28a77b to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define ld long double
#define ll long long int
#define IOS ios::sync_with_stdio(0),cout.tie(0)
#define pii pair<int,int>
#define ff first
#define ss second
using namespace std;
char table[10][10];
int n = 3;
//H->健康、Q->隔離、X->確診
fstream f;
void init(){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
table[i][j] = 'H';
}
}
}
bool check(){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(table[i][j] == 'H')return 0;
}
}
return 1;
}
int get_pos(int s){
int t = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
if(s % 2){table[i][j] = 'X';t++;}
s = s >> 1;
}
}
return t;
}
void print(int s){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
f<<table[i][j]<<" ";
}
f<<endl;
}
f << s <<endl<<endl;
}
void setup(pii pos){
bool f = 0;
if(table[pos.ff][pos.ss] == 'X')return;
for(int i = max(pos.ff-1,1);i <= min(pos.ff+1,n);i++){
for(int j = max(pos.ss-1,1);j <= min(pos.ss+1,n);j++){
if(table[i][j] == 'X'){f = 1;break;}
}
}
if(f == 1)table[pos.ff][pos.ss] = 'Q';
return;
}
ld C(int np,int mp){
ld ans = 1;
int t = min(mp,np-mp);
for(int i = np;i > np-t;i--)ans *= i;
for(int i = 1;i <= t;i++)ans /= i;
return ans;
}
int main(){
IOS;
init();
f.open("/Users/wupeien/Desktop/高二下學期資料夾/九宮格機率/6pos.txt",ios::out|ios::trunc);
int s = 0,total = 0;
ll m = pow(2,n*n);
cout<<m<<endl;
int test[40];fill(test,test+40,0);
//s->狀態壓縮,m->總共次數2^36
for(int i = 0;i < m;i++){
init();
int t = get_pos(s);
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
setup({i,j});
}
}
if(check()){test[t]++;total++;}
print(s);
if(i % 10000 == 0)cout<<"Progress: "<<(ld)100*i/m<<"%"<<endl;
s++;
}
ld p = 0.0;
for(int i = 1;i <= (n*n);i++)p += i * (ld)test[i];
for(int i = 1;i <= (n*n);i++){
cout<<i<<": "<<test[i]<<endl;
cout<<C(n*n,i)<<endl<<endl;
}
p /= (ld)total;
cout<<"全班隔離人數期望值:"<<p<<endl;
f << endl;
for(int i = 1;i <= (n*n);i++){f << test[i]<<" ";}
f << endl;
for(int i = 1;i <= (n*n);i++){f << C(n*n,i)<<" ";}
f << endl;
f << p << endl;
for(int i = 1;i <= (n*n);i++){
cout<<test[i]<<" ";
}
cout<<endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment