Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2509 | Accepted: 984 |
Description
Input
Output
Sample Input
4 6 2 4 4 5 5 9 3 6 2 5 3 5 7 2 4 4 5 5 9 3 6 2 5 3 5 7 8 6 1 2 2 3 1 3 2 4 2 5 4 5 10 1 2 2 5 3 6 5 8 4 7 6 10 2 4 4 5 4 8 7 8
Sample Output
Game 1: B wins. Game 2: A wins. Game 3: A wins. Game 4: B wins.
极大极小搜索,用alph,beda剪枝,注意a取反的话,如果是正数,要用(~a)&all;这样才是正数,因为all前的0会与1相消!
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define mem(a,b) memset(a,b,sizeof(a)) int mat[11][11]={ {0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,2,3,4,0,0,0,0,0}, {0,1,2,0,0,5,6,0,0,0,0}, {0,0,3,0,0,7,0,9,10,0,0}, {0,0,4,5,7,0,8,0,11,12,0}, {0,0,0,6,0,8,0,0,0,13,14}, {0,0,0,0,9,0,0,0,15,0,0}, {0,0,0,0,10,11,0,15,0,16,0}, {0,0,0,0,0,12,13,0,16,0,17}, {0,0,0,0,0,0,14,0,0,17,0} }; int maxsg(int ,int,int ,int); int all=(1<<18)-1; int tri[9]={7,152,52,352,34304,3200,71680,12544,155648}; int gettri(int old,int sg,int & s){ int now=old|sg; for(int i=0;i<9;i++){ if((old&tri[i])!=tri[i]&&(now&tri[i])==tri[i])// s++; } return now; } int minsg(int state,int blph,int sa,int sb){ if(sa>=5)return 1; if(sb>=5)return -1; if(state==all)return sa>sb?1:-1; int left=(~state)&all; int ans=1,tmp; while(left){ int k=left&(-left); int ta=sa,tb=sb; int now=gettri(state,k,tb); if(tb>sb)tmp=minsg(now,blph,ta,tb); else tmp=maxsg(now,ans,ta,tb); if(tmp<ans)ans=tmp; if(tmp<=blph)return ans; left=left-k; } return ans; } int maxsg(int state,int alph,int sa,int sb){ if(sa>=5)return 1; if(sb>=5)return -1; if(state==all)return sa>sb?1:-1; int left=(~state)&all; int ans=-1,tmp; while(left){ int k=left&(-left); int ta=sa,tb=sb; int now=gettri(state,k,ta); if(ta>sa)tmp=maxsg(now,alph,ta,tb); else tmp=minsg(now,ans,ta,tb); if(tmp>ans)ans=tmp; if(tmp>=alph)return ans; left=left-k; } return ans; } int main() { int tcase,n,i,x,y,state,sa,sb,cnt,tt=1,ta,tb; scanf("%d",&tcase); while(tcase--){ scanf("%d",&n); state=0;sa=0,sb=0;cnt=1; for(i=0;i<n;i++){ scanf("%d%d",&x,&y); ta=sa,tb=sb; state=gettri(state,1<<mat[x][y],cnt&1?sa:sb); if(ta==sa&&tb==sb) cnt++; } int ans; if(cnt&1)ans=maxsg(state,1,sa,sb); else ans=minsg(state,-1,sa,sb); if(ans==1)printf("Game %d: A wins.\n",tt++); else printf("Game %d: B wins.\n",tt++); } return 0; }