标签:size 于平 csp bit c++ 评测 win max 包含
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pii pair<int,int> 4 map<string,int>f; 5 int blank(string str){ int res=0; for(char c:str)res+=(c==‘0‘);return res; } 6 int check(string str){ 7 for(int i=0;i<3;++i){ 8 if(str[i*3]==‘1‘&&str[i*3+1]==‘1‘&&str[i*3+2]==‘1‘) return 1; 9 if(str[i*3]==‘2‘&&str[i*3+1]==‘2‘&&str[i*3+2]==‘2‘) return 2; 10 if(str[i]==‘1‘&&str[i+3]==‘1‘&&str[i+6]==‘1‘) return 1; 11 if(str[i]==‘2‘&&str[i+3]==‘2‘&&str[i+6]==‘2‘) return 2; 12 } 13 if(str[0]==‘1‘&&str[4]==‘1‘&&str[8]==‘1‘|| 14 str[2]==‘1‘&&str[4]==‘1‘&&str[6]==‘1‘)return 1; 15 if(str[0]==‘2‘&&str[4]==‘2‘&&str[8]==‘2‘|| 16 str[2]==‘2‘&&str[4]==‘2‘&&str[6]==‘2‘)return 2; 17 return 0; 18 } 19 void dfs(string state,int cur){ 20 if(f.count(state))return; 21 //cout<<state<<endl; 22 int r=check(state); 23 int b=blank(state); 24 if(r==1){ 25 f[state]=-b-1; 26 }else if(r==2){ 27 f[state]=-b-1; 28 }else if(b==0){ 29 f[state]=0; 30 } 31 else{ 32 int win=-999,lose=-999,ping=-999; 33 for(int i=0;i<=8;++i){ 34 if(state[i]==‘0‘){ 35 if(cur==1){ 36 state[i]=‘1‘; 37 dfs(state,2); 38 }else if(cur==2){ 39 state[i]=‘2‘; 40 dfs(state,1); 41 } 42 if(f[state]<0){ 43 win=max(win,-f[state]); 44 }else if(f[state]>0){ 45 lose=max(lose,-f[state]); 46 }else ping=1; 47 state[i]=‘0‘; 48 } 49 } 50 if(win!=-999){ 51 f[state]=win; 52 }else if(ping!=-999)f[state]=0; 53 else f[state]=lose; 54 } 55 } 56 int main(){ 57 int T; 58 char ch; 59 string str; 60 cin>>T; 61 while(T--){ 62 str=""; 63 for(int i=1;i<=9;++i){ 64 cin>>ch; 65 str+=ch; 66 } 67 if(!f.count(str)){ 68 dfs(str,1); 69 }cout<<f[str]<<endl; 70 } 71 return 0; 72 }
标签:size 于平 csp bit c++ 评测 win max 包含
原文地址:https://www.cnblogs.com/zzqc/p/12420985.html