|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/split-a-string-in-balanced-strings "Split a String in Balanced Strings") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/dice-roll-simulation "Dice Roll Simulation") |
| 11 | + |
| 12 | +## [1222. Queens That Can Attack the King (Medium)](https://leetcode.com/problems/queens-that-can-attack-the-king "可以攻击国王的皇后") |
| 13 | + |
| 14 | +<p>On an <strong>8x8</strong> chessboard, there can be multiple Black Queens and one White King.</p> |
| 15 | + |
| 16 | +<p>Given an array of integer coordinates <code>queens</code> that represents the positions of the Black Queens, and a pair of coordinates <code>king</code> that represent the position of the White King, return the coordinates of all the queens (in any order) that can attack the King.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong>Example 1:</strong></p> |
| 20 | + |
| 21 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2019/10/01/untitled-diagram.jpg" style="width: 321px; height: 321px;" /></p> |
| 22 | + |
| 23 | +<pre> |
| 24 | +<strong>Input:</strong> queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0] |
| 25 | +<strong>Output:</strong> [[0,1],[1,0],[3,3]] |
| 26 | +<strong>Explanation:</strong> |
| 27 | +The queen at [0,1] can attack the king cause they're in the same row. |
| 28 | +The queen at [1,0] can attack the king cause they're in the same column. |
| 29 | +The queen at [3,3] can attack the king cause they're in the same diagnal. |
| 30 | +The queen at [0,4] can't attack the king cause it's blocked by the queen at [0,1]. |
| 31 | +The queen at [4,0] can't attack the king cause it's blocked by the queen at [1,0]. |
| 32 | +The queen at [2,4] can't attack the king cause it's not in the same row/column/diagnal as the king. |
| 33 | +</pre> |
| 34 | + |
| 35 | +<p><strong>Example 2:</strong></p> |
| 36 | + |
| 37 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/10/01/untitled-diagram-1.jpg" style="width: 321px; height: 321px;" /></strong></p> |
| 38 | + |
| 39 | +<pre> |
| 40 | +<strong>Input:</strong> queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3] |
| 41 | +<strong>Output:</strong> [[2,2],[3,4],[4,4]] |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p><strong>Example 3:</strong></p> |
| 45 | + |
| 46 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/10/01/untitled-diagram-2.jpg" style="width: 321px; height: 321px;" /></strong></p> |
| 47 | + |
| 48 | +<pre> |
| 49 | +<strong>Input:</strong> 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] |
| 50 | +<strong>Output:</strong> [[2,3],[1,4],[1,6],[3,7],[4,3],[5,4],[4,5]] |
| 51 | +</pre> |
| 52 | + |
| 53 | +<p> </p> |
| 54 | +<p><strong>Constraints:</strong></p> |
| 55 | + |
| 56 | +<ul> |
| 57 | + <li><code>1 <= queens.length <= 63</code></li> |
| 58 | + <li><code>queens[0].length == 2</code></li> |
| 59 | + <li><code>0 <= queens[i][j] < 8</code></li> |
| 60 | + <li><code>king.length == 2</code></li> |
| 61 | + <li><code>0 <= king[0], king[1] < 8</code></li> |
| 62 | + <li>At most one piece is allowed in a cell.</li> |
| 63 | +</ul> |
| 64 | + |
| 65 | +### Related Topics |
| 66 | + [[Array](https://github.com/openset/leetcode/tree/master/tag/array/README.md)] |
| 67 | + |
| 68 | +### Hints |
| 69 | +<details> |
| 70 | +<summary>Hint 1</summary> |
| 71 | +Check 8 directions around the King. |
| 72 | +</details> |
| 73 | + |
| 74 | +<details> |
| 75 | +<summary>Hint 2</summary> |
| 76 | +Find the nearest queen in each direction. |
| 77 | +</details> |
0 commit comments