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

2596.Check the Cavaliers patrol plan

  2024-01-01        
字数统计: 215字   |   阅读时长: 1min

topic:

screenshot2023-09-13 afternoon5.14.07.png
topic链接

Thought:

I thought it was at first glance NQueen question,Then read it for a long time and didn’t read the question for a long time,So take a look at the answer()。
turn out to begrid[row][col] Index cells (row, col) It is the first of the Cavaliers grid[row][col] Cell。meanspos[grid[i][j]] = (i, j)
Then I understand,Set the initial value(2,1),Then from(0,0)Start traverse each point andlastIs the difference in the value of the value?2,1or1,2,if not,Just returnFalse。

In the answer,ylbUsepairwiseThe method of calculating the two elements of the front and rear
for (x1, y1), (x2, y2) in pairwise(pos): dx, dy = abs(x1 - x2), abs(y1 - y2)

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution:
def checkValidGrid(self, grid: List[List[int]]) -> bool:
if grid[0][0]:
return False
n = len(grid)
pos = [(0, 0)] * (n * n)
for i in range(n):
for j in range(n):
pos[grid[i][j]] = (i, j)
last = -2, 1
for i, j in pos:
if (abs(i - last[0]) == 2 and abs(j - last[1]) == 1) or \
(abs(i - last[0]) == 1 and abs(j - last[1]) == 2):
pass
else:
print(i, j)
return False
last = (i, j)

return True
  • Python
  • answer
  • Array
  • matrix
  • simulation
  • Priority search
  • In -depth priority search

扫一扫,分享到微信

微信分享二维码
2591.Child that divides money the most
2 .Two numbers
目录
  1. 1. topic:
  2. 2. Thought:
  3. 3. Code:

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