[LeetCode] 2596. Check Knight Tour Configuration

김민우·2023년 4월 8일
0

알고리즘

목록 보기
174/189

- Problem

2596. Check Knight Tour Configuration

- 내 풀이(BFS)

D = ((-2, 1), (-2, -1), (2, -1), (2, 1), (-1, 2), (-1, -2), (1, -2), (1, 2))

class Solution:
    def checkValidGrid(self, grid: List[List[int]]) -> bool:
        N = len(grid)
        q = deque([(0, 0)])
        moves = 1

        while q:
            y, x = q.popleft()

            for dy, dx in D:
                ny = y + dy
                nx = x + dx

                if 0 <= ny < N and 0 <= nx < N and grid[ny][nx] == grid[y][x] + 1:
                    q.append((ny, nx))
                    moves += 1

        return moves == (N**2)

- 결과

  • 시간 복잡도: O(N*N)
  • 공간 복잡도: O(N*N)
profile
Pay it forward.

1개의 댓글

comment-user-thumbnail
2025년 8월 27일

Planning an Oahu to Maui island tour is like solving a puzzle where each move matters, much like the logic behind the Knight Tour Configuration challenge. Both require strategy, precision, and an eye for detail to enjoy the journey or complete the problem effectively. You can also explore engaging reads on travel and culture at https://zunitive.com/club-mobay-sangster-airport-vip-lounge-with-fast-track-entry/> for more inspiration.

답글 달기