#include <stdio.h>

#define SIZE 8

int main() {
    int board[SIZE][SIZE] = {0};
    int accessibility[SIZE][SIZE] = {
        {2, 3, 4, 4, 4, 4, 3, 2},
        {3, 4, 6, 6, 6, 6, 4, 3},
        {4, 6, 8, 8, 8, 8, 6, 4},
        {4, 6, 8, 8, 8, 8, 6, 4},
        {4, 6, 8, 8, 8, 8, 6, 4},
        {4, 6, 8, 8, 8, 8, 6, 4},
        {3, 4, 6, 6, 6, 6, 4, 3},
        {2, 3, 4, 4, 4, 4, 3, 2}
    };

    int horizontal[8] = {2, 1, -1, -2, -2, -1, 1, 2};
    int vertical[8] = {-1, -2, -2, -1, 1, 2, 2, 1};

    int currentRow = 0;
    int currentCol = 0;
    board[currentRow][currentCol] = 1;

    for (int moveNumber = 2; moveNumber <= 64; moveNumber++) {
        int minAccess = 9;
        int bestMove = -1;
        int nextRow = -1;
        int nextCol = -1;

        for (int moveType = 0; moveType < 8; moveType++) {
            int testRow = currentRow + vertical[moveType];
            int testCol = currentCol + horizontal[moveType];

            if (testRow >= 0 && testRow < SIZE && testCol >= 0 && testCol < SIZE) {
                if (board[testRow][testCol] == 0) {
                    if (accessibility[testRow][testCol] < minAccess) {
                        minAccess = accessibility[testRow][testCol];
                        bestMove = moveType;
                        nextRow = testRow;
                        nextCol = testCol;
                    }
                }
            }
        }

        if (bestMove != -1) {
            currentRow = nextRow;
            currentCol = nextCol;
            board[currentRow][currentCol] = moveNumber;

            for (int moveType = 0; moveType < 8; moveType++) {
                int testRow = currentRow + vertical[moveType];
                int testCol = currentCol + horizontal[moveType];
                if (testRow >= 0 && testRow < SIZE && testCol >= 0 && testCol < SIZE) {
                    accessibility[testRow][testCol]--;
                }
            }
        } else {
            break;
        }
    }

    for (int r = 0; r < SIZE; r++) {
        for (int c = 0; c < SIZE; c++) {
            printf("%2d ", board[r][c]);
        }
        printf("\n");
    }

    return 0;
}