#include #include #include using namespace std; int moves[8][2]; // four moves knight is able to do int N; bool visited[100][100]; // visited cells inline bool inside_bounds(int xx, int yy) // checks if the given coordinates is inside the bounds (0,N-1) { return (xx >= 0 && yy >= 0 && xx <= N-1 && yy <= N-1); } int minCost(int a, int b) // calculates mininum cost with predefined moves { queue QX, QY, QCost; // Queues for current x, current y, current cost // assigning the moves moves[0][0] = a; moves[0][1] = b; moves[1][0] = a; moves[1][1] = -b; moves[2][0] = -a; moves[2][1] = b; moves[3][0] = -a; moves[3][1] = -b; moves[4][0] = b; moves[4][1] = a; moves[5][0] = b; moves[5][1] = -a; moves[6][0] = -b; moves[6][1] = a; moves[7][0] = -b; moves[7][1] = -a; // Pushing origin point and origin cost(which is zero) to queue QX.push(0); QY.push(0); QCost.push(0); for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) visited[i][j] = false; while (!QX.empty()) { int x, y, cost; x = QX.front(); y = QY.front(); cost = QCost.front(); QX.pop(); QY.pop(); QCost.pop(); if ( visited[x][y]) continue; if (x == N - 1 && y == N - 1) return cost; visited[x][y] = true; for (int i = 0; i < 8; ++i) { // Do not visit a coordinate if it is out of bounds or already visited if ( inside_bounds(x + moves[i][0], y + moves[i][1]) && !visited[x + moves[i][0]][y + moves[i][1]]) { QX.push(x + moves[i][0]); QY.push(y + moves[i][1]); QCost.push(cost + 1); } } } return -1; } int main() { cin>>N; for (int i = 1; i < N; ++i, cout<