LeetCode-1222. 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.

 

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:  
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]]

 

Constraints:

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

题解:

class Solution {
public:
    vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
        int n = queens.size();
        vector<vector<int>> res;
        int left = INT_MIN, right = INT_MAX, up = INT_MIN, down = INT_MAX, lu = INT_MIN, ld = INT_MIN, ru = INT_MAX, rd = INT_MAX;
        for (int i = 0; i < n; i++) {
            int x = queens[i][0], y = queens[i][1];
            if (x == king[0]) {
                if (y > king[1]) {
                    right = min(right, y);
                }
                if (y < king[1]) {
                    left = max(left, y);
                }
            }
            else if (y == king[1]) {
                if (x < king[0]) {
                    up = max(up, x);
                }
                if (x > king[0]) {
                    down = min(down, x);
                }
            }
            else if (x - king[0] == king[1] - y) {
                if (x > king[0]) {
                    rd = min(rd, x);
                }
                if (x < king[0]) {
                    lu = max(lu, x);
                }
            }
            else if (x - king[0] == y - king[1]) {
                if (x > king[0]) {
                    ru = min(ru, x);
                }
                if (x < king[0]) {
                    ld = max(ld, x);
                }
            }
        }
        if (left != INT_MIN) {
            res.push_back({king[0], left});
        }
        if (right != INT_MAX) {
            res.push_back({king[0], right});
        }
        if (up != INT_MIN) {
            res.push_back({up, king[1]});
        }
        if (down != INT_MAX) {
            res.push_back({down, king[1]});
        }
        if (lu != INT_MIN) {
            res.push_back({lu, king[0] + king[1] - lu});
        }
        if (rd != INT_MAX) {
            res.push_back({rd, king[0] + king[1] - rd});
        }
        if (ru != INT_MAX) {
            res.push_back({ru, ru + king[1] - king[0]});
        }
        if (ld != INT_MIN) {
            res.push_back({ld, ld + king[1] - king[0]});
        }
        return res;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值