C++ | Intuitive Solution | Beats 100% | Check 8 Directions
Approach\nThe approach here was to create an 8 x 8 chessboard matrix vector board which will have all cells initialized to false in the beginning. I traverse th
# Approach\nThe approach here was to create an 8 x 8 chessboard matrix vector `board` which will have all cells initialized to `false` in the beginning. I traverse through all the index positions given in `queens` and put `true` at all the cells where queens are present.\n\nAfter that, I have utilized 8 For Loops to check for the 8 Directions. Every Loop statement starts at the position of the `king` on the board and looks for the first queen in that direction. On encountering the first `false` (i.e. **Presence** of the **first queen** in that direction which can and will attack), it adds that position to `canAttack` which will be eventually returned.\n\nWe check for the **first queen in every direction** because **every other queen in that direction will be blocked by that queen and therefore they cannot attack**. There can be **at most One attack** from every direction and therefore, the maximum number of attacking queens can be 8 (For one queen in each of the 8 directions).\n\n![image.png](https://assets.leetcode.com/users/images/cd72aa29-7525-4751-8492-2d816a10e74e_1685340221.3229623.png)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {\n        // to store the chessboard situation\n        vector<vector<bool>> board(8,vector<bool>(8,false));\n        vector<vector<int>> canAttack; // to store the answer\n        // put true at all queen indices\n        for(auto it:queens)\n        board[it[0]][it[1]]=true;\n        int i=king[0],j=king[1];\n        // checking above\n        for(;i>=0;i--)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        i=king[0];\n        // checking below\n        for(;i<8;i++)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        i=king[0];\n        // checking left side\n        for(;j>=0;j--)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        j=king[1];\n        // checking right side\n        for(;j<8;j++)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        j=king[1];\n        // checking upper-left diagonal\n        for(;i>=0 && j>=0;i--,j--)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        i=king[0],j=king[1];\n        // checking upper-right diagonal\n        for(;i>=0 && j<8;i--,j++)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        i=king[0],j=king[1];\n        // checking lower-left diagonal\n        for(;i<8 && j>=0;i++,j--)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        i=king[0],j=king[1];\n        // checking lower-right diagonal\n        for(;i<8 && j<8;i++,j++)\n        if(board[i][j]){\n            canAttack.push_back({i,j});\n            break;\n        }\n        return(canAttack);\n    }\n};\n```\n# P.S.\nAll sorts of constructive feedback are welcome in the comments. Share and Upvote if helpful.
['Matrix', 'Simulation', 'C++']