// 棋盘覆盖
#include<iostream.h>
#include <iomanip.h>
int Board[8][8]={0};//定义棋盘并初始化棋盘
void ChessBoard(int tr,int tc,int dr,int dc,int size,int &tile);
void main()
{
int tile=0;
cout<<"请输入特殊盘的下标号:"<<endl;
int x,y;
cin>>x>>y;
ChessBoard(0,0,x,y,8,tile);
cout<<endl<<endl;
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
cout<<setw(2)<<Board[i][j]<<" ";
}
cout<<endl;
}
}
void ChessBoard(int tr,int tc,int dr,int dc,int size,int &tile)
{//采用分治策略的棋盘算法 c是列,r是行
if(size==1)
return;
++tile;//L型骨牌号
int s=size/2;//分割棋盘
if(dr<tr+s&&dc<tc+s)
{//特殊方格在左上角棋盘中
Board[tr+s-1][tc+s]=tile;//覆盖右上角子棋盘的左下角
Board[tr+s][tc+s-1]=tile;//覆盖左下角子棋盘的右上角
Board[tr+s][tc+s]=tile;//覆盖右下角子棋盘 的左上角
ChessBoard(tr,tc,dr,dc,s,tile);//覆盖左上角子棋盘
ChessBoard(tr,tc+s,tr+s-1,tc+s,s,tile);//覆盖右上角子棋盘
ChessBoard(tr+s,tc,tr+s,tc+s-1,s,tile);//覆盖左下角子棋盘
ChessBoard(tr+s,tc+s,tr+s,tc+s,s,tile);//覆盖右下角子棋盘
}
else if(dr<tr+s&&dc>=tc+s)
{//特殊方格在右上角棋盘中
Board[tr+s-1][tc+s-1]=tile;
Board[tr+s][tc+s-1]=tile;
Board[tr+s][tc+s]=tile;
ChessBoard(tr,tc,tr+s-1,tc+s-1,s,tile);//覆盖左上角子棋盘
ChessBoard(tr,tc+s,dr,dc,s,tile);//覆盖右上角子棋盘
ChessBoard(tr+s,tc,tr+s,tc+s-1,s,tile);//覆盖左下角子棋盘
ChessBoard(tr+s,tc+s,tr+s,tc+s,s,tile);//覆盖右下角子棋盘
}
else if(dr>=tr+s&&dc<tc+s)
{//特殊方格在左下角棋盘中
Board[tr+s-1][tc+s-1]=tile;
Board[tr+s-1][tc+s]=tile;
Board[tr+s][tc+s]=tile;
ChessBoard(tr,tc,tr+s-1,tc+s-1,s,tile);//覆盖左上角子棋盘
ChessBoard(tr,tc+s,tr+s-1,tc+s,s,tile);//覆盖右上角子棋盘
ChessBoard(tr+s,tc,dr,dc,s,tile);//覆盖左下角子棋盘
ChessBoard(tr+s,tc+s,tr+s,tc+s,s,tile);//覆盖右下角子棋盘
}
else if(dr>=tr+s&&dc>=tc+s)
{//特殊方格在右下角棋盘中
Board[tr+s-1][tc+s-1]=tile;
Board[tr+s-1][tc+s]=tile;
Board[tr+s][tc+s-1]=tile;
ChessBoard(tr,tc,tr+s-1,tc+s-1,s,tile);//覆盖左上角子棋盘
ChessBoard(tr,tc+s,tr+s-1,tc+s,s,tile);//覆盖右上角子棋盘
ChessBoard(tr+s,tc,tr+s,tc+s-1,s,tile);//覆盖左下角子棋盘
ChessBoard(tr+s,tc+s,dr,dc,s,tile);//覆盖右下角子棋盘
}
else
cout<<"你妹,错了!"<<endl;
}