TLE
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int n,g[2][61][21][21][21][21];
int dx[9] = {0,1,-1,0,0,2,-2,0,0};
int dy[9] = {0,0,0,1,-1,0,0,2,-2};
struct node{int x1,y1,x2,y2,s;bool f;};
int ab(int x1,int y1,int x2,int y2,int al,int be,int s,bool f)
{
if(s > 3*n) return 1e9;
if(x1 == x2 && y1 == y2) return f ? s : 1e9;
if(f)
{
for (int i = 1;i <= 4;i++)
{
int nx = x1+dx[i],ny = y1+dy[i];
if(nx < 1 || ny < 1 || nx > n || ny > n) continue;
al = max(al,ab(nx,ny,x2,y2,al,be,s+1,!f));
if(al >= be) break;
}
}
else
{
for (int i = 1;i <= 8;i++)
{
int nx = x2+dx[i],ny = y2+dy[i];
if(nx < 1 || ny < 1 || nx > n || ny > n) continue;
be = min(be,ab(x1,y1,nx,ny,al,be,s+1,!f));
if(al >= be) break;
}
}
return f ? al : be;
}
int main()
{
int r1,c1,r2,c2;cin >> n >> r1 >> c1 >> r2 >> c2;
if(abs(r1-r2)+abs(c1-c2) == 1) cout << "WHITE 1" << '\n';
else
{
memset(g,-1,sizeof(g));
cout << "BLACK " << ab(r1,c1,r2,c2,-1e9,1e9,0,1);
}
return 0;
}
WA
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int n,g[2][61][21][21][21][21];
int dx[9] = {0,1,-1,0,0,2,-2,0,0};
int dy[9] = {0,0,0,1,-1,0,0,2,-2};
struct node{int x1,y1,x2,y2,s;bool f;};
int ab(int x1,int y1,int x2,int y2,int al,int be,int s,bool f)
{
if(s > 3*n) return 1e9;
if(x1 == x2 && y1 == y2) return f ? s : 1e9;
if(~g[f][s][x1][y1][x2][y2]) return g[f][s][x1][y1][x2][y2];
if(f)
{
for (int i = 1;i <= 4;i++)
{
int nx = x1+dx[i],ny = y1+dy[i];
if(nx < 1 || ny < 1 || nx > n || ny > n) continue;
al = max(al,ab(nx,ny,x2,y2,al,be,s+1,!f));
if(al >= be) break;
}
}
else
{
for (int i = 1;i <= 8;i++)
{
int nx = x2+dx[i],ny = y2+dy[i];
if(nx < 1 || ny < 1 || nx > n || ny > n) continue;
be = min(be,ab(x1,y1,nx,ny,al,be,s+1,!f));
if(al >= be) break;
}
}
return g[f][s][x1][y1][x2][y2] = (f ? al : be);
}
int main()
{
int r1,c1,r2,c2;cin >> n >> r1 >> c1 >> r2 >> c2;
if(abs(r1-r2)+abs(c1-c2) == 1) cout << "WHITE 1" << '\n';
else
{
memset(g,-1,sizeof(g));
cout << "BLACK " << ab(r1,c1,r2,c2,-1e9,1e9,0,1);
}
return 0;
}