[LeetCode] Maximum Number of Moves to Kill All Pawns

3283. Maximum Number of Moves to Kill All Pawns

There is a 50 x 50 chessboard with one knight and some pawns on it. You are given two integers kx and ky where (kx, ky) denotes the position of the knight, and a 2D array positions where positions[i] = [xi, yi] denotes the position of the pawns on the chessboard.

Alice and Bob play a turn-based game, where Alice goes first. In each player’s turn:

  • The player selects a pawn that still exists on the board and captures it with the knight in the fewest possible moves. Note that the player can select any pawn, it might not be one that can be captured in the least number of moves.
  • In the process of capturing the selected pawn, the knight may pass other pawns without capturing them. Only the selected pawn can be captured in this turn.

Alice is trying to maximize the sum of the number of moves made by both players until there are no more pawns on the board, whereas Bob tries to minimize them.

Return the maximum total number of moves made during the game that Alice can achieve, assuming both players play optimally.

Note that in one move, a chess knight has eight possible positions it can move to, as illustrated below. Each move is two cells in a cardinal direction, then one cell in an orthogonal direction.

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
class Solution {
vector<vector<int>> cost;
int dp[1<<16][16];
vector<vector<int>> dist(vector<int>& A) {
vector<int> dy{-1,-2,-2,-1,1,2,2,1}, dx{-2,-1,1,2,2,1,-1,-2};
vector<vector<int>> dist(50,vector<int>(50,INT_MAX));
queue<pair<int,int>> q;
auto push = [&](auto y, auto x, int c) {
if(dist[y][x] > c) {
dist[y][x] = c;
q.push({y,x});
}
};
push(A[0],A[1],0);
while(q.size()) {
auto [y,x] = q.front(); q.pop();
for(int i = 0; i < 8; i++) {
int ny = y + dy[i], nx = x + dx[i];
if(0 <= ny and ny < 50 and 0 <= nx and nx < 50) push(ny,nx,dist[y][x] + 1);
}
}
return dist;
}
int helper(int mask, int x) {
if(dp[mask][x] != -1) return dp[mask][x];
int mi = INT_MAX, ma = INT_MIN, pc = __builtin_popcount(mask);
for(int i = 0; i < cost.size(); i++) {
if((mask>>i) & 1) continue;
int now = cost[x][i] + helper(mask | (1<<i), i);
mi = min(mi, now);
ma = max(ma, now);
}
return dp[mask][x] = pc & 1 ? mi : ma;
}
public:
int maxMoves(int kx, int ky, vector<vector<int>>& positions) {
memset(dp,-1,sizeof dp);
int n = positions.size();
cost = vector<vector<int>>(n, vector<int>(n));
for(int i = 0; i < n; i++) {
auto d = dist(positions[i]);
for(int j = i + 1; j < n; j++) {
cost[i][j] = cost[j][i] = d[positions[j][0]][positions[j][1]];
}
}
for(int i = 0; i < n; i++) dp[(1<<n) - 1][i] = 0;
int res = 0;
vector<int> k{kx,ky};
auto d = dist(k);
for(int i = 0; i < n; i++) {
res = max(res, d[positions[i][0]][positions[i][1]] + helper(1<<i,i));
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/08/PS/LeetCode/maximum-number-of-moves-to-kill-all-pawns/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.