简单acm题:棋盘覆盖

Description

 

在一个2k x 2k 个方格组成的棋盘中,恰有一个方格与其他方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。 

 

Input

 

k,dr,dc。k定义如前,dr,dc分别表示特殊方格所在的行号和列号 1= < k < =6

 

Output

 

按照左上,右上,左下,右下的顺序用分治法求解。特殊方格标0,其他位置按上述顺序依次标记。

 

Sample Input

2 1 1

Sample Output

2 2 3 3 
2 0 1 3 
4 1 1 5 
4 4 5 5 

 

//棋盘覆盖只需要不断的把棋盘分小然后用上述的L骨牌来把他变为特殊方格
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <math.h>

using namespace std;
#define ll long long
const int N = 1e6+5;
int a[100][100];
int cnt=1;

void f(int tr,int tc,int dr,int dc,int flag)//递归程序
{
    if(flag == 1)
        return ;
    int t = cnt++; //L型骨牌号
    int s = flag/2;  //分割棋盘
    //覆盖左上
    if(dr <tr+s&& dc<tc+s)//特殊方格在此棋盘
        f(tr,tc,dr,dc,s);
    else//无特殊方格
    {
        //用t号L方格来覆盖右下角
        a[tr+s-1][tc+s-1]=t;
        //覆盖其余方格
        f(tr,tc,tr+s-1,tc+s-1,s);
    }
    //覆盖右上角
    if(dr <tr+s&& dc>=tc+s)
        f(tr,tc+s,dr,dc,s);
    else
    {
        //用t号L方格来覆盖左下角
        a[tr+s-1][tc+s]=t;
        f(tr,tc+s,tr+s-1,tc+s,s);
    }
    //左下角
    if(dr >=tr+s&& dc<tc+s)
        f(tr+s,tc,dr,dc,s);
    else
    {
        //用t号L方格来覆盖右上角
        a[tr+s][tc+s-1]=t;
        f(tr+s,tc,tr+s,tc+s-1,s);
    }
    //右下角
    if(dr >=tr+s&& dc>=tc+s)
        f(tr+s,tc+s,dr,dc,s);
    else
    {    //用t号L方格来覆盖左上角
        a[tr+s][tc+s]=t;
        f(tr+s,tc+s,tr+s,tc+s,s);
    }


}


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(a,0,sizeof(a));
    int k,dr,dc;
    cin>>k>>dr>>dc;
    int tmp=pow(2,k);
    f(0,0,dr,dc,tmp);
    for(int i = 0 ;i<tmp;i++){
        for(int j = 0;j<tmp;j++)
    {
            cout<<a[i][j]<<" ";
    }
        cout<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值