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
- 90 lines of C++ from the credited upstream file 3283.cpp.
- The implementation visibly relies on sequence storage, hash 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:3 int maxMoves(int kx, int ky, vector<vector<int>>& positions) {4 const int n = positions.size();5 positions.push_back({kx, ky});6 unordered_map<int, int> hashedPositionToIndex;7 8 vector<vector<int>> dist(n + 1, vector<int>(n + 1));9 10 for (int i = 0; i < positions.size(); ++i) {11 const int x = positions[i][0];12 const int y = positions[i][1];13 hashedPositionToIndex[hash(x, y)] = i;14 }15 16 for (int sourceIndex = 0; sourceIndex < n + 1; ++sourceIndex)17 bfs(positions, sourceIndex, hashedPositionToIndex, dist);18 19 const int maxMask = 1 << (n + 1);20 21 22 23 24 vector<vector<vector<int>>> dp(25 n + 1, vector<vector<int>>(1 << (n + 1), vector<int>(2)));26 27 for (int i = 0; i < n + 1; ++i)28 for (int mask = 0; mask < maxMask - 1; ++mask)29 dp[i][mask] = {-kMax, kMax};30 31 for (int mask = maxMask - 2; mask >= 0; --mask)32 for (int i = 0; i < n + 1; ++i)33 for (int turn = 0; turn < 2; ++turn)34 for (int j = 0; j < n; ++j) {35 if (mask >> j & 1)36 continue;37 const int moves = dist[i][j] + dp[j][mask | 1 << j][1 - turn];38 dp[i][mask][turn] = turn == 0 ? max(dp[i][mask][turn], moves)39 : min(dp[i][mask][turn], moves);40 }41 42 43 44 return dp[n][1 << n][0];45 }46 47 private:48 static constexpr int kSize = 50;49 static constexpr int kMax = 1'000'000;50 static constexpr int kDirs[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2},51 {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};52 53 int hash(int x, int y) {54 return x * kSize + y;55 }56 57 58 void bfs(const vector<vector<int>>& positions, int sourceIndex,59 const unordered_map<int, int>& hashedPositionToIndex,60 vector<vector<int>>& dist) {61 const int sx = positions[sourceIndex][0];62 const int sy = positions[sourceIndex][1];63 queue<pair<int, int>> q{{{sx, sy}}};64 vector<vector<bool>> seen(kSize, vector<bool>(kSize));65 seen[sx][sy] = true;66 int seenPositions = 0;67 68 for (int step = 0; !q.empty() && seenPositions < positions.size(); ++step)69 for (int sz = q.size(); sz > 0; --sz) {70 const auto [i, j] = q.front();71 q.pop();72 if (const auto it = hashedPositionToIndex.find(hash(i, j));73 it != end(hashedPositionToIndex)) {74 dist[sourceIndex][it->second] = step;75 ++seenPositions;76 }77 for (const auto& [dx, dy] : kDirs) {78 const int x = i + dx;79 const int y = j + dy;80 if (x < 0 || x >= kSize || y < 0 || y >= kSize)81 continue;82 if (seen[x][y])83 continue;84 q.emplace(x, y);85 seen[x][y] = true;86 }87 }88 }89};90