.
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Tim
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/6dbae2df-9178-4004-aa41-2f4be16acc67_1725891371.8020248.png)\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n//odd set bit-->alise-->max;\n    int helper(int l, int kx, int ky, vector<vector<int>>& pos, vector<vector<int>>&dp, vector<vector<int>>&precum, vector<vector<int>>&precum1, int msk){\n        int i,j;\n        if(msk==0) return 0;\n \n        int set=pos.size() - __builtin_popcount(msk);\n;\n        // for(int k=0;k<pos.size();k++){\n        //     set+=((1<<k) & msk);\n        // }\n            if(dp[l][msk]!=-1) {\n                return dp[l][msk];\n            }\n            if(l==pos.size()){\n                i=kx;\n                j=ky;\n            }\n            else{\n                i=pos[l][0];\n                j=pos[l][1];\n            }\n\n\n        if(set%2==0){\n            dp[l][msk]=INT_MIN;\n            for(int k=0;k<pos.size();k++){\n                if(msk & (1<<k)){\n                    int newmsk=msk^(1<<k);\n                    int x=pos[k][0];\n                    int y=pos[k][1];\n                    int temp;\n                    if(((i==0 && j==0) || (i==0 && j==49) || (i==49 && j==0) || (i==49 && j==49)) || ((x==0 && y==0) || (x==0 && y==49) || (x==49 && y==0) || (x==49 && y==49))){\n                        temp=precum[abs(x-i)][abs(y-j)];\n                    }\n                    else{\n                        if((x-i+2)<2 && (y-j+2)<2) temp=precum1[4-(x-i+2)][4-(y-j+2)];\n                        else if((x-i+2)<2 && (y-j+2)>=2) temp=precum1[4-(x-i+2)][(y-j+2)];\n                        else if((x-i+2)>=2 && (y-j+2)<2) temp=precum1[(x-i+2)][4-(y-j+2)];\n                        else temp=precum1[(x-i+2)][(y-j+2)];\n                    }\n                    \n                    dp[l][msk]=max(dp[l][msk],helper(k, kx, ky, pos, dp, precum, precum1, newmsk)+temp);\n                }\n            \n            }\n        }\n        else{\n            dp[l][msk]=INT_MAX;\n            for(int k=0;k<pos.size();k++){\n                if(msk & (1<<k)){\n                    int newmsk=msk^(1<<k);\n                    int x=pos[k][0];\n                    int y=pos[k][1];\n                    int temp;\n                    if(((i==0 && j==0) || (i==0 && j==49) || (i==49 && j==0) || (i==49 && j==49)) || ((x==0 && y==0) || (x==0 && y==49) || (x==49 && y==0) || (x==49 && y==49))){\n                        temp=precum[abs(x-i)][abs(y-j)];\n                    }\n                    else{\n                        if((x-i+2)<2 && (y-j+2)<2) temp=precum1[4-(x-i+2)][4-(y-j+2)];\n                        else if((x-i+2)<2 && (y-j+2)>=2) temp=precum1[4-(x-i+2)][(y-j+2)];\n                        else if((x-i+2)>=2 && (y-j+2)<2) temp=precum1[(x-i+2)][4-(y-j+2)];\n                        else temp=precum1[(x-i+2)][(y-j+2)];\n                    }\n                    \n                    dp[l][msk]=min(dp[l][msk],helper(k, kx, ky, pos, dp, precum, precum1, newmsk)+temp);\n                }\n            \n            }\n        }\n        cout<<dp[l][msk]<<"  ";\n        return dp[l][msk];\n    }\n\n    int maxMoves(int kx, int ky, vector<vector<int>>& positions) {\n        vector<vector<int>>precum(50,vector<int>(50,0));\n        queue<pair<int,int>>q;\n        q.push({0,0});\n        vector<pair<int,int>>pos={{2,1}, {2,-1}, {-2,1}, {-2,-1}, {1,2}, {-1,2}, {1,-2}, {-1,-2}};\n        int x=0;\n        while(!q.empty()){\n            x++;\n            int s=q.size();\n            for(int k=0;k<s;k++){\n                int i=q.front().first;\n                int j=q.front().second;\n                q.pop();\n                for(auto k1: pos){\n                    int i1=i+k1.first;\n                    int j1=j+k1.second;\n                    if((i1>=0 && i1<50) && (j1>=0 && j1<50) && !precum[i1][j1]){\n                        precum[i1][j1]=x;\n                        q.push({i1,j1});\n                    }\n                }\n            }\n        }\n        vector<vector<int>>precum1(52,vector<int>(52,0));\n        q.push({2,2});\n        x=0;\n        while(!q.empty()){\n            x++;\n            int s=q.size();\n            for(int k=0;k<s;k++){\n                int i=q.front().first;\n                int j=q.front().second;\n                q.pop();\n                for(auto k1: pos){\n                    int i1=i+k1.first;\n                    int j1=j+k1.second;\n                    if((i1>=0 && i1<52) && (j1>=0 && j1<52) && !precum1[i1][j1]){\n                        precum1[i1][j1]=x;\n                        q.push({i1,j1});\n                    }\n                }\n            }\n        }\n       \n        // for(auto i: precum){\n        //     for(auto j: i){\n        //         cout<<j<<" ";\n\n        //     }\n        //     cout<<endl;\n        // }\n\n        int k=positions.size();\n        int msk=(1<<k)-1;\n        vector<vector<int>>dp(k+1,vector<int>((1<<k),-1));\n        int set=__builtin_popcount(msk);\n        // cout<<k<<" "<<msk<<" "<<set<<endl;\n        int l=k;\n        \n        return helper(l, kx, ky, positions, dp, precum, precum1, msk);;\n\n    }\n};\n```
['C++']