关闭

棋盘覆盖

415人阅读 评论(2) 收藏 举报
分类:
/*
    0 0 0 1 2
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
int total = 0;
void chessBoard(int, int, int, int, int);
int board[MAXSIZE][MAXSIZE];
int power(int);
int main(void)
{
    int i, j;
    int tr, tc, dr, dc, k;
    int size;
    scanf("%d %d %d %d %d", &tr, &tc, &dr, &dc, &k);
    size = power(k);
    chessBoard(tr, tc, dr, dc, size);
    board[dr][dc] = -1;
    printf("after chessBoard:\n");
    for (i = 0; i < size; ++i)
    {
        for (j = 0; j < size; ++j)
        {
            printf("%5d ", board[i][j]);
        }
        printf("\n");
    }
    return 0;
}
void chessBoard(int tr, int tc, int dr, int dc, int size)
{
    if (1 == size)
    {
        return ;
    }
    int t = total++;
    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, tr + 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);
    }
}
//计算2^n
int power(int n)
{
    int i;
    int result = 1;
    for (i = 0; i < n; ++i)
    {
        result *= 2;
    }
    return result;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:130611次
    • 积分:3887
    • 等级:
    • 排名:第8173名
    • 原创:293篇
    • 转载:22篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论