100% Time & 100% Space - C++ using BFS & Bitmask DP
Code\ncpp []\nint dx[] = {-2,-2,-1,-1,1,1,2,2};\nint dy[] = {-1,1,-2,2,-2,2,-1,1};\nclass Solution {\n    vector<int> bfs(int x, int y, map<pair<int,int>,int> &
# Code\n```cpp []\nint dx[] = {-2,-2,-1,-1,1,1,2,2};\nint dy[] = {-1,1,-2,2,-2,2,-1,1};\nclass Solution {\n    vector<int> bfs(int x, int y, map<pair<int,int>,int> &mp){\n        int n = mp.size();\n        vector<int> dis(n,1e9);\n        vector<vector<int>> vis(50,vector<int>(50,0));\n        vis[x][y] = 1;\n        dis[mp[{x,y}]] = 0;\n        int left = n-1;\n        queue<pair<int,pair<int,int>>> q;\n        q.push({0,{x,y}});\n        while(left){\n            auto p = q.front();\n            q.pop();\n            int t = p.first, x = p.second.first, y = p.second.second;\n            for(int i = 0; i<8; ++i){\n                int nx = dx[i] + x, ny = dy[i] + y;\n                if(nx>=0 && nx<50 && ny>=0 && ny<50 && !vis[nx][ny]){\n                    vis[nx][ny] = 1;\n                    if(mp.find({nx,ny})!=mp.end()){\n                        dis[mp[{nx,ny}]] = t+1;\n                        --left;\n                    }\n                    q.push({t+1,{nx,ny}});\n                }\n            }\n        }\n        return dis;\n    }\n    int solve(int indx, int mask, int n, vector<vector<int>> &dp, vector<vector<int>> &pre, vector<vector<int>> &p){\n        if(mask==((1<<n)-1)) return 0;\n        if(dp[indx][mask]!=-1) return dp[indx][mask];\n        int ans1 = -1e9, ans2 = 1e9, ans;\n        for(int i = 0; i<n; ++i){\n            if((mask)&(1<<i)) continue;\n            int curr = pre[indx][i] + solve(i,mask|(1<<i), n, dp, pre, p);\n            ans1 = max(ans1,curr);\n            ans2 = min(ans2,curr);\n        }\n\n        if(__builtin_popcount(mask)%2==0) ans = ans1;\n        else ans = ans2;\n        return dp[indx][mask] = ans;\n    }\npublic:\n    int maxMoves(int kx, int ky, vector<vector<int>>& p) {\n        p.push_back({kx,ky});\n        map<pair<int,int>,int> mp;\n        for(int i = 0; i<p.size(); ++i){\n            mp[{p[i][0],p[i][1]}] = i;\n        }\n        vector<vector<int>> pre(p.size());\n        for(int i = 0; i<p.size(); ++i){\n            pre[i] = bfs(p[i][0],p[i][1],mp);\n        }\n        vector<vector<int>> dp(p.size(), vector<int>(1<<(p.size()-1),-1));\n        return solve(p.size()-1,0,p.size()-1,dp, pre, p);\n    }\n};\n```
['C++']