|
| 1 | +package g3201_3300.s3283_maximum_number_of_moves_to_kill_all_pawns; |
| 2 | + |
| 3 | +// #Hard #Array #Math #Breadth_First_Search #Bit_Manipulation #Bitmask #Game_Theory |
| 4 | +// #2024_09_09_Time_250_ms_(98.43%)_Space_50.1_MB_(66.27%) |
| 5 | + |
| 6 | +import java.util.LinkedList; |
| 7 | +import java.util.Queue; |
| 8 | + |
| 9 | +public class Solution { |
| 10 | + private static final int[][] KNIGHT_MOVES = { |
| 11 | + {-2, -1}, {-2, 1}, {-1, -2}, {-1, 2}, |
| 12 | + {1, -2}, {1, 2}, {2, -1}, {2, 1} |
| 13 | + }; |
| 14 | + private int[][] distances; |
| 15 | + private Integer[][] memo; |
| 16 | + |
| 17 | + public int maxMoves(int kx, int ky, int[][] positions) { |
| 18 | + int n = positions.length; |
| 19 | + distances = new int[n + 1][n + 1]; |
| 20 | + memo = new Integer[n + 1][1 << n]; |
| 21 | + // Calculate distances between all pairs of positions (including knight's initial position) |
| 22 | + for (int i = 0; i < n; i++) { |
| 23 | + distances[n][i] = calculateMoves(kx, ky, positions[i][0], positions[i][1]); |
| 24 | + for (int j = i + 1; j < n; j++) { |
| 25 | + int dist = |
| 26 | + calculateMoves( |
| 27 | + positions[i][0], positions[i][1], positions[j][0], positions[j][1]); |
| 28 | + distances[i][j] = distances[j][i] = dist; |
| 29 | + } |
| 30 | + } |
| 31 | + return minimax(n, (1 << n) - 1, true); |
| 32 | + } |
| 33 | + |
| 34 | + private int minimax(int lastPos, int remainingPawns, boolean isAlice) { |
| 35 | + if (remainingPawns == 0) { |
| 36 | + return 0; |
| 37 | + } |
| 38 | + if (memo[lastPos][remainingPawns] != null) { |
| 39 | + return memo[lastPos][remainingPawns]; |
| 40 | + } |
| 41 | + int result = isAlice ? 0 : Integer.MAX_VALUE; |
| 42 | + for (int i = 0; i < distances.length - 1; i++) { |
| 43 | + if ((remainingPawns & (1 << i)) != 0) { |
| 44 | + int newRemainingPawns = remainingPawns & ~(1 << i); |
| 45 | + int moveValue = distances[lastPos][i] + minimax(i, newRemainingPawns, !isAlice); |
| 46 | + |
| 47 | + if (isAlice) { |
| 48 | + result = Math.max(result, moveValue); |
| 49 | + } else { |
| 50 | + result = Math.min(result, moveValue); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + memo[lastPos][remainingPawns] = result; |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + private int calculateMoves(int x1, int y1, int x2, int y2) { |
| 59 | + if (x1 == x2 && y1 == y2) { |
| 60 | + return 0; |
| 61 | + } |
| 62 | + boolean[][] visited = new boolean[50][50]; |
| 63 | + Queue<int[]> queue = new LinkedList<>(); |
| 64 | + queue.offer(new int[] {x1, y1, 0}); |
| 65 | + visited[x1][y1] = true; |
| 66 | + while (!queue.isEmpty()) { |
| 67 | + int[] current = queue.poll(); |
| 68 | + int x = current[0]; |
| 69 | + int y = current[1]; |
| 70 | + int moves = current[2]; |
| 71 | + for (int[] move : KNIGHT_MOVES) { |
| 72 | + int nx = x + move[0]; |
| 73 | + int ny = y + move[1]; |
| 74 | + if (nx == x2 && ny == y2) { |
| 75 | + return moves + 1; |
| 76 | + } |
| 77 | + if (nx >= 0 && nx < 50 && ny >= 0 && ny < 50 && !visited[nx][ny]) { |
| 78 | + queue.offer(new int[] {nx, ny, moves + 1}); |
| 79 | + visited[nx][ny] = true; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + // Should never reach here if input is valid |
| 84 | + return -1; |
| 85 | + } |
| 86 | +} |
0 commit comments