#include #include static int distance[26][26] = {{0}}; class Pos { public: unsigned x; unsigned y; Pos(unsigned a, unsigned b) : x(a), y(b) {}; }; bool insert_move(std::list& pq, int d[26][26], unsigned n, unsigned x, unsigned y, int v) { if(x < 1 || x > n || y < 1 || y > n || d[x][y] != 0) return false; d[x][y] = v; pq.emplace_back(x, y); return x==1 && y==1; } bool insert_all_moves(std::list& pq, int d[26][26], unsigned n, unsigned x, unsigned y, unsigned d1, unsigned d2) { if(insert_move(pq, d, n, x-d1, y-d2, d[x][y]+1)) return true; if(insert_move(pq, d, n, x-d1, y+d2, d[x][y]+1)) return true; if(insert_move(pq, d, n, x+d1, y-d2, d[x][y]+1)) return true; if(insert_move(pq, d, n, x+d1, y+d2, d[x][y]+1)) return true; if(insert_move(pq, d, n, x-d2, y-d1, d[x][y]+1)) return true; if(insert_move(pq, d, n, x-d2, y+d1, d[x][y]+1)) return true; if(insert_move(pq, d, n, x+d2, y-d1, d[x][y]+1)) return true; if(insert_move(pq, d, n, x+d2, y+d1, d[x][y]+1)) return true; return false; } unsigned knight_moves(unsigned n, unsigned i, unsigned j) { int d[26][26] = {{0}}; std::list pq; pq.emplace_back(n, n); int res = -1; while(!pq.empty()) { Pos p = pq.front(); pq.pop_front(); if(insert_all_moves(pq, d, n, p.x, p.y, i, j)) break; } return (d[1][1] == 0 ? -1 : d[1][1]); } int main() { unsigned n; std::cin >> n; int solution[26][26] = {{0}}; for(unsigned i=1; i