3106: [cqoi2013]棋盘游戏
分析:
极大极小搜索 + 记忆化。
代码
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 5 const int INF = 1e9; 6 int f[2][62][22][22][22][22]; 7 int dx[8] = {0,0,1,-1,0,0,2,-2}; 8 int dy[8] = {1,-1,0,0,2,-2,0,0}; 9 int n; 10 11 int Minimax(int player,int step,int a,int b,int c,int d) { 12 if (step > n*3) return INF; 13 if (a==c && b==d) { 14 if (player) return INF; 15 return 0; 16 } 17 if (f[player][step][a][b][c][d]) return f[player][step][a][b][c][d]; 18 int res = 0,x = 0,y = 0; 19 if (player) { // 黑棋走 20 res = INF; 21 for (int i=0; i<8; ++i) { 22 x = c + dx[i], y = d + dy[i]; 23 if (x>=1 && x<=n && y>=1 && y<=n) res = min(res,Minimax(player^1,step+1,a,b,x,y)); 24 } 25 } 26 else { // 白棋走 27 for (int i=0; i<4; ++i) { 28 x = a + dx[i], y = b + dy[i]; 29 if (x>=1 && x<=n && y>=1 && y<=n) res = max(res,Minimax(player^1,step+1,x,y,c,d)); 30 } 31 } 32 res ++; 33 f[player][step][a][b][c][d] = res; 34 return res; 35 } 36 37 int main() { 38 int a,b,c,d; 39 cin >> n >> a >> b >> c >> d; 40 if (abs(a-c)+abs(b-d) == 1) puts("WHITE 1"); // 白子一步吃掉黑子 41 else printf("BLACK %d",Minimax(0,0,a,b,c,d)); 42 return 0; 43 }