-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
maximum-number-of-moves-to-kill-all-pawns.cpp
69 lines (66 loc) · 2.74 KB
/
maximum-number-of-moves-to-kill-all-pawns.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Time: O(p * n^2 + p^2 + p^2 * 2^p) = O(p^2 * 2^p)
// Space: O(p^2 + n^2 + p * 2^p) = O(p * 2^p)
// bfs, bitmasks, dp
class Solution {
public:
int maxMoves(int kx, int ky, vector<vector<int>>& positions) {
static const int N = 50;
static const vector<pair<int, int>> DIRECTIONS = {{1, 2}, {-1, 2}, {1, -2}, {-1, -2}, {2, 1}, {-2, 1}, {2, -1}, {-2, -1}};
static const int POS_INF = numeric_limits<int>::max();
static const int NEG_INF = numeric_limits<int>::min();
const auto& bfs = [](int r, int c) {
vector<vector<int>> dist(N, vector<int>(N, POS_INF));
dist[r][c] = 0;
vector<pair<int, int>> q = {{r, c}};
while (!empty(q)) {
vector<pair<int, int>> new_q;
for (const auto& [r, c] : q) {
for (const auto& [dr, dc] : DIRECTIONS) {
const int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < N && 0 <= nc && nc < N && dist[nr][nc] == POS_INF)) {
continue;
}
dist[nr][nc] = dist[r][c] + 1;
new_q.emplace_back(nr, nc);
}
}
q = move(new_q);
}
return dist;
};
const int p = size(positions);
positions.emplace_back(vector<int>{kx, ky});
vector<vector<int>> dist(p + 1, vector<int>(p + 1));
for (int i = 0; i <= p; ++i) {
const auto& d = bfs(positions[i][0], positions[i][1]);
for (int j = i + 1; j <= p; ++j) {
dist[j][i] = dist[i][j] = d[positions[j][0]][positions[j][1]];
}
}
vector<vector<int>> dp(1 << p);
for (int mask = 1; mask < 1 << p; ++mask) {
dp[mask].assign(p, __builtin_popcount(mask) & 1 ? POS_INF : NEG_INF);
}
dp.back().assign(p, 0);
for (int mask = (1 << p) - 1; mask >= 1; --mask) {
const auto& turn = (__builtin_popcount(mask) & 1) ^ 1;
for (int i = 0; i < p; ++i) {
if ((mask & (1 << i)) == 0) {
continue;
}
for (int j = 0; j < p; ++j) {
if (j == i || (mask & (1 << j)) == 0) {
continue;
}
dp[mask ^ (1 << i)][j] = turn ? min(dp[mask ^ (1 << i)][j], dp[mask][i] + dist[i][j])
: max(dp[mask ^ (1 << i)][j], dp[mask][i] + dist[i][j]);
}
}
}
int result = 0;
for (int i = 0; i < p; ++i) {
result = max(result, dp[1 << i][i] + dist[i][p]);
}
return result;
}
};