#include <stdio.h>
int tile = 1; // Domino number
int board[128][128]; // 2D array representing the chessboard
// (tr, tc) represents the top-left coordinates of the chessboard, (dr, dc) represents the position of the special square, size = 2^k determines the chessboard size
void chessBoard(int tr, int tc, int dr, int dc, int size)
{
// Base case for recursion
if (size == 1)
return;
int s = size / 2; // Divide the chessboard
int t = tile++; // t records the domino number for this layer
// Check if the special square is in the top-left chessboard
if (dr < tr + s && dc < tc + s)
{
chessBoard(tr, tc, dr, dc, s); // Recursive method for the top-left chessboard with the special square
}
else
{
board[tr + s - 1][tc + s - 1] = t; // Use the domino number t to cover the bottom-right corner
chessBoard(tr, tc, tr + s - 1, tc + s - 1, s); // Recursively cover the remaining squares
}
// Check if the special square is in the top-right chessboard
if (dr < tr + s && dc >= tc + s)
{
chessBoard(tr, tc + s, dr, dc, s);
}
else
{
board[tr + s - 1][tc + s] = t;
chessBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
// Check if the special square is in the bottom-left chessboard
if (dr >= tr + s && dc < tc + s)
{
chessBoard(tr + s, tc, dr, dc, s);
}
else
{
board[tr + s][tc + s - 1] = t;
chessBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
// Check if the special square is in the bottom-right chessboard
if (dr >= tr + s && dc >= tc + s)
{
chessBoard(tr + s, tc + s, dr, dc, s);
}
else
{
board[tr + s][tc + s] = t;
chessBoard(tr + s, tc + s, tr + s, tc + s, s);
}
}
int main()
{
int boardSize = 8; // Chessboard side length
chessBoard(0, 0, 3, 3, boardSize); // (0, 0) is the vertex, chessboard size is boardSize; special square is at (3, 3)
// Print the chessboard
int i, j;
printf("\n\n\n");
for (i = 0; i < boardSize; i++)
{
for (j = 0; j < boardSize; j++)
{
printf("%d\t", board[i][j]);
}
printf("\n\n\n");
}
return 0;
}