class Solution:
    def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
        ans = []
        d = {(i[0],i[1]) : True for i in queens}
        def goUp(r,c):
            while r >=0:
                if (r,c) in d:
                    ans.append([r,c])
                    break
                r -= 1
        def goDown(r,c):
            while r < 8:
                if (r,c) in d: 
                    ans.append([r,c])
                    break
                r += 1
        def goLeft(r,c):
            while c >= 0:
                if (r,c) in d:
                    ans.append([r,c])
                    break
                c -= 1
        def goRight(r,c):
            while c < 8:
                if (r,c) in d: 
                    ans.append([r,c])
                    break
                c += 1
        def goD1(r,c):
            while r >=0 and c >= 0:
                if (r,c) in d:
                    ans.append([r,c])
                    break
                r -= 1
                c -= 1
        def goD2(r,c):
            while r < 8 and c >= 0:
                if (r,c) in d: 
                    ans.append([r,c])
                    break
                r += 1
                c -= 1
        def goD3(r,c):
            while r < 8 and c < 8:
                if (r,c) in d: 
                    ans.append([r,c])
                    break
                r += 1
                c += 1
        def goD4(r,c):
            while r >= 0 and c < 8:
                if (r,c) in d: 
                    ans.append([r,c])
                    break
                r -= 1
                c += 1

        goUp(king[0],king[1])
        goDown(king[0],king[1])
        goLeft(king[0],king[1])
        goRight(king[0],king[1])
        goD1(king[0],king[1])
        goD2(king[0],king[1])
        goD3(king[0],king[1])
        goD4(king[0],king[1])

        return ans
class Solution {
    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        List<List<Integer>> res = new ArrayList<>();
        int r=king[0];
        int l=king[1];
        int [][]board =new int[8][8];
        int n=8;

// 8 cases;
// moving upward
// moving downward
// moving right
// moving left
// moving upper right diagonal
// moving upper left diagonal'
// moving lower left diagonal
// moving lower right diagonal

        int i,j;
        for(i=0;i<queens.length;i++)
        {
            for(j=0;j<1;j++)
            {
                int row=queens[i][j];
                int column=queens[i][j+1];
                board[row][column]=1;
            }

        }
        // for(i=0;i<8;i++)
        // {
        // for(j=0;j<8;j++)
        // {
        // System.out.println(board[i][j]);
        // }
        // System.out.println();
        // }
        for(j=l;j<n;j++)
        {
            if(board[r][j]==1)
            {
                res.add(new ArrayList<>(Arrays.asList(r, j)));
                break;
            }
        }
        for(i=r;i<n;i++)
        {
            if(board[i][l]==1)
            {
                 res.add(new ArrayList<>(Arrays.asList(i, l)));
                 break;
            }
        }
        for(i=r;i>=0;i--)
        {
            if(board[i][l]==1)
            {
                 res.add(new ArrayList<>(Arrays.asList(i, l)));
                 break;

            }
        }
        for(j=l;j>=0;j--)
        {
            if(board[r][j]==1)
            {
                 res.add(new ArrayList<>(Arrays.asList(r, j)));
                 break;

            }
        }
        for(i=r,j=l;i>=0 && j>=0;j--,i--)
        {
            if(board[i][j]==1)
            {
                 res.add(new ArrayList<>(Arrays.asList(i, j)));
                 break;
            }
        }
        for(i=r,j=l;j<n && i>=0;j++,i--)
        {
            if(board[i][j]==1)
            {
                res.add(new ArrayList<>(Arrays.asList(i, j)));
                break;
            }
        }
        for(i=r,j=l;i<n && j<n;j++,i++)
        {
            if(board[i][j]==1)
            {
                 res.add(new ArrayList<>(Arrays.asList(i, j)));
                 break;
            }
        }
        for(i=r,j=l;j>=0 && i<n;j--,i++)
        {
            if(board[i][j]==1)
            {
                 res.add(new ArrayList<>(Arrays.asList(i, j)));
                 break;
            }
        }
        return res;
    }
}
Queens That Can Attack the King
On an 8x8 chessboard, there can be multiple Black Queens and one White King.

Given an array of integer coordinates queens that represents the positions of the Black Queens, and a pair of coordinates king that represent the position of the White King, return the coordinates of all the queens (in any order) that can attack the King.
&nbsp;
Example 1:



Input: queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
Output: [[0,1],[1,0],[3,3]]
Explanation:&nbsp; 
The queen at [0,1] can attack the king cause they're in the same row. 
The queen at [1,0] can attack the king cause they're in the same column. 
The queen at [3,3] can attack the king cause they're in the same diagnal. 
The queen at [0,4] can't attack the king cause it's blocked by the queen at [0,1]. 
The queen at [4,0] can't attack the king cause it's blocked by the queen at [1,0]. 
The queen at [2,4] can't attack the king cause it's not in the same row/column/diagnal as the king.


Example 2:



Input: queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
Output: [[2,2],[3,4],[4,4]]


Example 3:



Input: queens = [[5,6],[7,7],[2,1],[0,7],[1,6],[5,1],[3,7],[0,3],[4,0],[1,2],[6,3],[5,0],[0,4],[2,2],[1,1],[6,4],[5,4],[0,0],[2,6],[4,5],[5,2],[1,4],[7,5],[2,3],[0,5],[4,2],[1,0],[2,7],[0,1],[4,6],[6,1],[0,6],[4,3],[1,7]], king = [3,4]
Output: [[2,3],[1,4],[1,6],[3,7],[4,3],[5,4],[4,5]]

&nbsp;
Constraints:


	1 &lt;= queens.length&nbsp;&lt;= 63
	queens[i].length == 2
	0 &lt;= queens[i][j] &lt;&nbsp;8
	king.length == 2
	0 &lt;= king[0], king[1] &lt; 8
	At most one piece is allowed in a cell.


/**
 * @param {number[][]} queens
 * @param {number[]} king
 * @return {number[][]}
 */
var queensAttacktheKing = function(queens, king) {
    const map = {}, res = []
    for (let queen of queens) {
        map[queen] = 1
    }
    
    for (let i = -1; i < 2; i++) {
        for (let j = -1; j < 2; j++) {
            if (i==0 && j==0) continue
            for (let k = 1; k < 8; k++) {
                let x = king[0] + k*i
                let y = king[1] + k*j
                if (x < 0 || y < 0 || x > 7|| y > 7) break
                if ([x,y] in map) {
                    res.push([x,y])
                    break
                }
            }
        }
    }
    
    return res
};
class Solution {
public:
    vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) 
    {
        vector<vector<int>> ans;
        vector<vector<int>> board(8, vector<int>(8, 0));

        for(auto queen: queens) board[queen[0]][queen[1]] = 1;
        
        for(int x=-1; x<=1; x++)  // Both loops are for checking in all the 8 possible directions
        {
            for(int y=-1; y<=1; y++)
            {
                if(x == 0 and y == 0) continue;
                int startx = king[0], starty = king[1];
                
                while(startx >= 0 and startx < 8 and starty >= 0 and starty < 8)
                {
                    if(board[startx][starty] == 1)  // If queen is found, append it to ans and break
                    {
                        ans.push_back({startx, starty});
                        break;
                    }
                    startx += x, starty += y;      // Otherwise keep moving forward in earlier direction
                }
            }
        }
        return ans;
    }
};