Demo entry 6660712

algorithm.h

   

Submitted by anonymous on Nov 16, 2017 at 13:58
Language: C++. Code size: 1.5 kB.

#ifndef ALGORITHM_H
#define ALGORITHM_H
//algorithm.h
//实验1、棋盘覆盖问题

int board[33][33];//board[][]存储点的标记信息:

static int tile = 1;//L型骨牌号

//(tr,tc)是棋盘左上角的方格坐标,(dr,dc)是特殊方格所在的坐标,size是棋盘的行数和列数
void ChessBoard(int tr, int tc, int dr, int dc, int size)
{
    if (size == 1) return;//递归边界

    int t = tile++;//L型骨牌号
    int s = size / 2;//分割棋盘

    //覆盖左上角子棋盘
    if (dr<tr + s && dc<tc + s)//特殊方格在此棋盘中
        ChessBoard(tr, tc, dr, dc, s);
    else //此棋盘中无特殊方格,用t号L型骨牌覆盖右下角
    {
        board[tr + s - 1][tc + s - 1] = t;
        //覆盖其余方格
        ChessBoard(tr, tc, tr + s - 1, tc + s - 1, s);
    }

    //覆盖右上角子棋盘
    if (dr<tr + s && dc >= tc + s)
        ChessBoard(tr, tc + s, dr, dc, s);//特殊方格在此棋盘中
    else //此棋盘中无特殊方格,用t号L型骨牌覆盖左下角
    {
        board[tr + s - 1][tc + s] = t;
        //覆盖其余方格
        ChessBoard(tr, tc + s, tr + s - 1, tc + s, s);
    }


    //覆盖左下角子棋盘
    if (dr >= tr + s && dc<tc + s)//特殊方格在此棋盘中
        ChessBoard(tr + s, tc, dr, dc, s);
    else //此棋盘中无特殊方格,用t号L型骨牌覆盖右上角
    {
        board[tr + s][tc + s - 1] = t;
        //覆盖其余方格
        ChessBoard(tr + s, tc, tr + s, tc + s - 1, s);
    }
    //覆盖右下角子棋盘
    if (dr >= tr + s && dc >= tc + s)//特殊方格在此棋盘中
        ChessBoard(tr + s, tc + s, dr, dc, s);
    else //此棋盘中无特殊方格,用t号L型骨牌覆盖左上角
    {
        board[tr + s][tc + s] = t;
        //覆盖其余方格
        ChessBoard(tr + s, tc + s, tr + s, tc + s, s);
    }
}

#endif // ALGORITHM_H

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).