【问题描述】
棋盘覆盖问题要求在2^k * 2^k 个方格组成的棋盘中,你给定任意一个特殊点,用一种方案实现对除该特殊点的棋盘实现全覆盖。
#include
using namespace std;
int check[8][8];
int time=0;
void chessboard(int spx,int spy,int firstx,int firsty,int size)
{
if (size==1) return;
int t=time++; //这里time必须用一个临时变量t来记录递归的次数,不然只用time最后结果是乱的
int s=size/2; //将棋盘规模减半,分为四部分
if(spx<firstx+s && spy<firsty+s) //若特殊块在当前区域(左上)
chessboard(spx,spy,firstx,firsty,s);
else //若不在
{
check[firstx+s-1][firsty+s-1]=t; //将左上子棋盘的最右下角一块覆盖
chessboard(firstx+s-1,firsty+s-1,firstx,firsty,s); //此时特殊块变为刚刚覆盖后的坐标
}
if(spx<firstx+s && spy>=firsty+s) //右上
chessboard(spx,spy,firstx,firsty+s,s);
else
{
check[firstx+s-1][firsty+s]=t;
chessboard(firstx+s-1,firsty+s,firstx,firsty+s,s);
}
if(spx>=firstx+s && spy<firsty+s) //左下
chessboard(spx,spy,firstx+s,firsty,s);
else
{
check[firstx+s][firsty+s-1]=t;
chessboard(firstx+s,firsty+s-1,firstx+s,firsty,s);
}
if(spx>=firstx+s && spy>=firsty+s) // 右下
chessboard(spx,spy,firstx+s,firsty+s,s);
else
{
check[firstx+s][firsty+s]=t;
chessboard(firstx+s,firsty+s,firstx+s,firsty+s,s);
}
}
void putout()
{
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
cout<<check[i][j]<<'\t';
}
cout<<endl<<endl;
}
}
int main(int argc, char** argv) {
chessboard(3,3,0,0,8);
putout();
return 0;
}