BZOJ 3106: [cqoi2013]棋盘游戏(对抗搜索)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3106

对抗搜索,f[x][y][a][b][c][d]表示当前谁走,走了几步,及位置。

(因为脑残+手残+眼拙写了一坨if还瞪了好久。。。最后还是这种做法靠谱。。。

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#define maxn 109
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define ll long long
#define inf int(1e9)
using namespace std;
int dx[8]={1,0,-1,0,2,0,-2,0};
int dy[8]={0,1,0,-1,0,2,0,-2};
int f[2][69][21][21][21][21];
int n,a,b,c,d; 
int read(){
    int x=0,f=1; char ch=getchar();
    while (!isdigit(ch)) {
        if (ch=='-') f=-1; ch=getchar();
    }
    while (isdigit(ch)){
        x=x*10+ch-'0'; ch=getchar();
    }
    return x*f;
}
int dfs(int x,int y,int a,int b,int c,int d){
    if (y>3*n) return inf;
    if (a==c&&b==d) {
        if (x) return inf;
        return 0;
    }
    if (f[x][y][a][b][c][d]) return f[x][y][a][b][c][d];
    int ans=0,xx=0,yy=0;
    if (x){
        ans=inf;
        rep(j,0,7) {
            xx=c+dx[j]; yy=d+dy[j];
            if (1<=xx&&xx<=n&&1<=yy&&yy<=n) ans=min(ans,dfs(0,y+1,a,b,xx,yy));
        }
    }   
    else {
        rep(j,0,3){
            xx=a+dx[j]; yy=b+dy[j];
            if (1<=xx&&xx<=n&&1<=yy&&yy<=n) ans=max(ans,dfs(1,y+1,xx,yy,c,d));
        }
    }
    ans++;
    f[x][y][a][b][c][d]=ans;
    return f[x][y][a][b][c][d]; 
}
int main(){
    n=read(); a=read(); b=read(); c=read(); d=read();
    if (abs(a-c)+abs(b-d)==1) puts("WHITE 1");
    else printf("BLACK %d",dfs(0,0,a,b,c,d));
    return 0;
}

 

posted on 2015-11-25 20:19  ctlchild  阅读(745)  评论(0编辑  收藏  举报

导航