Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4576 [CQOI2013]棋盘游戏

P4576 [CQOI2013]棋盘游戏

白色比较菜,胜利当且仅当开局第一步可以吃掉黑色。黑色考虑尽快赢,白色考虑最后输。对于所有决策取最优即可。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <cstdio>
#include <algorithm>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
if (c == '-') flag = true;
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
const int N = 21, M = 81;
const int dx[] = { 0, -1, 1, 0 }, dy[] = { 1, 0, 0, -1 };
int n, f[M][N][N][N][N];
bool inside (int x, int y) { return x > 0 && y > 0 && x <= n && y <= n; }
int dfs (int t, int x1, int y1, int x2, int y2)
{
if (t >= M || !inside(x1, y1) || !inside(x2, y2)) return t & 1 ? M : 0;
if (x1 == x2 && y1 == y2) return t & 1 ? 0 : M;
int &res = f[t][x1][y1][x2][y2];
if (res) return res;
res = t & 1 ? 0 : M;
if (t & 1)
for (int k = 0; k < 4; ++k)
res = max(res, dfs(t + 1, x1 + dx[k], y1 + dy[k], x2, y2));
else
for (int k = 0; k < 4; ++k)
for (int d = 1; d <= 2; ++d)
res = min(res, dfs(t + 1, x1, y1, x2 + dx[k] * d, y2 + dy[k] * d));
return ++res;
}
int main ()
{
int x1, y1, x2, y2;
read(n, x1, y1, x2, y2);
if (abs(x1 - x2) + abs(y1 - y2) == 1) return puts("WHITE 1"), 0;
printf("BLACK %d", dfs(1, x1, y1, x2, y2));
return 0;
}