Find the fewest knight moves to a square on an infinite chessboard

mediumAbout 25 minutesUpdated CodingPrographsbfsgridssymmetry
Not solved

Share this question

Problem

A knight moves two squares in one direction and one square at right angles: eight possible moves. On an infinite chessboard, starting at (0, 0), write minKnightMoves(x, y), which returns the fewest moves needed to land on (x, y).

minKnightMoves(2, 1);   // 1
minKnightMoves(5, 5);   // 4
minKnightMoves(1, 0);   // 3
minKnightMoves(0, 0);   // 0
minKnightMoves(-1, -2); // 1

Ref. UR-1426 · © UIRound · uiround.com/questions/find-the-fewest-knight-moves-to-a-square-on-an-infinite-chessboard

Stuck? Take a hint

Try it in the editor

Run your code as often as you like. Submitting it to the full test suite is part of Pro.

Private, and saved in this browser as you type. All your notes are on My lists.

Every question and answer7 days, one payment