#include #include #include #include #include #include #include int a; int b; int n; void jump(int *, int, int, int); void print(int *, int); int main(){ scanf("%d", &n); int **res = calloc(n - 1, sizeof(int *)); for (int i = 0; i < n; i++) res[i] = calloc(n - 1, sizeof(int)); a = 2; b = 1; int *mat = calloc(n * n, sizeof(int)); /* print(mat, n); jump(mat, n - 1, n - 1, 0); print(mat, n); */ for (int i = 0; i < n - 1; i++) { for (int j = i; j < n - 1; j++) { int *mat = calloc(n * n, sizeof(int)); a = i + 1; b = j + 1; jump(mat, n - 1, n - 1, 0); res[i][j] = mat[0] ? mat[0] : -1; } } for (int i = 0; i < n - 1; i++) for (int j = 0; j < i; j++) res[i][j] = res[j][i]; for (int i = 0; i < n - 1; i++) for (int j = 0; j < n - 1; j++) if (j == n - 2) printf("%d\n", res[i][j]); else printf("%d ", res[i][j]); return 0; } void jump(int *mat, int x, int y, int step) { if ((x < 0) || (x >= n) || (y < 0) || (y >= n)) { //printf("(%d, %d) = %d:%d\n", x, y, step, n); return; } if (mat[x * n + y] > step || !mat[x * n + y]) { mat[x * n + y] = step; if (x != 0 || y != 0) { jump(mat, x + a, y + b, step + 1); jump(mat, x + a, y - b, step + 1); jump(mat, x - a, y + b, step + 1); jump(mat, x - a, y - b, step + 1); jump(mat, x + b, y + a, step + 1); jump(mat, x + b, y - a, step + 1); jump(mat, x - b, y + a, step + 1); jump(mat, x - b, y - a, step + 1); } } } void print(int *mat, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) printf("%d ", mat[i * n + j]); printf("\n"); } }