码迷,mamicode.com
首页 > 其他好文 > 详细

棋盘覆盖问题

时间:2019-06-22 13:29:19      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:org   using   for   class   data   std   can   覆盖   void   

题面: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]);
    }
}

棋盘覆盖问题

标签:org   using   for   class   data   std   can   覆盖   void   

原文地址:https://www.cnblogs.com/baoyihan/p/11068519.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!