POJ 1085 Triangle War(極大極小搜尋+alpha-beta剪枝)
阿新 • • 發佈:2019-01-30
// // main.cpp // Richard // // Created by 邵金傑 on 16/8/29. // Copyright © 2016年 邵金傑. All rights reserved. // #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int map[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 State=(1<<18)-1; int tri[9]={7,152,52,352,34304,3200,71680,12544,155648}; int get_States(int state,int seg,int &cnt) { int now=state|seg; for(int i=0;i<9;i++) { if((state&tri[i])!=tri[i]&&(now&tri[i])==tri[i]) cnt++; } return now; } int MinSearch(int state,int alpha,int a,int b); int MaxSearch(int state,int beta ,int a,int b); int MinSearch(int state,int alpha,int a,int b) { if(a>=5) return 1; if(b>=5) return -1; if(state==State) return a>b?1:-1; int ans=1; int reminds=(~state)&State; while(reminds) { int seg=reminds&(-reminds); int tb=b; int temp=get_States(state,seg,tb); int tp; if(tb>b) tp=MinSearch(temp,alpha,a,tb); else tp=MaxSearch(temp,ans,a,tb); ans=min(ans,tp); if(ans<=alpha) return ans; reminds-=seg; } return ans; } int MaxSearch(int state,int beta,int a,int b) { if(a>=5) return 1; if(b>=5) return -1; if(state==State) return a>b?1:-1; int ans=-1; int reminds=(~state)&State; while(reminds) { int seg=reminds&(-reminds); int ta=a; int temp=get_States(state,seg,ta); int tp; if(ta>a) tp=MaxSearch(temp,beta,ta,b); else tp=MinSearch(temp,ans,ta,b); ans=max(ans,tp); if(ans>=beta) return ans; reminds-=seg; } return ans; } int main() { int t,kase=0; scanf("%d",&t); while(t--) { int m; scanf("%d",&m); int x,y; int state=0; int a=0,b=0; int cnt=0; for(int i=0;i<m;i++) { scanf("%d%d",&x,&y); int ta=a,tb=b; state=get_States(state,1<<map[x][y],(cnt&1)?b:a); if(ta==a&&tb==b) cnt++; } int ans; if(cnt&1) ans=MinSearch(state,-1,a,b); else ans=MaxSearch(state,1,a,b); printf("Game %d: %c wins.\n",++kase,ans==1?'A':'B'); } return 0; }