A Knight in chess moves two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of a capital L)
Given a Knight current square in a chess board and an array of unavailable squares, calculate the minimum number of valid jumps required to reach a target square. If no route is available to reach the required target return 0.
A valid jump is a move that:
- Does not go out of the board (8x8 square board)
- Does not overlap with another piece
- Follows the Knight L shape move pattern
Input
Knight square, array of occupied squares, target square. Input might look like:
f("b3", ["c4", "h7", "g5", "a8"], "b7")
You can use a set of coordinates instead of chess algebraic notation. It can be 0 or 1-index. Example:
b3 -> (2,3)
orb3 -> (1,2)
You can assume that
Knight Square != Target Square
for every input.You can assume every input square is valid and non overlapping
Input values can be taken in any order you'd like
Output
Minimal number of valid jumps to reach a square, or 0 in case no route can be found
Test Cases
"e3", [], "f5" -> 1
"e3", [], "f6" -> 2
"c4", [], "f6" -> 3
"g1", ["f3", "g3"], "h4" -> 4
"c4", ["d6", "b6", "e5", "e3", "e4"], "f6" -> 5
"a1", [], "h8" -> 6
"g1", ["h3","h5","g2","g3","g6","f3", "e5", "e6", "d4", "d5", "c1", "c3", "c5", "b2"], "b7" -> 7
"h1", ["f2", "g3"], "h3" -> 0
"d4", ["b3", "b5", "c2", "c6", "e2", "e6", "f3", "f5"], "a8" -> 0
[x,y]
tuples instead of strings? \$\endgroup\$g1,e2,c1,b3,a5,b7
seems to be a valid path in only 5 jumps \$\endgroup\$