comments | difficulty | edit_url | rating | source | tags | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
true |
Hard |
2473 |
Weekly Contest 414 Q4 |
|
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.
Example 1:
Input: kx = 1, ky = 1, positions = [[0,0]]
Output: 4
Explanation:
The knight takes 4 moves to reach the pawn at (0, 0)
.
Example 2:
Input: kx = 0, ky = 2, positions = [[1,1],[2,2],[3,3]]
Output: 8
Explanation:
- Alice picks the pawn at
(2, 2)
and captures it in two moves:(0, 2) -> (1, 4) -> (2, 2)
. - Bob picks the pawn at
(3, 3)
and captures it in two moves:(2, 2) -> (4, 1) -> (3, 3)
. - Alice picks the pawn at
(1, 1)
and captures it in four moves:(3, 3) -> (4, 1) -> (2, 2) -> (0, 3) -> (1, 1)
.
Example 3:
Input: kx = 0, ky = 0, positions = [[1,2],[2,4]]
Output: 3
Explanation:
- Alice picks the pawn at
(2, 4)
and captures it in two moves:(0, 0) -> (1, 2) -> (2, 4)
. Note that the pawn at(1, 2)
is not captured. - Bob picks the pawn at
(1, 2)
and captures it in one move:(2, 4) -> (1, 2)
.
Constraints:
0 <= kx, ky <= 49
1 <= positions.length <= 15
positions[i].length == 2
0 <= positions[i][0], positions[i][1] <= 49
- All
positions[i]
are unique. - The input is generated such that
positions[i] != [kx, ky]
for all0 <= i < positions.length
.
First, we preprocess the shortest distance for each pawn to any position on the chessboard and record it in the array
Next, we design a function
The specific implementation of the function
- If
$\textit{state} = 0$ , it means there are no pawns left, return$0$ ; - If
$\textit{k} = 1$ , it means it is Alice's turn. We need to find a pawn such that the number of moves after eating this pawn is maximized, i.e.,$\text{dfs}(i, \textit{state} \oplus 2^i, \textit{k} \oplus 1) + \textit{dist}[\textit{last}][x][y]$ ; - If
$\textit{k} = 0$ , it means it is Bob's turn. We need to find a pawn such that the number of moves after eating this pawn is minimized, i.e.,$\text{dfs}(i, \textit{state} \oplus 2^i, \textit{k} \oplus 1) + \textit{dist}[\textit{last}][x][y]$ .
To avoid repeated calculations, we use memoization, i.e., using a hash table to record the states that have already been calculated.
The time complexity is
class Solution:
def maxMoves(self, kx: int, ky: int, positions: List[List[int]]) -> int:
@cache
def dfs(last: int, state: int, k: int) -> int:
if state == 0:
return 0
if k:
res = 0
for i, (x, y) in enumerate(positions):
if state >> i & 1:
t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y]
if res < t:
res = t
return res
else:
res = inf
for i, (x, y) in enumerate(positions):
if state >> i & 1:
t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y]
if res > t:
res = t
return res
n = len(positions)
m = 50
dist = [[[-1] * m for _ in range(m)] for _ in range(n + 1)]
dx = [1, 1, 2, 2, -1, -1, -2, -2]
dy = [2, -2, 1, -1, 2, -2, 1, -1]
positions.append([kx, ky])
for i, (x, y) in enumerate(positions):
dist[i][x][y] = 0
q = deque([(x, y)])
step = 0
while q:
step += 1
for _ in range(len(q)):
x1, y1 = q.popleft()
for j in range(8):
x2, y2 = x1 + dx[j], y1 + dy[j]
if 0 <= x2 < m and 0 <= y2 < m and dist[i][x2][y2] == -1:
dist[i][x2][y2] = step
q.append((x2, y2))
ans = dfs(n, (1 << n) - 1, 1)
dfs.cache_clear()
return ans
class Solution {
private Integer[][][] f;
private Integer[][][] dist;
private int[][] positions;
private final int[] dx = {1, 1, 2, 2, -1, -1, -2, -2};
private final int[] dy = {2, -2, 1, -1, 2, -2, 1, -1};
public int maxMoves(int kx, int ky, int[][] positions) {
int n = positions.length;
final int m = 50;
dist = new Integer[n + 1][m][m];
this.positions = positions;
for (int i = 0; i <= n; ++i) {
int x = i < n ? positions[i][0] : kx;
int y = i < n ? positions[i][1] : ky;
Deque<int[]> q = new ArrayDeque<>();
q.offer(new int[] {x, y});
for (int step = 1; !q.isEmpty(); ++step) {
for (int k = q.size(); k > 0; --k) {
var p = q.poll();
int x1 = p[0], y1 = p[1];
for (int j = 0; j < 8; ++j) {
int x2 = x1 + dx[j], y2 = y1 + dy[j];
if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < m && dist[i][x2][y2] == null) {
dist[i][x2][y2] = step;
q.offer(new int[] {x2, y2});
}
}
}
}
}
f = new Integer[n + 1][1 << n][2];
return dfs(n, (1 << n) - 1, 1);
}
private int dfs(int last, int state, int k) {
if (state == 0) {
return 0;
}
if (f[last][state][k] != null) {
return f[last][state][k];
}
int res = k == 1 ? 0 : Integer.MAX_VALUE;
for (int i = 0; i < positions.length; ++i) {
int x = positions[i][0], y = positions[i][1];
if ((state >> i & 1) == 1) {
int t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y];
res = k == 1 ? Math.max(res, t) : Math.min(res, t);
}
}
return f[last][state][k] = res;
}
}
class Solution {
public:
int maxMoves(int kx, int ky, vector<vector<int>>& positions) {
int n = positions.size();
const int m = 50;
const int dx[8] = {1, 1, 2, 2, -1, -1, -2, -2};
const int dy[8] = {2, -2, 1, -1, 2, -2, 1, -1};
int dist[n + 1][m][m];
memset(dist, -1, sizeof(dist));
for (int i = 0; i <= n; ++i) {
int x = (i < n) ? positions[i][0] : kx;
int y = (i < n) ? positions[i][1] : ky;
queue<pair<int, int>> q;
q.push({x, y});
dist[i][x][y] = 0;
for (int step = 1; !q.empty(); ++step) {
for (int k = q.size(); k > 0; --k) {
auto [x1, y1] = q.front();
q.pop();
for (int j = 0; j < 8; ++j) {
int x2 = x1 + dx[j], y2 = y1 + dy[j];
if (x2 >= 0 && x2 < m && y2 >= 0 && y2 < m && dist[i][x2][y2] == -1) {
dist[i][x2][y2] = step;
q.push({x2, y2});
}
}
}
}
}
int f[n + 1][1 << n][2];
memset(f, -1, sizeof(f));
auto dfs = [&](this auto&& dfs, int last, int state, int k) -> int {
if (state == 0) {
return 0;
}
if (f[last][state][k] != -1) {
return f[last][state][k];
}
int res = (k == 1) ? 0 : INT_MAX;
for (int i = 0; i < positions.size(); ++i) {
int x = positions[i][0], y = positions[i][1];
if ((state >> i) & 1) {
int t = dfs(i, state ^ (1 << i), k ^ 1) + dist[last][x][y];
if (k == 1) {
res = max(res, t);
} else {
res = min(res, t);
}
}
}
return f[last][state][k] = res;
};
return dfs(n, (1 << n) - 1, 1);
}
};
func maxMoves(kx int, ky int, positions [][]int) int {
n := len(positions)
const m = 50
dx := []int{1, 1, 2, 2, -1, -1, -2, -2}
dy := []int{2, -2, 1, -1, 2, -2, 1, -1}
dist := make([][][]int, n+1)
for i := range dist {
dist[i] = make([][]int, m)
for j := range dist[i] {
dist[i][j] = make([]int, m)
for k := range dist[i][j] {
dist[i][j][k] = -1
}
}
}
for i := 0; i <= n; i++ {
x := kx
y := ky
if i < n {
x = positions[i][0]
y = positions[i][1]
}
q := [][2]int{[2]int{x, y}}
dist[i][x][y] = 0
for step := 1; len(q) > 0; step++ {
for k := len(q); k > 0; k-- {
p := q[0]
q = q[1:]
x1, y1 := p[0], p[1]
for j := 0; j < 8; j++ {
x2 := x1 + dx[j]
y2 := y1 + dy[j]
if x2 >= 0 && x2 < m && y2 >= 0 && y2 < m && dist[i][x2][y2] == -1 {
dist[i][x2][y2] = step
q = append(q, [2]int{x2, y2})
}
}
}
}
}
f := make([][][]int, n+1)
for i := range f {
f[i] = make([][]int, 1<<n)
for j := range f[i] {
f[i][j] = make([]int, 2)
for k := range f[i][j] {
f[i][j][k] = -1
}
}
}
var dfs func(last, state, k int) int
dfs = func(last, state, k int) int {
if state == 0 {
return 0
}
if f[last][state][k] != -1 {
return f[last][state][k]
}
var res int
if k == 0 {
res = math.MaxInt32
}
for i, p := range positions {
x, y := p[0], p[1]
if (state>>i)&1 == 1 {
t := dfs(i, state^(1<<i), k^1) + dist[last][x][y]
if k == 1 {
res = max(res, t)
} else {
res = min(res, t)
}
}
}
f[last][state][k] = res
return res
}
return dfs(n, (1<<n)-1, 1)
}