Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4970aed

Browse files
committed
feat: add solutions to lc problem: No.1222
No.1222.Queens That Can Attack the King
1 parent 164d1c7 commit 4970aed

File tree

6 files changed

+294
-22
lines changed

6 files changed

+294
-22
lines changed

‎solution/1200-1299/1222.Queens That Can Attack the King/README.md‎

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,130 @@
6363
<li>一个棋盘格上最多只能放置一枚棋子。</li>
6464
</ul>
6565

66-
6766
## 解法
6867

6968
<!-- 这里可写通用的实现逻辑 -->
7069

70+
先将所有 queens 存放到一个哈希表中。
71+
72+
然后从 king 位置,循环遍历 "上、下、左、右、对角线"等 8 个方向。对于每个方向,碰到第一个 queen 时,将该 queen 加到结果列表中,然后结束此方向的遍历。
73+
74+
最后返回结果列表 ans 即可。
75+
7176
<!-- tabs:start -->
7277

7378
### **Python3**
7479

7580
<!-- 这里可写当前语言的特殊实现逻辑 -->
7681

7782
```python
78-
83+
class Solution:
84+
def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
85+
n = 8
86+
s = set((i, j) for i, j in queens)
87+
ans = []
88+
for a, b in [[-1, 0], [1, 0], [0, -1], [0, 1], [1, 1], [1, -1], [-1, 1], [-1, -1]]:
89+
x, y = king
90+
while 0 <= x + a < n and 0 <= y + b < n:
91+
x, y = x + a, y + b
92+
if (x, y) in s:
93+
ans.append([x, y])
94+
break
95+
return ans
7996
```
8097

8198
### **Java**
8299

83100
<!-- 这里可写当前语言的特殊实现逻辑 -->
84101

85102
```java
103+
class Solution {
104+
private static final int N = 8;
105+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
106+
107+
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
108+
Set<Integer> s = get(queens);
109+
List<List<Integer>> ans = new ArrayList<>();
110+
for (int[] dir : dirs) {
111+
int x = king[0], y = king[1];
112+
int a = dir[0], b = dir[1];
113+
while (x + a >= 0 && x + a < N && y + b >= 0 && y + b < N) {
114+
x += a;
115+
y += b;
116+
if (s.contains(x * N + y)) {
117+
ans.add(Arrays.asList(x, y));
118+
break;
119+
}
120+
}
121+
}
122+
return ans;
123+
}
124+
125+
private Set<Integer> get(int[][] queens) {
126+
Set<Integer> ans = new HashSet<>();
127+
for (int[] queen : queens) {
128+
ans.add(queen[0] * N + queen[1]);
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
141+
unordered_set<int> s;
142+
int n = 8;
143+
for (auto& queen : queens) s.insert(queen[0] * n + queen[1]);
144+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
145+
vector<vector<int>> ans;
146+
for (auto& dir : dirs)
147+
{
148+
int x = king[0], y = king[1];
149+
int a = dir[0], b = dir[1];
150+
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n)
151+
{
152+
x += a;
153+
y += b;
154+
if (s.count(x * n + y))
155+
{
156+
ans.push_back({x, y});
157+
break;
158+
}
159+
}
160+
}
161+
return ans;
162+
}
163+
};
164+
```
86165
166+
### **Go**
167+
168+
```go
169+
func queensAttacktheKing(queens [][]int, king []int) [][]int {
170+
s := make(map[int]bool)
171+
n := 8
172+
for _, queen := range queens {
173+
s[queen[0]*n+queen[1]] = true
174+
}
175+
dirs := [8][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
176+
var ans [][]int
177+
for _, dir := range dirs {
178+
x, y := king[0], king[1]
179+
a, b := dir[0], dir[1]
180+
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n {
181+
x, y = x+a, y+b
182+
if s[x*n+y] {
183+
ans = append(ans, []int{x, y})
184+
break
185+
}
186+
}
187+
}
188+
return ans
189+
}
87190
```
88191

89192
### **...**

‎solution/1200-1299/1222.Queens That Can Attack the King/README_EN.md‎

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,12 @@
66

77
<p>On an <strong>8x8</strong> chessboard, there can be multiple Black Queens and one White King.</p>
88

9-
10-
119
<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>
1210
<p>&nbsp;</p>
1311
<p><strong>Example 1:</strong></p>
1412

15-
16-
1713
<p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1222.Queens%20That%20Can%20Attack%20the%20King/images/untitled-diagram.jpg" style="width: 321px; height: 321px;" /></p>
1814

19-
20-
2115
<pre>
2216

2317
<strong>Input:</strong> queens = [[0,1],[1,0],[4,0],[0,4],[3,3],[2,4]], king = [0,0]
@@ -40,16 +34,10 @@ The queen at [2,4] can&#39;t attack the king cause it&#39;s not in the same row/
4034

4135
</pre>
4236

43-
44-
4537
<p><strong>Example 2:</strong></p>
4638

47-
48-
4939
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1222.Queens%20That%20Can%20Attack%20the%20King/images/untitled-diagram-1.jpg" style="width: 321px; height: 321px;" /></strong></p>
5040

51-
52-
5341
<pre>
5442

5543
<strong>Input:</strong> queens = [[0,0],[1,1],[2,2],[3,4],[3,5],[4,4],[4,5]], king = [3,3]
@@ -58,16 +46,10 @@ The queen at [2,4] can&#39;t attack the king cause it&#39;s not in the same row/
5846

5947
</pre>
6048

61-
62-
6349
<p><strong>Example 3:</strong></p>
6450

