棋盘覆盖(递归实现)

棋盘覆盖(递归实现)

#include<stdio.h>
#define MAXSIZE 1<<10

int ChessBoard[MAXSIZE][MAXSIZE];

void BoardCover(int tr, int tc, int dr, int dc, int size)
{
    static int Tag = 1;
    if (size == 1)return;
    int s = size >> 1;
    
    int t = Tag++;
    /*处理左上角*/
    if (dr < tr + s && dc < tc + s)BoardCover(tr, tc, dr, dc, s);
    else {
        ChessBoard[tr + s - 1][tc + s - 1] = t;
        BoardCover(tr, tc, tr + s - 1, tc + s - 1, s);
    }
    /*处理左下角*/
    if (dr >= tr + s && dc < tc + s)BoardCover(tr + s, tc, dr, dc, s);
    else {
        ChessBoard[tr + s][tc + s - 1] = t;
        BoardCover(tr + s, tc, tr + s, tc + s - 1, s);
    }
    /*处理右上角*/
    if (dr < tr + s && dc >= tc + s)BoardCover(tr, tc + s, dr, dc, s);
    else {
        ChessBoard[tr + s - 1][tc + s] = t;
        BoardCover(tr, tc + s, tr + s - 1, tc + s, s);
    }

    /*处理右下角*/
    if (dr >= tr + s && dc >= tc + s)BoardCover(tr + s, tc + s, dr, dc, s);
    else {
        ChessBoard[tr + s][tc + s] = t;
        BoardCover(tr + s, tc + s, tr + s, tc + s, s);
    }
}

void PrintBoard(int size)
{
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            printf("%4d", ChessBoard[i][j]);
}

int main()
{
    int k=4;
    int size = 1 << 4;
    int dr = 1, dc = 2;//特殊方块的行与列
    
    BoardCover(0, 0, dr, dc, size);
    PrintBoard(size);
}
上一篇:jQuery 学习系列笔记(三)(续)


下一篇:php 时间段查询排序分组