4
$\begingroup$

I had this task in programming competition: There are two knights, which are $(p_1,q_1)$ and $(p_2, q_2)$. $(p,q)$ knight is figure, with p(q)-length first step, and q(p)-length second step in perpendicular direction. Simple chess knight is (2,1) or (1,2) knight. Given $(x_1,y_1)$ and $(x_2,y_2)$ - positions of two knights on $(W\times H)$ chess board. Find minimal path which leads both knights in same cell.

As I never saw task like this, I've started with simplier:

Given $(p,q)$ knight on $(W\times H)$ chess board in $(x_0,y_0)$ initial cell, can knight visit $(x,y)$ cell?

I can use some recursive algorithm for finding it, of course, but I've decided to look at this problem analyticaly. I made up this system:

$$pk_1+qk_2=x-x_0\\ qk_1+pk_2=y-y_0$$

Solutions of this system are: $$k_2=\frac{(y-y_0)p}{p^2-q^2}\\ k_1=\frac{x-x_0-qk_2}{p}$$

I thought, that point will be: all available for visit cells will lead to $k_1, k_2$ be integers. But from doing some examples I realised, that actually for available cells $k_1, k_2$ are rational, and for aunavailiable cells they are irrational. So I have questions:

  1. Where is my mistake in reasoning? [Answer] Because I don't take into account direction, thanks @Joffan.
  2. It is not seem to be practical way to use in program, because I can not distinguish rational from irrational in code. So what will be except recursion and linear programming?
  3. I think that this problem has some name, to google it and further reading. So where I can find some information about it?
$\endgroup$
9
  • 4
    $\begingroup$ Chess folks call your "horses" "knights" $\endgroup$ Commented May 22, 2015 at 13:58
  • 1
    $\begingroup$ Oh, I'm sorry. English is not my first language and I stupidly assumed, that figures are called same as in my language. $\endgroup$ Commented May 22, 2015 at 14:04
  • $\begingroup$ Any shortest path algorithm will do (for the question "can knight visit $(x,y)$ cell"). E.g. Dijkstra. $\endgroup$ Commented May 22, 2015 at 14:12
  • 1
    $\begingroup$ @DoctorMoisha Well, they are not and it's hard to guess the right names from a direct translation - for example, a direct translation from German would be: King, lady, runner, horse (or jumper), tower, farmer instead of the correct terms king, queen, bishop, knight, rook, pawn. - So now you at least have a reference list for the future ;) $\endgroup$ Commented May 22, 2015 at 14:25
  • 1
    $\begingroup$ @DoctorMoisha back on topic, I think the problem is that you have neglected sign. Your moves can be in eight directions and the sign of movement (direction) may be reversed for some of your move and in opposite directions for $x$ and $y$. $\endgroup$
    – Joffan
    Commented May 22, 2015 at 14:31

1 Answer 1

3
$\begingroup$

If you allow $k_1,k_2$ to be integers, you have accounted for moves $(p,q), (-p,-q), (q,p), (-q,-p)$, but have not allowed $(p,-q),(-p,q),(q,-p),(-q,p)$ You are correct that $k_1,k_2$ should be integers but need $k_3,k_4$ for the other moves available. Then your intuition that the $k$'s should be integers is correct. The solution will not be unique as there are paths that return to the starting point. If you can move to a neighboring square, you can get anywhere. For example, with a standard $(2,1)$ knight you can move $(0,0) \to (1,2) \to (-1,1) \to (0,1)$, showing you can get to any square. If you can't get to every square there is a nice pattern that repeats of the squares you can get to. If $p$ and $q$ have a common factor $n$ you will only be able to get to squares that are multiples of $n$ in the orthogonal directions. There can also be parity restrictions. With a $(1,1)$ leaper you can't change colors so can only reach half the squares.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .