#include #include struct queue { int x; int y; struct queue * next; }*startp, *endp; void enqueue(int x, int y); int dequeue(int *i, int *j); int main(int argc, char **argv) { int board[25][25]; int ans[24][24]; int n, a, b, i, j; int swap, meh, x, y, k, l, flag; int tempa, tempb; scanf("%d", &n); for(b = 1; b < n; b++) for(a = b; a < n; a++) { tempa = a; tempb = b; flag = 0; for(k = 0; k < n; k++) for(l = 0; l < n; l++) board[k][l] = 0; i = j = 0; startp = endp = NULL; while(1) { for(meh = 0; meh < 2; meh++) { if(((x = i + tempa) <= n - 1) && ((y = j + tempb) <= n - 1) && !board[x][y]) { board[x][y] = board[i][j] + 1; if((x == n - 1) && (y == n - 1)) { flag = 1; break; } enqueue(x, y); } if(((x = i + tempa) <= n - 1) && ((y = j - tempb) >= 0) && !board[x][y]) { board[x][y] = board[i][j] + 1; enqueue(x, y); } if(((x = i - tempa) >= 0) && ((y = j + tempb) <= n- 1) && !board[x][y]) { board[x][y] = board[i][j] + 1; enqueue(x, y); } if(((x = i - tempa) >= 0) && ((y = j - tempb) >= 0) && (x || y) && !board[x][y]) { board[x][y] = board[i][j] + 1; enqueue(x, y); } if(tempa != tempb) { swap = tempa; tempa = tempb; tempb = swap; } else break; } if(flag) { ans[a-1][b-1] = board[n-1][n-1]; ans[b-1][a-1] = ans[a-1][b-1]; break; } else if(!dequeue(&i, &j)) { ans[a-1][b-1] = -1; ans[b-1][a-1] = ans[a-1][b-1]; break; } } } for(i = 0; i < n - 1; i++) { for(j = 0; j < n - 1; j++) printf("%d ", ans[i][j]); printf("\n"); } return 0; } void enqueue(int x, int y) { struct queue *ptr = malloc(sizeof(struct queue)); ptr -> x = x; ptr -> y = y; ptr -> next = NULL; if(!endp) startp = ptr; else endp -> next = ptr; endp = ptr; return ; } int dequeue(int *i, int *j) { struct queue *ptr = startp; if(ptr) { *i = ptr -> x; *j = ptr -> y; startp = startp->next; if(!startp) endp = startp; free(ptr); return 1; } return 0; }