2017-11-01 13:28

# 棋盘覆盖问题，用4种不同形态的L型骨牌覆盖在一个给定的特殊棋盘上

#include

int tile = 0;
int k = 1;
int Board[100][100];
void ChessBoard(int tr,int tc,int dr,int dc,int size){
if(size == 1){
return;
}

int t =tile++;
t =t%4;
int 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{
//用t形L型骨牌覆盖左下角
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{
//用t形L型骨牌覆右上角
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{
//用t形L型骨牌覆盖左上角
Board[tr+s][tc+s] = t;
ChessBoard(tr+s,tc+s,tr+s,tc+s,s);
}

}
int main(){
int i = 0;
int j = 0;
int dr = 0;
int dc = 0;
printf("请输入边长：");
scanf("%d",&k);
printf("请输入断点的横坐标和纵坐标");
scanf("%d %d",&dr,&dc);
Board[dr-1][dc-1] = -1;
ChessBoard(0,0,dr-1,dc-1,k);

for(i=0;i<k;i++){
for(j=0;j<k;j++){
printf("%4d",Board[i][j]);
}
printf("\n");
}

}

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答