class Rextester { static int N = 8; static int dx[] = {2, 1,-1,-2,-2,-1, 1, 2}; // ходи коня static int dy[] = {1, 2, 2, 1,-1,-2,-2,-1}; static boolean step (int x, int y, int i, int sol[][]) { int k, next_x, next_y; if (i == N * N) return true; for (k = 0; k < 8; k++) { next_x = x + dx[k]; next_y = y + dy[k]; if (next_x >= 0 && next_x < N && next_y >= 0 && next_y < N && sol[next_x][next_y] == -1) { sol[next_x][next_y] = i; if (step(next_x, next_y, i + 1, sol)) return true; else sol[next_x][next_y] = -1; } } return false; } public static void main(String args[]) { int sol[][] = new int[N][N]; // розв'язок for (int x = 0; x < N; x++) for (int y = 0; y < N; y++) sol[x][y] = -1; sol[0][0] = 0; if (!step(0, 0, 1, sol)) System.out.println("Розв'язків немає"); else for (int x = 0; x < N; x++) { for (int y = 0; y < N; y++) if (sol[x][y]<10) System.out.print(" "+ sol[x][y]); else System.out.print(" " + sol[x][y]); System.out.println(); } } }