Bitmask DP check all permutations
\n#include <vector>\n#include <deque>\n#include <algorithm>\n#include <climits>\n\nusing namespace std;\n\nclass Solution {\npublic:\n    int maxMoves(int kx, i
```\n#include <vector>\n#include <deque>\n#include <algorithm>\n#include <climits>\n\nusing namespace std;\n\nclass Solution {\npublic:\n    int maxMoves(int kx, int ky, vector<vector<int>>& positions) {\n        static vector<vector<int>> dist(2500, vector<int>(2500, 0));\n        static vector<bool> done(2500, false);\n        int m = 50, n = 50;\n        positions.push_back({kx, ky});\n        for (auto el : positions) {\n            int i = el[0];\n            int j = el[1];\n            int source = n * i + j;\n            if (done[source]) continue;\n            deque<tuple<int, int, int>> q;\n            q.emplace_back(i, j, 0);\n            vector<vector<bool>> v(m, vector<bool>(n, false));\n            v[i][j] = true;\n\n            while (!q.empty()) {\n                auto [ii, jj, d] = q.back(); q.pop_back();\n                dist[source][n * ii + jj] = d;\n\n                for (int dx : {-2, -1, 1, 2}) {\n                    for (int dy : {-2, -1, 1, 2}) {\n                        if (abs(dx) == abs(dy)) continue;\n                        int x = ii + dx, y = jj + dy;\n                        if (x >= 0 && x < m && y >= 0 && y < n && !v[x][y]) {\n                            v[x][y] = true;\n                            q.emplace_front(x, y, d + 1);\n                        }\n                    }\n                }\n            }\n            \n            done[source] = true;\n        }\n\n        int n_positions = positions.size();\n        int target = (1 << n_positions) - 1;\n        vector<vector<vector<int>>> dp(n_positions, vector<vector<int>>(1 << n_positions, vector<int>(2, -1)));\n        for (int i = 0; i < n_positions; ++i) dp[i][target][0] = dp[i][target][1] = 0;\n        for (int mask = target - 1; mask >= 0; --mask) {\n            for (int curr = 0; curr < n_positions; ++curr) {\n                for (int alice = 0; alice < 2; ++alice) {\n                    dp[curr][mask][alice] = alice ? -1e7 : 1e7;\n                    for (int i = 0; i < n_positions - 1; ++i) {\n                        if (!(mask & (1 << i))) {\n                            int cost = dist[n * positions[curr][0] + positions[curr][1]][n * positions[i][0] + positions[i][1]];\n                            if (alice) {\n                                dp[curr][mask][alice] = max(dp[curr][mask][alice], cost + dp[i][mask | (1 << i)][1 - alice]);\n                            } else {\n                                dp[curr][mask][alice] = min(dp[curr][mask][alice], cost + dp[i][mask | (1 << i)][1 - alice]);\n                            }\n                        }\n                    }\n                }\n            }\n        }\n        return dp[n_positions - 1][1 << (n_positions - 1)][1];\n    }\n};\n```
['C++']