bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1197. Minimum Knight Moves

1197. Minimum Knight Moves

Leetcode Solutions

BFS (Breadth-First Search)

  1. Initialize a queue and add the starting point (0, 0) with a step count of 0.
  2. Initialize a visited set to keep track of visited squares.
  3. While the queue is not empty, process the nodes in the queue: a. Dequeue the current square and step count from the queue. b. If the current square is the target [x, y], return the step count. c. Otherwise, iterate over all possible knight moves from the current square. d. For each move, if the new square has not been visited, add it to the queue with a step count incremented by 1 and mark it as visited.
  4. Continue this process until the target is reached.
UML Thumbnail

DFS (Depth-First Search) with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...