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

当k>0时,将2k×2k棋盘分割为4个2k-1×2k-1 子棋盘(a)所示。
特殊方格必位于4个较小子棋盘之一中,其余3个子棋盘中无特殊方格。为了将这3个无特殊方格的子棋盘转化为特殊棋盘,可以用一个L型骨牌覆盖这3个较小棋盘的会合处,如 (b)所示,从而将原问题转化为4个较小规模的棋盘覆盖问题。递归地使用这种分割,直至棋盘简化为棋盘1×1。
利用分治与递归的思想来做,将棋盘一直分割,直到棋盘的大小为1为止,每个小棋盘有两种情况,即特殊方格在此小棋盘中,和特殊方格不在此棋盘中,若特殊方格不在此棋盘中,那么就将L形棋盘放入,再标记黄色方格为特殊方格,一直递归下去。
代码如下:
#include<iostream>
using namespace std;
int map[100][100]={0};
int tile=1;
void chessBoard(int tr,int tc,int dr,int dc,int size)
{
if(size==1)return;
int t=tile++;
int s=size/2;
///左上角棋盘
if(dr<tr+s&&dc<tc+s)///特殊方格在左上角棋盘中
{
chessBoard(tr,tc,dr,dc,s);
}
else ///不在此棋盘中
{
map[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 ///特殊方格不在右上角棋盘中
{
map[tr+s-1][tc+s]=t;
///将此方格做为特殊方格处理
chessBoard(tr,tc+s,tr+s-1,tc+s,s);
}
///左下角棋盘
if(dr>=tr+s&&dc<tc+s)///特殊方格在左下角棋盘中
{
chessBoard(tr+s,tc,dr,dc,s);
}
else ///特殊方格不在左下角棋盘中
{
map[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 ///特殊方格不在右下角棋盘中
{
map[tr+s][tc+s]=t;
///将此方格做为特殊方格处理
chessBoard(tr+s,tc+s,tr+s,tc+s,s);
}
}
int main()
{
int c_x,c_y,i,j;
int size;
cout<<"please enter the size:"<<endl;
cin>>size;
cout<<"please enter the direction of exceptional chess:"<<endl;
cin>>c_x>>c_y;
chessBoard(0,0,c_x,c_y,size);
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
cout<<map[i][j]<<'\t';
}
cout<<endl;
}
return 0;
}