avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

1222.Can attack the queen of the king

  2024-01-01        
字数统计: 358字   |   阅读时长: 2min

topic:

screenshot2023-09-15 morning12.10.07.png
topic链接

Thought:

直接simulation,Determine horizontal,Vertical,Whether the queen on the diagonal line exists,if it exists,Just record,Last return。

The answer method is similar to mine,But more concise,Go out of the king,The first queen that recording encountered。

Code:

I am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution:
def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
x = king[0]
y = king[1]
x_left_diff = 64
x_right_diff = 64
left_high_diff = 64
left_low_diff = 64
right_high_diff = 64
right_low_diff = 64
y_left_diff = 64
y_right_diff = 64
res = [[]] * 8
print(res)
for i, j in queens:
# Horizontal
if i == x:
if j < y and abs(j - y) < x_left_diff:
res[0] = [i, j]
x_left_diff = abs(j - y)
elif j > y and abs(j - y) < x_right_diff:
res[1] = [i, j]
x_right_diff = abs(j - y)
# Vertical
elif j == y:
if i < x and abs(i - x) < y_left_diff:
res[6] = [i, j]
y_left_diff = abs(i - x)
elif i > x and abs(i - x) < y_right_diff:
res[7] = [i, j]
y_right_diff = abs(i - x)
# Diagonal
elif abs(i - x) == abs(j - y):
if i < x and j < y and abs(i - x) < left_high_diff:
res[2] = [i, j]
left_high_diff = abs(i - x)
elif i < x and j > y and abs(i - x) < left_low_diff:
res[3] = [i, j]
left_low_diff = abs(i - x)
elif i > x and j < y and abs(i - x) < right_high_diff:
res[4] = [i, j]
right_high_diff = abs(i - x)
elif i > x and j > y and abs(i - x) < right_low_diff:
res[5] = [i, j]
right_low_diff = abs(i - x)
return [i for i in res if i != []]
Answer
1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) -> List[List[int]]:
s = set(map(tuple, queens))
ans = []
for dx, dy in (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1):
x, y = king[0] + dx, king[1] + dy
while 0 <= x < 8 and 0 <= y < 8:
if (x, y) in s:
ans.append([x, y])
break
x += dx
y += dy
return ans
  • Python
  • answer
  • Array
  • matrix
  • simulation

扫一扫,分享到微信

微信分享二维码
122.The best time for buying and selling stocksII
1333.Restaurant filter
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号