16
1
Given two different positions on a chess board and the type of piece, output the minimum number of moves it will take for that piece to go from one position to another.
Rules
The given piece can be King,Queen,Rook,Knight and Bishop. (This input can be taken as any 5 unique characters)
The 2 positions can be taken in any convenient format,
Example:
a8 b8 c8 d8 ... h8
a7 b7 c7 d7 ... h7
...
...
a1 b1 c1 d1 ... h1
In case the piece cannot reach there, output anything other than a positive integer.
Examples
i/p ---- o/p
King
a1,a4 3
a1,h6 7
b3,h5 6
Queen
a1,a4 1
a1,h6 2
b3,f7 1
Rook
a1,a4 1
a1,h6 2
h2,c7 2
Knight
a1,a4 3
a1,h6 4
b2,d3 1
b2,c3 2
b3,c3 3
a1,b2 4
Bishop
a1,a4 -1
a1,h6 2
b2,d3 -1
e1,h4 1
1Why do King need 12 to a1-h6? Can't King go diag? – l4m2 – 2018-11-26T07:40:39.290
@l4m2, corrected – Vedant Kandoi – 2018-11-26T07:43:14.580
Need Knight be searched? or is there easier solution? – l4m2 – 2018-11-26T07:59:10.880
The inference is that there are no pieces in between the origin and the destination, correct? – guest271314 – 2018-11-26T08:47:14.827
@guest271314, yes there are no other pieces on board. – Vedant Kandoi – 2018-11-26T08:48:07.860
@VedantKandoi can the two positions be identical? can we use 0 to indicate unreachability? – ngn – 2018-11-26T10:52:43.543
1@ngn, you can use 0 to indicate unreachability, the 2 positions will always be different. – Vedant Kandoi – 2018-11-26T11:13:11.777
@ngn, corrected typo. Corner case was nice, added – Vedant Kandoi – 2018-11-26T11:45:05.730
5All knight distances – Arnauld – 2018-11-26T15:37:42.703
Related Finds fastest piece and has different shaped boards. – Post Rock Garf Hunter – 2018-11-27T15:52:52.517
1Some definitions (such as ISO-80000-2) of natural numbers include 0. Recommend substituting with "positive integer". – None – 2018-11-28T13:48:36.770