棋盘覆盖问题c语言

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值