棋盘覆盖问题

Posted baoyihan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了棋盘覆盖问题相关的知识,希望对你有一定的参考价值。

题面:link

#include <cstdio>
#include <iostream>
using namespace std;

int a[1024][1024];

int cnt = 0;
int n, kr, kc;

void solve(int k, int r, int c, int x, int y) 
    if (k == 0)
        return;
    int num = ++cnt;
    int midx = x + (1<<k-1), midy = y + (1<<k-1);
    int flag;
    if (r < midx && c < midy) flag = 1; //左上角
    if (r < midx && c >= midy) flag = 2; //右上角
    if (r >= midx && c < midy) flag = 3; //左下角
    if (r >= midx && c >= midy) flag = 4; //右下角
    if (flag == 1)
        solve(k-1, r, c, x, y);
    else 
        a[midx-1][midy-1] = num;
        solve(k-1, midx-1, midy-1, x, y);
    
    if (flag == 2)
        solve(k-1, r, c, x, midy);
    else 
        a[midx-1][midy] = num;
        solve(k-1, midx-1, midy, x, midy);
    
    if (flag == 3)
        solve(k-1, r, c, midx, y);
    else 
        a[midx][midy-1] = num;
        solve(k-1, midx, midy-1, midx, y);
    
    if (flag == 4)
        solve(k-1, r, c, midx, midy);
    else 
        a[midx][midy] = num;
        solve(k-1, midx, midy, midx, midy);
    


int main() 
    scanf("%d%d%d", &n, &kr, &kc);
    a[kr-1][kc-1] = -1; 
    solve(n, kr-1, kc-1, 0, 0);
    for (int i = 0; i < (1<<n); i++) 
        for (int j = 0; j < (1<<n)-1; j++) 
            printf("%d ", a[i][j]);
        printf("%d\n", a[i][(1<<n)-1]);
    

以上是关于棋盘覆盖问题的主要内容,如果未能解决你的问题,请参考以下文章

分治算法----棋盘覆盖问题

残缺棋盘的覆盖问题

残缺棋盘的覆盖问题

棋盘覆盖问题——分治法——代码清晰易懂

C++经典算法问题:棋盘覆盖问题(分治算法)!含源码示例

js算法:分治法-棋盘覆盖