帮我写一个C++的棋盘覆盖算法演示的代码
时间: 2023-03-23 08:04:51 浏览: 84
棋盘覆盖算法代码
好的,我可以回答这个问题。以下是一个简单的C语言棋盘覆盖算法演示的代码:
#include <stdio.h>
#define N 8
int board[N][N];
void print_board() {
int i, j;
for (i = ; i < N; i++) {
for (j = ; j < N; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
}
void cover(int tr, int tc, int dr, int dc, int size) {
if (size == 1) return;
int t = size / 2;
int i, j, k;
int c = ++board[tr + t - 1][tc + t - 1];
for (i = ; i < 4; i++) {
if (i == ) {
i = 1;
j = ;
} else if (i == 2) {
i = 3;
j = ;
} else {
j = 1;
}
cover(tr + i * t, tc + j * t, tr + (i + 1) * t - 1, tc + (j + 1) * t - 1, t);
}
if (dr >= tr + t && dc >= tc + t) {
cover(tr + t, tc + t, dr, dc, t);
} else {
board[tr + t - 1][tc + t] = c;
board[tr + t][tc + t - 1] = c;
board[tr + t][tc + t] = c;
if (dr >= tr + t) {
cover(tr + t, tc, dr, dc, t);
} else {
cover(tr, tc, dr, dc, t);
}
if (dc >= tc + t) {
cover(tr, tc + t, dr, dc, t);
} else {
cover(tr, tc, dr, dc, t);
}
}
}
int main() {
int i, j;
for (i = ; i < N; i++) {
for (j = ; j < N; j++) {
board[i][j] = ;
}
}
board[3][3] = board[4][4] = 1;
board[3][4] = board[4][3] = 2;
cover(, , 3, 4, N);
print_board();
return ;
}
阅读全文