Skip to content

Commit bc55c52

Browse files
authored
feat: add solutions to lc problem: No.1222 (doocs#1585)
No.1222.Queens That Can Attack the King
1 parent 058a314 commit bc55c52

File tree

7 files changed

+286
-224
lines changed

7 files changed

+286
-224
lines changed

Diff for: solution/1200-1299/1222.Queens That Can Attack the King/README.md

+93-68
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66-
先将所有 queens 存放到一个哈希表中。
66+
**方法一:直接搜索**
6767

68-
然后从 king 位置,循环遍历 “上、下、左、右、对角线”等 8 个方向。对于每个方向,碰到第一个 queen 时,将该 queen 加到结果列表中,然后结束此方向的遍历
68+
我们先将所有皇后的位置存入哈希表或者二维数组 $s$ 中
6969

70-
最后返回结果列表 ans 即可。
70+
接下来,我们从国王的位置开始,依次向上、下、左、右、左上、右上、左下、右下八个方向搜索,如果某个方向上存在皇后,那么就将其位置加入答案中,并且停止继续搜索该方向。
71+
72+
搜索结束后,返回答案即可。
73+
74+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。本题中 $n = 8$。
7175

7276
<!-- tabs:start -->
7377

@@ -83,22 +87,15 @@ class Solution:
8387
n = 8
8488
s = {(i, j) for i, j in queens}
8589
ans = []
86-
for a, b in [
87-
[-1, 0],
88-
[1, 0],
89-
[0, -1],
90-
[0, 1],
91-
[1, 1],
92-
[1, -1],
93-
[-1, 1],
94-
[-1, -1],
95-
]:
96-
x, y = king
97-
while 0 <= x + a < n and 0 <= y + b < n:
98-
x, y = x + a, y + b
99-
if (x, y) in s:
100-
ans.append([x, y])
101-
break
90+
for a in range(-1, 2):
91+
for b in range(-1, 2):
92+
if a or b:
93+
x, y = king
94+
while 0 <= x + a < n and 0 <= y + b < n:
95+
x, y = x + a, y + b
96+
if (x, y) in s:
97+
ans.append([x, y])
98+
break
10299
return ans
103100
```
104101

@@ -108,35 +105,30 @@ class Solution:
108105

109106
```java
110107
class Solution {
111-
private static final int N = 8;
112-
private int[][] dirs
113-
= new int[][] {{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
114-
115108
public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
116-
Set<Integer> s = get(queens);
109+
final int n = 8;
110+
var s = new boolean[n][n];
111+
for (var q : queens) {
112+
s[q[0]][q[1]] = true;
113+
}
117114
List<List<Integer>> ans = new ArrayList<>();
118-
for (int[] dir : dirs) {
119-
int x = king[0], y = king[1];
120-
int a = dir[0], b = dir[1];
121-
while (x + a >= 0 && x + a < N && y + b >= 0 && y + b < N) {
122-
x += a;
123-
y += b;
124-
if (s.contains(x * N + y)) {
125-
ans.add(Arrays.asList(x, y));
126-
break;
115+
for (int a = -1; a <= 1; ++a) {
116+
for (int b = -1; b <= 1; ++b) {
117+
if (a != 0 || b != 0) {
118+
int x = king[0] + a, y = king[1] + b;
119+
while (x >= 0 && x < n && y >= 0 && y < n) {
120+
if (s[x][y]) {
121+
ans.add(List.of(x, y));
122+
break;
123+
}
124+
x += a;
125+
y += b;
126+
}
127127
}
128128
}
129129
}
130130
return ans;
131131
}
132-
133-
private Set<Integer> get(int[][] queens) {
134-
Set<Integer> ans = new HashSet<>();
135-
for (int[] queen : queens) {
136-
ans.add(queen[0] * N + queen[1]);
137-
}
138-
return ans;
139-
}
140132
}
141133
```
142134

@@ -146,20 +138,24 @@ class Solution {
146138
class Solution {
147139
public:
148140
vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {
149-
unordered_set<int> s;
150141
int n = 8;
151-
for (auto& queen : queens) s.insert(queen[0] * n + queen[1]);
152-
vector<vector<int>> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
142+
bool s[8][8]{};
143+
for (auto& q : queens) {
144+
s[q[0]][q[1]] = true;
145+
}
153146
vector<vector<int>> ans;
154-
for (auto& dir : dirs) {
155-
int x = king[0], y = king[1];
156-
int a = dir[0], b = dir[1];
157-
while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n) {
158-
x += a;
159-
y += b;
160-
if (s.count(x * n + y)) {
161-
ans.push_back({x, y});
162-
break;
147+
for (int a = -1; a <= 1; ++a) {
148+
for (int b = -1; b <= 1; ++b) {
149+
if (a || b) {
150+
int x = king[0] + a, y = king[1] + b;
151+
while (x >= 0 && x < n && y >= 0 && y < n) {
152+
if (s[x][y]) {
153+
ans.push_back({x, y});
154+
break;
155+
}
156+
x += a;
157+
y += b;
158+
}
163159
}
164160
}
165161
}
@@ -171,26 +167,55 @@ public:
171167
### **Go**
172168
173169
```go
174-
func queensAttacktheKing(queens [][]int, king []int) [][]int {
175-
s := make(map[int]bool)
170+
func queensAttacktheKing(queens [][]int, king []int) (ans [][]int) {
176171
n := 8
177-
for _, queen := range queens {
178-
s[queen[0]*n+queen[1]] = true
172+
s := [8][8]bool{}
173+
for _, q := range queens {
174+
s[q[0]][q[1]] = true
179175
}
180-
dirs := [8][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}
181-
var ans [][]int
182-
for _, dir := range dirs {
183-
x, y := king[0], king[1]
184-
a, b := dir[0], dir[1]
185-
for x+a >= 0 && x+a < n && y+b >= 0 && y+b < n {
186-
x, y = x+a, y+b
187-
if s[x*n+y] {
188-
ans = append(ans, []int{x, y})
189-
break
176+
for a := -1; a <= 1; a++ {
177+
for b := -1; b <= 1; b++ {
178+
if a != 0 || b != 0 {
179+
x, y := king[0]+a, king[1]+b
180+
for 0 <= x && x < n && 0 <= y && y < n {
181+
if s[x][y] {
182+
ans = append(ans, []int{x, y})
183+
break
184+
}
185+
x += a
186+
y += b
187+
}
190188
}
191189
}
192190
}
193-
return ans
191+
return
192+
}
193+
```
194+
195+
### **TypeScript**
196+
197+
```ts
198+
function queensAttacktheKing(queens: number[][], king: number[]): number[][] {
199+
const n = 8;
200+
const s: boolean[][] = Array.from({ length: n }, () => Array.from({ length: n }, () => false));
201+
queens.forEach(([x, y]) => (s[x][y] = true));
202+
const ans: number[][] = [];
203+
for (let a = -1; a <= 1; ++a) {
204+
for (let b = -1; b <= 1; ++b) {
205+
if (a || b) {
206+
let [x, y] = [king[0] + a, king[1] + b];
207+
while (x >= 0 && x < n && y >= 0 && y < n) {
208+
if (s[x][y]) {
209+
ans.push([x, y]);
210+
break;
211+
}
212+
x += a;
213+
y += b;
214+
}
215+
}
216+
}
217+
}
218+
return ans;
194219
}
195220
```
196221

0 commit comments

Comments
 (0)