triomino拼图c语言
时间: 2023-06-23 18:56:56 浏览: 103
好的,以下是Triomino拼图的C语言代码:
```c
#include <stdio.h>
#include <stdbool.h>
#define SIZE 3
void TrominoTile(int x_board, int y_board, int x_missing, int y_missing, int size_board, int board[SIZE][SIZE]) {
int x_center, y_center, count;
if (size_board == 1) {
return;
}
count = 0;
x_center = x_board + size_board / 2;
y_center = y_board + size_board / 2;
if (x_missing < x_center && y_missing < y_center) {
count++;
board[x_center - 1][y_center] = count;
board[x_center][y_center - 1] = count;
board[x_center][y_center] = count;
TrominoTile(x_board, y_board, x_missing, y_missing, size_board / 2, board);
TrominoTile(x_board, y_center, x_center - 1, y_center, size_board / 2, board);
TrominoTile(x_center, y_board, x_center, y_center - 1, size_board / 2, board);
TrominoTile(x_center, y_center, x_center, y_center, size_board / 2, board);
} else if (x_missing < x_center && y_missing >= y_center) {
count++;
board[x_center - 1][y_center - 1] = count;
board[x_center][y_center - 1] = count;
board[x_center][y_center] = count;
TrominoTile(x_board, y_board, x_center - 1, y_center - 1, size_board / 2, board);
TrominoTile(x_board, y_center, x_missing, y_missing, size_board / 2, board);
TrominoTile(x_center, y_board, x_center, y_center - 1, size_board / 2, board);
TrominoTile(x_center, y_center, x_center, y_center, size_board / 2, board);
} else if (x_missing >= x_center && y_missing < y_center) {
count++;
board[x_center - 1][y_center - 1] = count;
board[x_center - 1][y_center] = count;
board[x_center][y_center] = count;
TrominoTile(x_board, y_board, x_center - 1, y_center - 1, size_board / 2, board);
TrominoTile(x_board, y_center, x_center - 1, y_center, size_board / 2, board);
TrominoTile(x_center, y_board, x_missing, y_missing, size_board / 2, board);
TrominoTile(x_center, y_center, x_center, y_center, size_board / 2, board);
} else { // (x_missing >= x_center && y_missing >= y_center)
count++;
board[x_center - 1][y_center - 1] = count;
board[x_center][y_center - 1] = count;
board[x_center - 1][y_center] = count;
TrominoTile(x_board, y_board, x_center - 1, y_center - 1, size_board / 2, board);
TrominoTile(x_board, y_center, x_center - 1, y_center, size_board / 2, board);
TrominoTile(x_center, y_board, x_center, y_center - 1, size_board / 2, board);
TrominoTile(x_center, y_center, x_missing, y_missing, size_board / 2, board);
}
}
void PrintBoard(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");
}
}
int main() {
int board[SIZE][SIZE] = {{0}};
int x_missing, y_missing;
printf("请输入缺失方块的坐标(x, y):");
scanf("%d%d", &x_missing, &y_missing);
board[x_missing][y_missing] = -1; // 标记缺失方块
TrominoTile(0, 0, x_missing, y_missing, SIZE, board);
printf("Triomino拼图如下:\n");
PrintBoard(board);
return 0;
}
```
在代码中,先定义了一个`TrominoTile`函数,用于填充拼图。其中,`x_board`和`y_board`表示当前矩阵的左上角坐标,`size_board`表示当前矩阵的大小,`x_missing`和`y_missing`表示缺失方块的坐标。当`size_board`等于1时,表示只有一个方块,直接返回;否则,根据缺失方块所在的象限,将矩阵分为四个子矩阵,递归调用`TrominoTile`函数进行填充。
在`main`函数中,先将缺失方块的位置标记为-1,然后调用`TrominoTile`函数进行填充。最后,调用`PrintBoard`函数打印填充后的矩阵。
希望能够帮到你!
阅读全文