boolis_valid_move(int x, int y, int board[SIZE][SIZE]) { if (x >= 0 && x < SIZE && y >= 0 && y < SIZE && board[x][y] == -1) { returntrue; } returnfalse; }
voidprint_board(int board[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { printf("%2d ", board[i][j]); } printf("\n"); } }
voidsolve_knight_tour(int start_x, int start_y) { int board[SIZE][SIZE]; int move_count = 1;
// 初始化棋盘 for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { board[i][j] = -1; } }
int x = start_x; int y = start_y; board[x][y] = move_count;
while (move_count < SIZE * SIZE) { int min_deg = SIZE + 1; int min_index = -1; int next_x, next_y;
// 尝试所有可能的移动 for (int i = 0; i < 8; i++) { next_x = x + move_x[i]; next_y = y + move_y[i];
if (is_valid_move(next_x, next_y, board)) { int deg = 0;
// 计算下一个位置的度数 for (int j = 0; j < 8; j++) { int new_x = next_x + move_x[j]; int new_y = next_y + move_y[j];
if (is_valid_move(new_x, new_y, board)) { deg++; } }
count = 1 while count <= len(lines)**2: for i inrange(len(lines)): for j inrange(len(lines)): if lines[i][j] == count: order_x.append(i) order_y.append(j) count += 1
# 绘制棋盘 plt.figure(figsize=(8, 8))
# 绘制棋盘的格子 for i inrange(len(lines)+1): plt.plot([i, i], [0, len(lines)], color='black') plt.plot([0, len(lines)], [i, i], color='black')