Approach
Breadth-first search
For Maximum Number of Moves to Kill All Pawns, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 87 lines of Java from the credited upstream file 3283.java.
- The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue, cached states.
- 11 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int maxMoves(int kx, int ky, int[][] positions) {3 final int n = positions.length;4 List<int[]> positionsList = new ArrayList<>(List.of(positions));5 positionsList.add(new int[] {kx, ky});6 Map<Integer, Integer> hashedPositionToIndex = new HashMap<>();7 8 int[][] dist = new int[n + 1][n + 1];9 10 for (int i = 0; i < positionsList.size(); ++i) {11 final int x = positionsList.get(i)[0];12 final int y = positionsList.get(i)[1];13 hashedPositionToIndex.put(hash(x, y), i);14 }15 16 for (int sourceIndex = 0; sourceIndex < n + 1; ++sourceIndex)17 bfs(positionsList, sourceIndex, hashedPositionToIndex, dist);18 19 int MAX_MASK = 1 << (n + 1);20 21 22 23 24 int[][][] dp = new int[n + 1][1 << (n + 1)][2];25 26 for (int i = 0; i < n + 1; ++i)27 for (int mask = 0; mask < MAX_MASK - 1; ++mask)28 dp[i][mask] = new int[] {-MAX, MAX};29 30 for (int mask = MAX_MASK - 2; mask >= 0; --mask)31 for (int i = 0; i < n + 1; ++i)32 for (int turn = 0; turn < 2; ++turn)33 for (int j = 0; j < n; ++j) {34 if ((mask >> j & 1) == 1)35 continue;36 final int moves = dist[i][j] + dp[j][mask | 1 << j][1 - turn];37 dp[i][mask][turn] = turn == 0 ? Math.max(dp[i][mask][turn], moves) 38 : Math.min(dp[i][mask][turn], moves);39 }40 41 42 43 return dp[n][1 << n][0];44 }45 46 private static final int SIZE = 50;47 private static final int MAX = 1_000_000;48 private static final int[][] DIRS = {{1, 2}, {2, 1}, {2, -1}, {1, -2},49 {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};50 51 private int hash(int x, int y) {52 return x * SIZE + y;53 }54 55 56 private void bfs(List<int[]> positions, int sourceIndex,57 Map<Integer, Integer> hashedPositionToIndex, int[][] dist) {58 final int sx = positions.get(sourceIndex)[0];59 final int sy = positions.get(sourceIndex)[1];60 Queue<Pair<Integer, Integer>> q = new ArrayDeque<>(List.of(new Pair<>(sx, sy)));61 boolean[][] seen = new boolean[SIZE][SIZE];62 seen[sx][sy] = true;63 int seenPositions = 0;64 65 for (int step = 0; !q.isEmpty() && seenPositions < positions.size(); ++step)66 for (int sz = q.size(); sz > 0; --sz) {67 final int i = q.peek().getKey();68 final int j = q.poll().getValue();69 final int hashedPosition = hash(i, j);70 if (hashedPositionToIndex.containsKey(hashedPosition)) {71 dist[sourceIndex][hashedPositionToIndex.get(hashedPosition)] = step;72 ++seenPositions;73 }74 for (int[] dir : DIRS) {75 final int x = i + dir[0];76 final int y = j + dir[1];77 if (x < 0 || x >= SIZE || y < 0 || y >= SIZE)78 continue;79 if (seen[x][y])80 continue;81 q.offer(new Pair<>(x, y));82 seen[x][y] = true;83 }84 }85 }86}87