欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

棋盘覆盖问题

程序员文章站 2022-05-13 18:54:46
...

输入:棋盘大小、特殊方格的行号和列号
输出:基本要求:以相同的数字表示一个L型骨牌,输出棋盘中每个方格对应的数字

#include <stdio.h>
int Board[1000][1000],tile;
void ChessBoard(int tr,int tc,int dr,int dc,int size)
{
    if(size == 1)
        return;
    int t = tile++;
    int s = size / 2;
    if(dr < tr + s && dc < tc + s)
        ChessBoard(tr,tc,dr,dc,s);
    else
    {
        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
    {
        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
    {
        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
    {
        Board[tr+s][tc+s] = t;
        ChessBoard(tr+s,tc+s,tr+s,tc+s,s);
    }
 
}
int main()
{
    int k,i,j,x,y;
    printf("输入方格阶层 特殊方格横坐标 纵坐标\n");
    while(scanf("%d%d%d",&k,&x,&y))
    {
        tile = 0;
        Board[x][y] = 0;
        ChessBoard(0,0,x,y,k);
        for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
               printf("%d ",Board[i][j]);
            printf("\n");
        }
 
    }
    return 0;
}
相关标签: 棋盘覆盖