【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
queensthat represents the positions of the Black Queens, and a pair of coordinateskingthat 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 <= 63queens[0].length == 20 <= queens[i][j] < 8king.length == 20 <= king[0], king[1] < 8- At most one piece is allowed in a cell.
解题思路:往king的上下左右,上左,上右,下左,下右八个方向移动,如果某个方向遇到queen,则表示这个queen能攻击到king,然后停止这个方向的移动。
代码如下:
class Solution(object):
def queensAttacktheKing(self, queens, king):
"""
:type queens: List[List[int]]
:type king: List[int]
:rtype: List[List[int]]
"""
res = []
dic = {}
for (x,y) in queens:dic[(x,y)] = 1
direction = [(0,1),(0,-1),(-1,0),(1,0),(-1,-1),(-1,1),(1,-1),(1,1)]
for (x,y) in direction:
kx,ky = king
while kx + x >= 0 and kx + x < 8 and ky+y>=0 and ky+ y < 8:
if (kx+x,ky+y) in dic:
res.append([kx+x,ky+y])
break
kx += x
ky += y
return res
【leetcode】1222. Queens That Can Attack the King的更多相关文章
- 【LeetCode】1222. Queens That Can Attack the King 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
随机推荐
- Spring 中如何自动创建代理(spring中的三种自动代理创建器)
Spring 提供了自动代理机制,可以让容器自动生成代理,从而把开发人员从繁琐的配置中解脱出来 . 具体是使用 BeanPostProcessor 来实现这项功能. 这三种自动代理创建器 为:Bean ...
- vc/vs常见报错:/****error C2106: '=' : left operand must be l-value****/
一.错误信息解析: 1.error,表示这是一条出错信息. C语言信息一般有error(出错)和warning(警告)两种. error是编译器遇到了致命错误,无法继续进行编译,必须修改. warni ...
- USACO1.5 Mother's Milk【搜索】
题目传送门 这道题还记得是我当年学广搜的时候做过. 如今再做,做了一个$dfs$版本的,比较简单,直接搞就可以了. 广搜的话,用结构体保存,然后塞到$queue$里面就可以了. /* ID: Star ...
- 第七次学习总结&&第五次实验报告
一.实验目的 (1)理解抽象类与接口的使用: (2)了解包的作用,掌握包的设计方法. 二.实验要求 (1)掌握使用抽象类的方法. (2)掌握使用系统接口的技术和创建自定义接口的方法. (3)了解 Ja ...
- C语言Ⅰ作业12—学期总结
一.我学到的内容 二.我的收获 作业链接 收获 C语言Ⅰ博客作业01 认识了PTA编程,博客园,Markdown基本语法1,Markdown基本语法2 C语言Ⅰ博客作业02 PTA系统常见问题解答 C ...
- HTML标签-->段落,格式,文本
只有努力奔跑,才能一直停留在原地. <!--段落标签--> <h1>默认向左</h1> <h1 align="right">向右对齐 ...
- String,StringBuffer,StringBulider 三者的区别
1.String 是字符串常量,StringBuffer 和StringBuilder 是字符串变量. 2.运行速度 StringBuilder > StringBuffer > Stri ...
- 三校联训 【NOIP模拟】寻找
题面 “我有个愿望,我希望穿越一切找到你.” 这是个二维平面世界,平面上有n个特殊的果实,我从(0,0)点出发,希望得到尽量多的果实,但是出于某种特殊的原因,我的运动方式只有三种(假设当前我在(x,y ...
- js知识点——2之navigator
navigator(领航者) 1.appCodeName(返回浏览器的代码名) var x= navigator; document.write("CodeName:"+x.app ...
- PHP中时区(timezone)设置的三种方法(转)
方法1: (最好的方法)在php.ini里加上找到date.timezone项,设置date.timezone = "Asia/Shanghai",重启环境就ok了. 方法2: 在 ...


