bool reachable_in( unsigned n, char x1, char y1, char x2, char y2 ) { if ( x1 == x2 && y1 == y2 ) return true; if ( n == 0 || x1 >= 8 || x1 < 0 || y1 >= 8 || y1 < 0 ) return false; for ( int dx = 1; dx <= 2; ++dx ) { int dy = 3 - dx; for ( int sx = -1; sx < 2; sx += 2 ) { for ( int sy = -1; sy < 2; sy += 2 ) { if ( reachable_in( n - 1, x1 + sx * dx, y1 + sy * dy, x2, y2 ) ) return true; } } } return false; } unsigned knight_hops( char x1, char y1, char x2, char y2 ) { int n = 0; while( !reachable_in( n, x1, y1, x2, y2 ) ) ++ n; return n; }