f[X][Y][A][B][C][D]代表白在A,B;黑在C,D时走了Y步。如果X是0代表这一步改白走。
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define INF 0x3fffffff
int f[2][61][22][22][22][22];
int n,a,b,c,d,i,j,k;
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;
if(x>0)
{
ans=INF;
if(c > 1)
ans=min(ans,dfs(0,y+1,a,b,c-1,d));
if(c > 2)
ans=min(ans,dfs(0,y+1,a,b,c-2,d));
if(c < n)
ans=min(ans,dfs(0,y+1,a,b,c+1,d));
if(c < n-1)
ans=min(ans,dfs(0,y+1,a,b,c+2,d));
if(d > 1)
ans=min(ans,dfs(0,y+1,a,b,c,d-1));
if(d > 2)
ans=min(ans,dfs(0,y+1,a,b,c,d-2));
if(d < n)
ans=min(ans,dfs(0,y+1,a,b,c,d+1));
if(d < n-1)
ans=min(ans,dfs(0,y+1,a,b,c,d+2));
}
else
{
if(a > 1)
ans=max(ans,dfs(1,y+1,a-1,b,c,d));
if(a < n)
ans=max(ans,dfs(1,y+1,a+1,b,c,d));
if(b > 1)
ans=max(ans,dfs(1,y+1,a,b-1,c,d));
if(b < n)
ans=max(ans,dfs(1,y+1,a,b+1,c,d));
}
++ans;
f[x][y][a][b][c][d]=ans;
return ans;
}
int main()
{
scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);
if(abs(a-c)+abs(b-d)<=1)
printf("WHITE 1\n");
else
printf("BLACK %d\n",dfs(0,0,a,b,c,d));
return 0;
}