QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#230182 | #7635. Fairy Chess | ucup-team1191# | WA | 70ms | 15160kb | C++23 | 5.0kb | 2023-10-28 17:49:07 | 2023-10-28 17:49:08 |
Judging History
answer
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
enum class Piece {
BISHOP,
ROOK,
QUEEN,
ARCHBISHOP,
CHANCELLOR,
MAHARAJAH,
KNIGHT,
};
const int N = 8;
const int K = 6;
ull mask_cover[N][N][K + 1];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
auto valid = [&](int x, int y) { return 0 <= x && x < N && 0 <= y && y < N; };
auto add = [&](int i, int j, int x, int y, Piece k) {
if (valid(x, y)) {
mask_cover[i][j][(int)k] |= 1ULL << (x * N + y);
}
};
{ // knight
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
add(i, j, i, j, Piece::KNIGHT);
add(i, j, i + 1, j + 2, Piece::KNIGHT);
add(i, j, i + 1, j - 2, Piece::KNIGHT);
add(i, j, i - 1, j + 2, Piece::KNIGHT);
add(i, j, i - 1, j - 2, Piece::KNIGHT);
add(i, j, i + 2, j + 1, Piece::KNIGHT);
add(i, j, i + 2, j - 1, Piece::KNIGHT);
add(i, j, i - 2, j + 1, Piece::KNIGHT);
add(i, j, i - 2, j - 1, Piece::KNIGHT);
}
}
}
{ // bishop
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int t = 0; t < 3 * N; ++t) {
add(i, j, i + t, j + t, Piece::BISHOP);
add(i, j, i + t, j - t, Piece::BISHOP);
add(i, j, i - t, j + t, Piece::BISHOP);
add(i, j, i - t, j - t, Piece::BISHOP);
}
}
}
}
{ // rook
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
for (int t = 0; t < 3 * N; ++t) {
add(i, j, i + t, j, Piece::ROOK);
add(i, j, i - t, j, Piece::ROOK);
add(i, j, i, j + t, Piece::ROOK);
add(i, j, i, j - t, Piece::ROOK);
}
}
}
}
{ // queen
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
mask_cover[i][j][(int)Piece::QUEEN] =
mask_cover[i][j][(int)Piece::BISHOP] |
mask_cover[i][j][(int)Piece::ROOK];
}
}
}
{ // archibishop
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
mask_cover[i][j][(int)Piece::ARCHBISHOP] =
mask_cover[i][j][(int)Piece::BISHOP] |
mask_cover[i][j][(int)Piece::KNIGHT];
}
}
}
{ // chancellor
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
mask_cover[i][j][(int)Piece::CHANCELLOR] =
mask_cover[i][j][(int)Piece::ROOK] |
mask_cover[i][j][(int)Piece::KNIGHT];
}
}
}
{ // maharajah
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
mask_cover[i][j][(int)Piece::MAHARAJAH] =
mask_cover[i][j][(int)Piece::QUEEN] |
mask_cover[i][j][(int)Piece::KNIGHT];
}
}
}
vector<int> order(2 * K);
for (int i = 0; i < 2 * K; ++i) {
char c;
cin >> c;
if (c == 'B') {
order[i] = (int)Piece::BISHOP;
} else if (c == 'R') {
order[i] = (int)Piece::ROOK;
} else if (c == 'Q') {
order[i] = (int)Piece::QUEEN;
} else if (c == 'A') {
order[i] = (int)Piece::ARCHBISHOP;
} else if (c == 'C') {
order[i] = (int)Piece::CHANCELLOR;
} else if (c == 'M') {
order[i] = (int)Piece::MAHARAJAH;
} else {
assert(false);
}
}
/*for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cout << ((mask_cover[3][3][(int)Piece::MAHARAJAH] >> (N * i + j)) & 1);
}
cout << '\n';
}
return 0;*/
mt19937_64 rnd(123);
ull mask_pieces = 0;
ull mask_covered = 0;
int ptr = 0;
map<pair<ull, int>, bool> mem;
function<bool()> rec = [&]() {
auto hash_pos = make_pair(mask_covered, ptr);
if (mem.count(hash_pos)) {
return mem[hash_pos];
}
if (ptr == 2 * K) {
mem[hash_pos] = false;
return false;
}
ull mask_covered_anti = ~mask_covered;
while (mask_covered_anti > 0) {
int b = __builtin_ctzll(mask_covered_anti);
mask_covered_anti &= ~(1ULL << b);
int i = b / N;
int j = b % N;
int p = order[ptr];
if (mask_pieces & mask_cover[i][j][p]) {
continue;
}
ull sf_covered = mask_covered;
mask_pieces |= (1ULL << (i * N + j));
mask_covered |= mask_cover[i][j][p];
++ptr;
auto res = rec();
--ptr;
mask_pieces &= ~(1ULL << (i * N + j));
mask_covered = sf_covered;
if (!res) {
mem[hash_pos] = true;
return true;
}
}
mem[hash_pos] = false;
return false;
};
cout << (rec() ? "Alice" : "Bob") << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 70ms
memory: 15160kb
input:
BBAARRCCQQMM
output:
Bob
result:
ok single line: 'Bob'
Test #2:
score: 0
Accepted
time: 5ms
memory: 4672kb
input:
BAMBAMQQRCCR
output:
Alice
result:
ok single line: 'Alice'
Test #3:
score: 0
Accepted
time: 0ms
memory: 4268kb
input:
QQRAACMRMCBB
output:
Alice
result:
ok single line: 'Alice'
Test #4:
score: 0
Accepted
time: 7ms
memory: 5228kb
input:
MBBARQRMACQC
output:
Alice
result:
ok single line: 'Alice'
Test #5:
score: 0
Accepted
time: 4ms
memory: 4224kb
input:
ACQCMQRBBRMA
output:
Alice
result:
ok single line: 'Alice'
Test #6:
score: -100
Wrong Answer
time: 24ms
memory: 7380kb
input:
MRCMABRQCQAB
output:
Bob
result:
wrong answer 1st lines differ - expected: 'Alice', found: 'Bob'