65-
66-
6751
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1222.Queens%20That%20Can%20Attack%20the%20King/images/untitled-diagram-2.jpg" style="width: 321px; height: 321px;" /></strong></p>
6852

69-
70-
7153
<pre>
7254

7355
<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]
@@ -87,21 +69,118 @@ The queen at [2,4] can&#39;t attack the king cause it&#39;s not in the same row/
8769
<li>At most one piece is allowed in a cell.</li>
8870
</ul>
8971

90-
9172
## Solutions
9273

9374
<!-- tabs:start -->
9475

9576
### **Python3**
9677

9778
```python
98-
79+
class Solution:
80+
def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
81+
n = 8
82+
s = set((i, j) for i, j in queens)
83+
ans = []
84+
for a, b in [[-1, 0], [1, 0], [0, -1], [0, 1], [1, 1], [1, -1], [-1, 1], [-1, -1]]:
85+
x, y = king
86+
while 0 <= x + a < n and 0 <= y + b < n:
87+
x, y = x + a, y + b
88+
if (x, y) in s:
89+
ans.append([x, y])
90+
break
91+
return ans
9992
```
10093

10194
### **Java**
10295

10396
```java
97+
class Solution {
98+
private static final int N = 8;
99+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
100+
101+
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
102+
Set<Integer> s = get(queens);
103+
List<List<Integer>> ans = new ArrayList<>();
104+
for (int[] dir : dirs) {
105+
int x = king[0], y = king[1];
106+
int a = dir[0], b = dir[1];
107+
while (x + a >= 0 && x + a < N && y + b >= 0 && y + b < N) {
108+
x += a;
109+
y += b;
110+
if (s.contains(x * N + y)) {
111+
ans.add(Arrays.asList(x, y));
112+
break;
113+
}
114+
}
115+
}
116+
return ans;
117+
}
118+
119+
private Set<Integer> get(int[][] queens) {
120+
Set<Integer> ans = new HashSet<>();
121+
for (int[] queen : queens) {
122+
ans.add(queen[0] * N + queen[1]);
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
135+
unordered_set<int> s;
136+
int n = 8;
137+
for (auto& queen : queens) s.insert(queen[0] * n + queen[1]);
138+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
139+
vector<vector<int>> ans;
140+
for (auto& dir : dirs)
141+
{
142+
int x = king[0], y = king[1];
143+
int a = dir[0], b = dir[1];
144+
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n)
145+
{
146+
x += a;
147+
y += b;
148+
if (s.count(x * n + y))
149+
{
150+
ans.push_back({x, y});
151+
break;
152+
}
153+
}
154+
}
155+
return ans;
156+
}
157+
};
158+
```
104159
160+
### **Go**
161+
162+
```go
163+
func queensAttacktheKing(queens [][]int, king []int) [][]int {
164+
s := make(map[int]bool)
165+
n := 8
166+
for _, queen := range queens {
167+
s[queen[0]*n+queen[1]] = true
168+
}
169+
dirs := [8][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
170+
var ans [][]int
171+
for _, dir := range dirs {
172+
x, y := king[0], king[1]
173+
a, b := dir[0], dir[1]
174+
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n {
175+
x, y = x+a, y+b
176+
if s[x*n+y] {
177+
ans = append(ans, []int{x, y})
178+
break
179+
}
180+
}
181+
}
182+
return ans
183+
}
105184
```
106185

107186
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
4+
unordered_set<int> s;
5+
int n = 8;
6+
for (auto& queen : queens) s.insert(queen[0] * n + queen[1]);
7+
vector<vector<int>> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
8+
vector<vector<int>> ans;
9+
for (auto& dir : dirs)
10+
{
11+
int x = king[0], y = king[1];
12+
int a = dir[0], b = dir[1];
13+
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n)
14+
{
15+
x += a;
16+
y += b;
17+
if (s.count(x * n + y))
18+
{
19+
ans.push_back({x, y});
20+
break;
21+
}
22+
}
23+
}
24+
return ans;
25+
}
26+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func queensAttacktheKing(queens [][]int, king []int) [][]int {
2+
s := make(map[int]bool)
3+
n := 8
4+
for _, queen := range queens {
5+
s[queen[0]*n+queen[1]] = true
6+
}
7+
dirs := [8][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
8+
var ans [][]int
9+
for _, dir := range dirs {
10+
x, y := king[0], king[1]
11+
a, b := dir[0], dir[1]
12+
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n {
13+
x, y = x+a, y+b
14+
if s[x*n+y] {
15+
ans = append(ans, []int{x, y})
16+
break
17+
}
18+
}
19+
}
20+
return ans
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
private static final int N = 8;
3+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
4+
5+
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
6+
Set<Integer> s = get(queens);
7+
List<List<Integer>> ans = new ArrayList<>();
8+
for (int[] dir : dirs) {
9+
int x = king[0], y = king[1];
10+
int a = dir[0], b = dir[1];
11+
while (x + a >= 0 && x + a < N && y + b >= 0 && y + b < N) {
12+
x += a;
13+
y += b;
14+
if (s.contains(x * N + y)) {
15+
ans.add(Arrays.asList(x, y));
16+
break;
17+
}
18+
}
19+
}
20+
return ans;
21+
}
22+
23+
private Set<Integer> get(int[][] queens) {
24+
Set<Integer> ans = new HashSet<>();
25+
for (int[] queen : queens) {
26+
ans.add(queen[0] * N + queen[1]);
27+
}
28+
return ans;
29+
}
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /