Is there a formula to compute the "knight distance" on an infinite board? i.e. how many step a knight need to move from (0,0) to any point (i,j)?
-
This link talks about this in what they call "the knight metric" – WaveX Jun 22 '19 at 03:46
-
I don't quite get it, it says $d_K(x,y) = \max{\left(\lceil\frac{M}{2}\rceil, \lceil\frac{M+m}{3}\rceil\right)} + (M+m) - \left(\max{\left(\lceil\frac{M}{2}\rceil, \lceil\frac{M+m}{3}\rceil\right)}\pmod{2}\right)$, while $M=\max{\left(|x|,|y|\right)}$ and $m=\min{\left(|x|, |y|\right)}$, but what does $|\cdot|$ mean? for example, if $x = (i,j)$, where $i,j>0$, does it mean $|x| = i + j$ , $|x|= max(i,j)$ or something else? – athos Jun 22 '19 at 05:45
-
1That formula is meant to give the distance when $i=x$ and $j=y.$ The notation on that page is inconsistent; it uses $x$ and $y$ to identify points (with two coordinates each) everywhere except in that formula. The formula is also presented in a somewhat silly fashion--$M+m=|x|+|y|,$ so the definition of $m$ is a bit redundant--and it appears to be incorrect (there are misplaced parentheses). A formula that appears to be correct is here: https://math.stackexchange.com/a/1137144 – David K Jun 22 '19 at 14:27
2 Answers
Drawing on the formulas from this answer, but adapted to your problem statement (moving from the square at coordinates $(0,0)$ to the square at coordinates $(i,j)$), the number of moves is $$ m(i,j) = \begin{cases} 3 & \lvert i\rvert + \lvert j\rvert = 1, \\ 4 & \lvert i\rvert = \lvert j\rvert = 2, \\ m' + ((m' + \lvert i\rvert + \lvert j\rvert) \bmod 2) & \text{otherwise}, \end{cases} $$ where $m' = \left\lceil \max\left\{\frac{\lvert i\rvert}2, \frac{\lvert j\rvert}2, \frac{\lvert i\rvert + \lvert j\rvert}3\right\}\right\rceil,$ where $\lceil t\rceil$ denotes the least integer $n$ such that $t \leq n.$
This result is slightly simpler than the answer from which it is taken, because your question (using an infinite chessboard) does not have the restrictions of the chessboard edge implied by minimum number of steps for knight in chess.
In notation comparable to that of your other source, $m' = \max\left\{\left\lceil\frac M2\right\rceil, \left\lceil\frac{M+m}3\right\rceil\right\}$ where $M = \max\left\{\lvert i\rvert, \lvert j\rvert\right\}$ and $m = \min\left\{\lvert i\rvert, \lvert j\rvert\right\},$ which implies that $M + m = \lvert i\rvert + \lvert j\rvert.$ It turns out that inserting negative signs into the expression $(m' + \lvert i\rvert + \lvert j\rvert)$ makes no difference, because in the end the result is just that we add $1$ if the result of that expression is even and $0$ if it is odd. There is an error in the formula on that page, however, because it has moved the term $\lvert i\rvert + \lvert j\rvert$ (written $M + m$) outside the modulo-$2$ operation, which means instead of just contributing $0$ or $1$ to the final result it can contribute a number much larger than the total result should be. For example, for $i = 4,$ $j = 2,$ the correct result is $2$ moves, but the term $M + m$ alone adds $6$ to the formula on that page.
- 108,155
Before finding this site, I developed a formula for m(i,j) which is different from David K's formula but gives the same answer for every knight journey I have tested. (In mine, the target square is (x,y) rather than (i,j).) The formulas are simplest to express when written for the one-eighth of the board where 0≤y≤x, from which the results can easily be extended by symmetry to cover the whole board.
Here's the formula I posted on another site where I first saw the question being discussed:
[r] means integer part of r
WOLOG, assume 0≤y≤x.
m(1,0)=3; m(2,2)=4; else if 2y>x, m(x,y)=x-y+2[(2y-x+2)/3]; else m(x,y)=x-y-2[(x-2y)/4]
Here is the simplified form of David's formula:
⌈r⌉ means the smallest integer greater than or equal to r
WOLOG, assume 0≤y≤x.
m(1,0)=3; m(2,2)=4; else let z=⌈max(x/2,(x+y)/3)⌉; m(x,y)=z+((x+y+z) mod 2)
As I said, both formulas give the same answer for the same target square. If they are equivalent, it should be possible to prove it, but I haven't tried to do so. There are some similarities. Both have the same special cases for target squares (1,0) and (2,2). Both contain two expressions for the answer. In my formula, which expression is used depends on whether or not 2y is greater than x. In David's formula, which expression is used depends on whether or not x/2 is greater than (x+y)/3. My formula uses the integer part function and David's uses the ceiling function. My formula has a mod 3 part and a mod 4 part, while David's has a mod 2 part and a mod 3 part.
If anyone comes up with a proof that the two formulas are equivalent, please post it here.
- 11