|
| 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_11_Time_638_ms_(100.00%)_Space_62.2_MB_(87.50%) |
| 5 | + |
| 6 | +import java.util.LinkedList |
| 7 | +import java.util.Queue |
| 8 | +import kotlin.math.max |
| 9 | +import kotlin.math.min |
| 10 | + |
| 11 | +class Solution { |
| 12 | + private lateinit var distances: Array<IntArray> |
| 13 | + private lateinit var memo: Array<Array<Int?>?> |
| 14 | + |
| 15 | + fun maxMoves(kx: Int, ky: Int, positions: Array<IntArray>): Int { |
| 16 | + val n = positions.size |
| 17 | + distances = Array<IntArray>(n + 1) { IntArray(n + 1) { 0 } } |
| 18 | + memo = Array<Array<Int?>?>(n + 1) { arrayOfNulls<Int>(1 shl n) } |
| 19 | + // Calculate distances between all pairs of positions (including knight's initial position) |
| 20 | + for (i in 0 until n) { |
| 21 | + distances[n][i] = calculateMoves(kx, ky, positions[i][0], positions[i][1]) |
| 22 | + for (j in i + 1 until n) { |
| 23 | + val dist = |
| 24 | + calculateMoves( |
| 25 | + positions[i][0], positions[i][1], positions[j][0], positions[j][1] |
| 26 | + ) |
| 27 | + distances[j][i] = dist |
| 28 | + distances[i][j] = distances[j][i] |
| 29 | + } |
| 30 | + } |
| 31 | + return minimax(n, (1 shl n) - 1, true) |
| 32 | + } |
| 33 | + |
| 34 | + private fun minimax(lastPos: Int, remainingPawns: Int, isAlice: Boolean): Int { |
| 35 | + if (remainingPawns == 0) { |
| 36 | + return 0 |
| 37 | + } |
| 38 | + if (memo[lastPos]!![remainingPawns] != null) { |
| 39 | + return memo[lastPos]!![remainingPawns]!! |
| 40 | + } |
| 41 | + var result = if (isAlice) 0 else Int.Companion.MAX_VALUE |
| 42 | + for (i in 0 until distances.size - 1) { |
| 43 | + if ((remainingPawns and (1 shl i)) != 0) { |
| 44 | + val newRemainingPawns = remainingPawns and (1 shl i).inv() |
| 45 | + val moveValue = distances[lastPos][i] + minimax(i, newRemainingPawns, !isAlice) |
| 46 | + result = if (isAlice) { |
| 47 | + max(result, moveValue) |
| 48 | + } else { |
| 49 | + min(result, moveValue) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + memo[lastPos]!![remainingPawns] = result |
| 54 | + return result |
| 55 | + } |
| 56 | + |
| 57 | + private fun calculateMoves(x1: Int, y1: Int, x2: Int, y2: Int): Int { |
| 58 | + if (x1 == x2 && y1 == y2) { |
| 59 | + return 0 |
| 60 | + } |
| 61 | + val visited = Array<BooleanArray?>(50) { BooleanArray(50) } |
| 62 | + val queue: Queue<IntArray> = LinkedList<IntArray>() |
| 63 | + queue.offer(intArrayOf(x1, y1, 0)) |
| 64 | + visited[x1]!![y1] = true |
| 65 | + while (queue.isNotEmpty()) { |
| 66 | + val current = queue.poll() |
| 67 | + val x = current[0] |
| 68 | + val y = current[1] |
| 69 | + val moves = current[2] |
| 70 | + for (move in KNIGHT_MOVES) { |
| 71 | + val nx = x + move[0] |
| 72 | + val ny = y + move[1] |
| 73 | + if (nx == x2 && ny == y2) { |
| 74 | + return moves + 1 |
| 75 | + } |
| 76 | + if (nx >= 0 && nx < 50 && ny >= 0 && ny < 50 && !visited[nx]!![ny]) { |
| 77 | + queue.offer(intArrayOf(nx, ny, moves + 1)) |
| 78 | + visited[nx]!![ny] = true |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + // Should never reach here if input is valid |
| 83 | + return -1 |
| 84 | + } |
| 85 | + |
| 86 | + companion object { |
| 87 | + private val KNIGHT_MOVES = arrayOf<IntArray>( |
| 88 | + intArrayOf(-2, -1), intArrayOf(-2, 1), intArrayOf(-1, -2), intArrayOf(-1, 2), |
| 89 | + intArrayOf(1, -2), intArrayOf(1, 2), intArrayOf(2, -1), intArrayOf(2, 1) |
| 90 | + ) |
| 91 | + } |
| 92 | +} |
0 commit comments