用c语言编写该题 在一个2k×2k 个方格组成的棋盘中,恰有一个方格与其它方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。
时间: 2024-04-25 12:16:28 浏览: 136
#include<stdio.h>
#include<stdlib.h>
#define N 2050
int special_x, special_y; // 特殊方格坐标
int board[N][N]; // 棋盘
void cover(int x, int y, int size, int special_x, int special_y);
void draw_L(int x, int y, int special_x, int special_y, int type);
int main(){
int k; // 棋盘大小
scanf("%d%d%d", &k, &special_x, &special_y);
board[special_x][special_y] = -1;
cover(0, 0, k, special_x, special_y);
for(int i = 0; i < k; i++){
for(int j = 0; j < k; j++){
if(board[i][j] == -1) printf("S ");
else printf("%d ", board[i][j]);
}
printf("\n");
}
return 0;
}
void cover(int x, int y, int size, int special_x, int special_y){
if(size == 1) return; // 如果当前棋盘大小为1,则直接返回
int half = size / 2; // 将当前棋盘分成四个大小相等的子棋盘
int cnt = 0; // 记录L型骨牌的编号
// 分别处理四个子棋盘
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
cnt++;
int sub_special_x = x + half - 1 + i; // 子棋盘中的特殊方格坐标
int sub_special_y = y + half - 1 + j;
if(sub_special_x == special_x && sub_special_y == special_y){ // 如果子棋盘中存在特殊方格
// 分别用L型骨牌覆盖子棋盘中除特殊方格以外的所有方格
draw_L(x + half - 1, y + half - 1, special_x, special_y, cnt);
cover(x, y, half, sub_special_x, sub_special_y);
cover(x, y + half, half, sub_special_x, sub_special_y);
cover(x + half, y, half, sub_special_x, sub_special_y);
cover(x + half, y + half, half, sub_special_x, sub_special_y);
}
else{ // 如果子棋盘中不存在特殊方格
// 分别用L型骨牌覆盖子棋盘中的所有方格
draw_L(x + half - 1, y + half - 1, sub_special_x, sub_special_y, cnt);
cover(x, y, half, sub_special_x - 1, sub_special_y - 1);
cover(x, y + half, half, sub_special_x - 1, sub_special_y);
cover(x + half, y, half, sub_special_x, sub_special_y - 1);
cover(x + half, y + half, half, sub_special_x, sub_special_y);
}
}
}
}
void draw_L(int x, int y, int special_x, int special_y, int type){
int dx[4][3] = {{0, 1, 1}, {0, 0, 1}, {0, 0, -1}, {0, -1, -1}}; // L型骨牌各个方向的偏移量
int dy[4][3] = {{0, 0, 1}, {0, 1, 1}, {0, -1, -1}, {0, 0, -1}};
for(int i = 0; i < 4; i++){ // 枚举四个方向的L型骨牌
int flag = 1; // 标记是否可以放置该方向的L型骨牌
for(int j = 0; j < 3; j++){
int nx = x + dx[i][j];
int ny = y + dy[i][j];
if(nx < 0 || nx >= N || ny < 0 || ny >= N || (nx == special_x && ny == special_y) || board[nx][ny] != 0){ // 如果超出边界、与特殊方格重合或已有骨牌
flag = 0;
break;
}
}
if(flag){ // 如果可以放置该方向的L型骨牌
for(int j = 0; j < 3; j++){
int nx = x + dx[i][j];
int ny = y + dy[i][j];
board[nx][ny] = type; // 将该方向的L型骨牌放置在棋盘上
}
break;
}
}
}
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)