0ms C++ code with explaination
The point to be noted here is that there are at max 8 possible attackers (for 8 directions) \nWe denote directions using this matrix:\n\n0      1     2\n3   Kin
The point to be noted here is that there are at max 8 possible attackers (for 8 directions) \nWe denote directions using this matrix:\n\n0      1     2\n3   King   4\n5    6      7\n\nNow for iterate on all the queens and check if it can attack the king, if yes then from which direction, if no previous queen is attacking from that direction, then we assign current queen to attack king from this direction, else we choose the queen with minimum distance from current queen and the previously assigned queen.\n\n```\nclass Solution {\n    int dis(vector<int>ans,vector<int>attacker){\n        return abs(ans[0]-attacker[0])+abs(ans[1]-attacker[1]);\n    }\npublic:\n    vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {\n        vector<vector<int> >sol;\n        vector< vector<int> > attackers(8);\n        for(int i=0;i<queens.size();i++){\n            if(king[0]==queens[i][0]&&king[1]>queens[i][1]){\n                if((attackers[3].size()==0)||(dis(attackers[3],king)>dis(queens[i],king)))\n                    attackers[3]=queens[i];\n            }\n            if(king[0]==queens[i][0]&&king[1]<queens[i][1]){\n                if((attackers[4].size()==0)||(dis(attackers[4],king)>dis(queens[i],king)))\n                    attackers[4]=queens[i];\n            }\n            if(king[0]-queens[i][0]==king[1]-queens[i][1]&&king[0]>queens[i][0]){\n                if((attackers[0].size()==0)||(dis(attackers[0],king)>dis(queens[i],king)))\n                    attackers[0]=queens[i];\n                \n            }\n            if(king[0]-queens[i][0]==king[1]-queens[i][1]&&king[0]<queens[i][0]){\n                if((attackers[7].size()==0)||(dis(attackers[7],king)>dis(queens[i],king)))\n                    attackers[7]=queens[i];\n            }\n            if(king[1]-queens[i][1]==0 &&king[0]>queens[i][0]){\n                if((attackers[1].size()==0)||(dis(attackers[1],king)>dis(queens[i],king)))\n                    attackers[1]=queens[i];\n            }\n            if(king[1]-queens[i][1]==0 &&king[0]<queens[i][0]){\n                if((attackers[6].size()==0)||(dis(attackers[6],king)>dis(queens[i],king)))\n                    attackers[6]=queens[i];\n            }\n            if(king[0]-queens[i][0]==-(king[1]-queens[i][1])&&king[0]>queens[i][0]){\n                if((attackers[2].size()==0)||(dis(attackers[2],king)>dis(queens[i],king)))\n                    attackers[2]=queens[i];\n            }\n            if(king[0]-queens[i][0]==-(king[1]-queens[i][1])&&king[0]<queens[i][0]){\n                if((attackers[5].size()==0)||(dis(attackers[5],king)>dis(queens[i],king)))attackers[5]=queens[i];\n            }\n        }\n        for(int i=0;i<8;i++)if(attackers[i].size())sol.push_back(attackers[i]);\n        return sol;\n    }\n};\n```
['C']