Fairy Chess
题目分析
这道题主要结合了位运算、深度优先遍历(DFS)和模拟,需要分别处理六种棋子的行棋规则及其影响。
题目概述
在这道题中,共有六种棋子。Alice 和 Bob 一共有12个棋子,每种棋子各有两个,棋子的具体行棋规则如下:
- Rook: 可以沿着横向和纵向移动。
- Bishop: 可以沿着对角线移动。
- Queen: 结合了 Rook 和 Bishop 的移动能力。
- Knight: 可以进行L形的跳跃移动。
- Archbishop: 结合了 Knight 和 Bishop 的移动能力。
- Chancellor: 结合了 Rook 和 Knight 的移动能力。
- Maharaja: 拥有 Rook、Bishop 和 Knight 的所有移动能力。
游戏规则为:输入一行表示棋局的字符串,Alice 先手。游戏过程中,如果一方无法再下棋子,则该方失败,输出谁获得胜利。
代码实现
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define close ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef unsigned long long ull;
const string t = "RBQCAM";
array<array<ull, 64>, 6> hit;
string s;
array<int, 12> a;
inline bool Alice(int nums, ull vis, ull used);
inline bool Bob(int nums, ull vis, ull used);
inline int pos(int x, int y) {
return 8 * x + y;
}
inline bool Alice(int nums, ull vis, ull used) {
if (nums == 12) {
return 0;
}
for (int i = 0; i < 64; i++) {
if ((1ull << i) & used) {
continue;
}
ull who = hit[a[nums]][i];
if (vis & who) {
continue;
}
if (Bob(nums + 1, vis | (1ull << i), used | who)) {
return 1;
}
}
return 0;
}
inline bool Bob(int nums, ull vis, ull used) {
for (int i = 0; i < 64; i++) {
if ((1ull << i) & used) {
continue;
}
ull who = hit[a[nums]][i];
if (vis & who) {
continue;
}
if (!Alice(nums + 1, vis | (1ull << i), used | who)) {
return 0;
}
}
return 1;
}
void solved() {
cin >> s;
for (int i = 0; i < 12; i++) {
a[i] = t.find(s[i]);
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
ull p = 0;
int idx = pos(i, j);
for (int k = 0; k < 8; k++) {
hit[0][idx] |= 1ull << pos(i, k);
hit[0][idx] |= 1ull << pos(k, j);
}
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
if (x + y == i + j || x - y == i - j) {
hit[1][idx] |= 1ull << pos(x, y);
}
}
}
hit[2][idx] = hit[0][idx] | hit[1][idx];
for (int x = 0; x < 8; x++) {
for (int y = 0; y < 8; y++) {
int dx = i - x;
int dy = j - y;
if (dx * dx + dy * dy == 5) {
p |= 1ull << pos(x, y);
}
}
}
for (int k = 3; k <= 5; k++) {
hit[k][idx] = p | hit[k - 3][idx];
}
}
}
if (Alice(0, 0, 0)) {
cout << "Alice" << endl;
} else {
cout << "Bob" << endl;
}
}
signed main() {
close;
solved();
return 0;
}
代码分析
首先,将输入的字符串映射到一个数组中,表示每个棋子的类型。接下来,对于每个棋子类型,预处理出它落在每个位置时能够攻击到的所有点。
然后,开始模拟 Alice 先手的情况。在每一步中,首先判断当前选择的点是否已经被其他棋子攻击到了;其次,判断这个点是否已经被占用。通过深度优先遍历(DFS)模拟整个过程,最终根据题目要求输出结果。