递归与分治

棋盘覆盖

2^k的棋盘格子,有一个缺陷,用L形状的的拼图把棋盘覆盖。

每次涂抹的时候先按照缺陷的四周涂抹……23333

参考

int dir[4][2] = {{0,0},{0,1},{1,0},{1,1}};  ///棋盘L形状对应的分别缺少那个格子

int graph[10000][10000];

void set_piece(int r,int c,int x){  ///r,c代表所要涂抹的位置,x表示涂抹的哪种类型,其他的表示不涂抹

    for(int i = 0;i < 4;i++){

        if(i == x)

            continue;

        graph[r+dir[i][0]][c+dir[i][1]] = x+1;

    }

}

void chessBoard(int sx,int sy,int x,int y,int len){

    if(len == 1)

        return;

    int s = len / 2;

    int dx = x >= sx + s; ///判断缺陷位置

    int dy = y >= sy + s;

    for(int i = 0;i < 4;i++){

        if(dir[i][0] != dx || dir[i][1] != dy)

            continue;

        set_piece(sx+s-1,sy+s-1,i);

        for(int j = 0;j < 4;j++){

            int ssx = sx + dir[j][0] * s;

            int ssy = sy + dir[j][1] * s;

            if(i == j){

                chessBoard(ssx,ssy,x,y,s);

            }

            else{

                int ex = sx + s - 1 + dir[j][0];

                int ey = sy + s - 1 + dir[j][1];

                chessBoard(ssx,ssy,ex,ey,s);

            }

        }

    }

}

 

你可能感兴趣的:(递归)