1222. Queens That Can Attack the King**

2023-10-23 09:10
文章标签 attack king queens 1222

本文主要是介绍1222. Queens That Can Attack the King**,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1222. Queens That Can Attack the King**

https://leetcode.com/problems/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.

Example1:

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.

还有几个例子见原网页吧.

C++ 实现 1

向 8 个方向分别搜索最近的 Queen. 思路来自 LeetCode 的 Submission.

class Solution {
private:void findQueensByDirection(const vector<vector<int>>& queens,const vector<int>& king,vector<vector<int>>& res,int i, int j) {int x = king[0], y = king[1];while (x >= 0 && x < 8 && y >= 0 && y < 8) {x += i;y += j;if (std::find(queens.begin(),queens.end(),vector<int>{x, y}) != queens.end()) {res.push_back({x, y});return;}}}
public:vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {vector<vector<int>> res;for (auto &i : {-1, 0, 1}) {for (auto &j : {-1, 0, 1}) {if (i == 0 && j == 0) continue;findQueensByDirection(queens, king, res, i, j);}}return res;}
};

C++ 实现 2

思路来自: [Python] Check 8 steps in 8 Directions, 检查 8 个方向的所有格子, 使用 Hash 表保存 Queens 的坐标, 判断 queen 是否在 Hash 表中.

class Solution {
private:// https://stackoverflow.com/questions/29855908/c-unordered-set-of-vectorsstruct VectorHash {size_t operator()(const std::vector<int>& v) const {std::hash<int> hasher;size_t seed = 0;for (int i : v) {seed ^= hasher(i) + 0x9e3779b9 + (seed << 6) + (seed >> 2);}return seed;}};
public:vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {unordered_set<vector<int>, VectorHash> records;vector<vector<int>> res;for (auto &p : queens) records.insert(p);for (auto &i : {-1, 0, 1}) {for (auto &j : {-1, 0, 1}) {for (auto &k : {1, 2, 3, 4, 5, 6, 7}) {int qx = king[0] + i * k, qy = king[1] + j * k;if (records.count({qx, qy})) {res.push_back({qx, qy});break;}}}}return res;}
};

这篇关于1222. Queens That Can Attack the King**的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/266888

相关文章

预计算攻击(Precomputation Attack):概念与防范

预计算攻击(Precomputation Attack)是一种密码学中的攻击技术,攻击者通过提前计算出可能的密钥或哈希值的映射表,来减少实际攻击时的计算量和时间。预计算攻击广泛应用于针对密码散列函数和对称加密算法的攻击中,常用于破解密码哈希、数字签名等。 1. 预计算攻击的基本概念 预计算攻击的核心思想是通过在攻击前进行大量的计算工作,生成一个可能值到其哈希值(或加密值)的映射表(即“预计算表

【POJ】2728 Desert King 最优比率生成树——01分数规划【经典】

最近在刷巨巨们放出来的专题,然后没做几题就卡住了,果然还是太弱了T U T... 这次做到了一题01分数规划求解的生成树问题。 题目大意是这样的:给你一个无向完全图,每条边i都有两个权值,长度a[ i ],花费b[ i ],需要选出其中的一些边构造一颗生成树,生成树需要满足条件:∑ b [ i ] / ∑ a [ i ]最小。 这样我还是先来介绍一下01分数规划吧~ 给定一个上述的问

【HDU】3861 The King’s Problem 强连通缩点+有向图最小路径覆盖

传送门:【HDU】3861 The King’s Problem 题目分析:首先强连通缩点,因为形成一个环的王国肯定在一条路径中,这样才能保证拆的少。 然后缩点后就是DAG图了,由于题目要求的是最小路径覆盖,那么二分匹配即可。 代码如下: #include <cstdio>#include <cstring>#include <algorithm>#includ

【贪心】codeforces30D King‘s problem

直接上中文题面 King's Problem? 时间限制:3.0s  内存限制:256.0MB   Special Judge 问题描述   每一个真正的国王在他的一生中,一定会征服世界,取得 codeforces 的世界冠军,在射击场赢得粉色的熊猫(= =),并游历整个王国。   国王 Copa 已经完成了前三件事。现在他只需要游历完整个王国了。他的王国在一个无限大的笛卡尔坐标系上,每

SQL_SERVER Lockrequesttimeoutperiodexceeded MicrosoftSQLServer,错误:1222

Lockrequesttimeoutperiodexceeded.(MicrosoftSQLServer,错误:1222)  (2018-05-08 17:46:30) 转载▼      select *        from master..SysProcesses       where db_Name(dbID) = 'DM_CRS'         and spId <> @@S

CF #364 (Div. 2) (B. Cells Not Under Attack 标记)

题目连接 在一个n*n的网格上,若果某个位置上放了一个棋子,那么棋子所在的行和列就算被覆盖了,每次放一个棋子问剩余的没有覆盖的点有几个 使用,两个数组分别标记行和列,在用两个变量保存X集合的可用数,Y集合的可用数,那么放入一个棋子,检查下X,Y集合就好了 #include<cstdio>#include<algorithm>#include<iostream>#include<vect

论文《Adversarial Examples on Graph Data: Deep Insights into Attack and Defense》笔记

【IG-Attack 2019 IJCAI】本文提出了一种基于integrated gradients的对抗攻击和防御算法。对于攻击,本文证明了通过引入integrated gradients可以很容易解决离散问题,integrated gradients可以准确反映扰动某些特征或边的影响,同时仍然受益于并行计算。对于防御,本文观察到目标攻击的被攻击图在统计上不同于正常图。在此基础上,本文提出了一

Leetcode204: N-Queens II

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 有了上一题的解法,简单修改下即可 class Solution {private:int num=0;public: in

非常有趣的一道区块连CTF题目的思考————king

区块连CTF题目 区块连CTF题目king 区块连CTF题目前言一、题目以及解答二、题目分析1.进攻receive()函数2.守护king强行selfdestruct转入为什么拿不到king 前言 这道题目在于处理接受函数的知识,另外我们结合selfdestruct函数进行分析 一、题目以及解答 这是一个非常有意思的问题: 首先下面的solidity代码是本次的题

HDU 3861 The King’s Problem

http://acm.hdu.edu.cn/showproblem.php?pid=3861 The King’s Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1828    Accepted Submission