1 #include<iostream.h> 2 int tile=1; 3 int board[100][100]; 4 void chessBoard(int tr, int tc, int dr, int dc, int size) 5 { 6 if(size==1) 7 return; 8 int t=tile++;//L型骨牌號 9 int s=size/2;//分割棋盤 10 if(dr<tr+s && dc<tc+s)//若是棋盤在左上角中 11 chessBoard(tr, tc, dr, dc, s);//分割左上角棋盤 12 else 13 { 14 board[tr+s-1][tc+s-1]=t;//用t號覆蓋右下角 15 chessBoard(tr, tc, tr+s-1, tc+s-1, s);//覆蓋其餘方格 16 } 17 if(dr<tr+s && dc>=tc+s)//若是棋盤在右上角中 18 chessBoard(tr, tc+s, dr, dc, s); //分割右上角棋盤 19 else 20 { 21 board[tr+s-1][tc+s]=t;//用t號覆蓋左上角 22 chessBoard(tr, tc+s, tr+s-1, tc+s, s);//覆蓋其餘方格 23 } 24 if(dr>=tr+s && dc<tc+s)//若是棋盤在左下角中 25 chessBoard(tr+s, tc, dr, dc, s);//分割左下角棋盤 26 else 27 { 28 board[tr+s][tc+s-1]=t;//用t號覆蓋右上角 29 chessBoard(tr+s, tc, tr+s, tc+s-1, s);//覆蓋其餘方格 30 } 31 if(dr>=tr+s && dc>=tc+s)//若是棋盤在右下角中 32 chessBoard(tr+s, tc+s, dr, dc, s);//分割右下角棋盤 33 else 34 { 35 board[tr+s][tc+s]=t;//用t號覆蓋左上角 36 chessBoard(tr+s, tc+s, tr+s, tc+s, s);//覆蓋其餘方格 37 } 38 } 39 40 void main() 41 { 42 int size; 43 cout<<"輸入棋盤的size(大小必須是2的n次冪): "; 44 cin>>size; 45 int index_x,index_y; 46 cout<<"輸入特殊方格位置的座標: "; 47 cin>>index_x>>index_y; 48 chessBoard(0,0,index_x,index_y,size); 49 for(int i=0;i<size;i++) 50 { 51 for(int j=0;j<size;j++) 52 cout<<board[i][j]<<"\t"; 53 cout<<endl; 54 } 55 }