在一个2k x 2k ( 即:2^k x 2^k )个方格组成的棋盘中,恰有一个方格与其他方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。
直接看这篇文章就好了,有图可以帮助理解。
#include<iostream>
using namespace std;
int qp[64][64];
int qz=1;
void fun(int th,int tl,int qh,int jh,int qsl,int jzl)
{ // th/tl-特殊位置 qh/jh-起始行/截至行 qsl/jzl-起始列/截至列
int zsh,zsl,ysh,ysl,zxh,zxl,yxh,yxl; //记录每个分治的特殊位置:左上 右上 左下 右下
float x,y; //坐标原点
x=float(qh+jh)/2; y=float(qsl+jzl)/2;
//判断特殊点在第几象限
if(float(th)<x && float(tl)<y){ //第一象限 左上
qp[int(x+0.5)][int(y-0.5)]=qp[int(x+0.5)][int(y+0.5)]=qp[int(x-0.5)][int(y+0.5)]=qz++;
//记录每个分治的特殊位置
zsh=th;zsl=tl;
ysh=int(x-0.5);ysl=int(y+0.5);
zxh=int(x+0.5);zxl=int(y-0.5);
yxh=int(x+0.5);yxl=int(y+0.5);
}
else if(float(th)<x && float(tl)>y){ //第二象限 右上
qp[int(x-0.5)][int(y-0.5)]=qp[int(x+0.5)][int(y+0.5)]=qp[int(x+0.5)][int(y-0.5)]=qz++;
ysh=th;ysl=tl;
zsh=int(x-0.5);zsl=int(y-0.5);
zxh=int(x+0.5);zxl=int(y-0.5);
yxh=int(x+0.5);yxl=int(y+0.5);
}
else if(float(th)>x && float(tl)<y){ //第三象限 左下
qp[int(x-0.5)][int(y+0.5)]=qp[int(x-0.5)][int(y-0.5)]=qp[int(x+0.5)][int(y+0.5)]=qz++;
zxh=th;zxl=tl;
ysh=int(x-0.5);ysl=int(y+0.5);
zsh=int(x-0.5);zsl=int(y-0.5);
yxh=int(x+0.5);yxl=int(y+0.5);
}
else if(float(th)>x && float(tl)>y){ //第四象限 右下
qp[int(x+0.5)][int(y-0.5)]=qp[int(x-0.5)][int(y-0.5)]=qp[int(x-0.5)][int(y+0.5)]=qz++;
yxh=th;yxl=tl;
ysh=int(x-0.5);ysl=int(y+0.5);
zxh=int(x+0.5);zxl=int(y-0.5);
zsh=int(x-0.5);zsl=int(y-0.5);
}
if(qh+1==jh) return; //分治结束
//递归左上分治
fun(zsh,zsl,qh,(qh+jh)/2,qsl,(qsl+jzl)/2);
//递归右上分治
fun(ysh,ysl,qh,(qh+jh)/2,(qsl+jzl)/2+1,jzl);
//递归左下分治
fun(zxh,zxl,(qh+jh)/2+1,jh,qsl,(qsl+jzl)/2);
//递归右下分治
fun(yxh,yxl,(qh+jh)/2+1,jh,(qsl+jzl)/2+1,jzl);
}
int main(){
int N,n,row,col,i,j,k=1;
cin>>N;
while(N--){
qz=1;
cin>>n>>row>>col;
fun(row,col,0,n-1,0,n-1);
cout<<"CASE:"<<k++<<"\n";
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(j!=n-1) cout<<qp[i][j]<<"\t";
else cout<<qp[i][j];
}
cout<<"\n";
}
}
}
上面是我自己写的,确实有点一言难尽呀。
还是要学学大佬的代码呀:
1.
#include<iostream>
using namespace std;
int tile=1; //L型骨牌的编号(递增)
int board[100][100]; //棋盘
/*****************************************************
* 递归方式实现棋盘覆盖算法
* 输入参数:
* tr--当前棋盘左上角的行号
* tc--当前棋盘左上角的列号
* dr--当前特殊方格所在的行号
* dc--当前特殊方格所在的列号
* size:当前棋盘的:2^k
*****************************************************/
void chessBoard ( int tr, int tc, int dr, int dc, int size )
{
if ( size==1 ) //棋盘方格大小为1,说明递归到最里层
return;
int t=tile++; //每次递增1
int s=size/2; //棋盘中间的行、列号(相等的)
//检查特殊方块是否在左上角子棋盘中
if ( dr<tr+s && dc<tc+s ) //在
chessBoard ( tr, tc, dr, dc, s );
else //不在,将该子棋盘右下角的方块视为特殊方块
{
board[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 //不在,将该子棋盘左下角的方块视为特殊方块
{
board[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 //不在,将该子棋盘右上角的方块视为特殊方块
{
board[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 //不在,将该子棋盘左上角的方块视为特殊方块
{
board[tr+s][tc+s]=t;
chessBoard ( tr+s, tc+s, tr+s, tc+s, s );
}
}
void main()
{
int size;
cout<<"输入棋盘的size(大小必须是2的n次幂): ";
cin>>size;
int index_x,index_y;
cout<<"输入特殊方格位置的坐标: ";
cin>>index_x>>index_y;
chessBoard ( 0,0,index_x,index_y,size );
for ( int i=0; i<size; i++ )
{
for ( int j=0; j<size; j++ )
cout<<board[i][j]<<"/t";
cout<<endl;
}
}
#include <iostream>
using namespace std;
const int N = 11;
int Board[N][N];
int tile = 0;
/*
tr:棋盘左上角方格的行号
tc:棋盘左上角方格的列号
dr:特殊方格所在的行号
dc:特殊方格所在的列号
size:方形棋盘的边长
*/
void ChessBoard(int tr, int tc, int dr, int dc, int size)
{
if(size == 1)
return;
int t = ++tile, s = size/2;
//覆盖左上角子棋盘
if(dr<tr+s && dc<tc+s)
//特殊方格在此棋盘中
ChessBoard(tr, tc, dr, dc, s);
else // 此棋盘无特殊方格
{
// 用t号L型骨型牌覆盖右下角
Board[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
{
Board[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
{
Board[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
{
Board[tr+s][tc+s] = t;
ChessBoard(tr+s, tc+s, tr+s, tc+s, s);
}
}
void DisplayBoard(int size)
{
for(int i=1; i<=size; ++i)
{
for(int j=1; j<=size; ++j)
printf("%2d ", Board[i][j]);
printf("\n");
}
}
int main()
{
ChessBoard(1, 1, 1, 2, 4);
DisplayBoard(4);
return 0;
}
以上两段代码来自这里
原文:https://www.cnblogs.com/xxmmqg/p/12766222.html