棋盘覆盖问题
1 #include <iostream> 2 using namespace std; 3 const int N = 11; 4 int Board[N][N]; 5 int tile = 0; 6 7 /* 8 tr:棋盘左上角方格的行号 9 tc:棋盘左上角方格的列号 10 dr:特殊方格所在的行号 11 dc:特殊方格所在的列号 12 size:方形棋盘的边长 13 */ 14 void ChessBoard(int tr, int tc, int dr, int dc, int size) 15 { 16 if(size == 1) 17 return; 18 int t = ++tile, s = size/2; 19 20 //覆盖左上角子棋盘 21 if(dr<tr+s && dc<tc+s) 22 //特殊方格在此棋盘中 23 ChessBoard(tr, tc, dr, dc, s); 24 else // 此棋盘无特殊方格 25 { 26 // 用t号L型骨型牌覆盖右下角 27 Board[tr+s-1][tc+s-1] = t; 28 // 覆盖其余方格 29 ChessBoard(tr, tc, tr+s-1, tc+s-1, s); 30 } 31 32 //覆盖右上角子棋盘 33 if(dr<tr+s && dc>=tc+s) 34 ChessBoard(tr, tc+s, dr, dc, s); 35 else 36 { 37 Board[tr+s-1][tc+s] = t; 38 ChessBoard(tr, tc+s, tr+s-1, tc+s, s); 39 } 40 41 //覆盖左下角子棋盘 42 if(dr>=tr+s && dc<tc+s) 43 ChessBoard(tr+s, tc, dr, dc, s); 44 else 45 { 46 Board[tr+s][tc+s-1] = t; 47 ChessBoard(tr+s, tc, tr+s, tc+s-1, s); 48 } 49 50 //覆盖右下角子棋盘 51 if(dr>=tr+s && dc>=tc+s) 52 ChessBoard(tr+s, tc+s, dr, dc, s); 53 else 54 { 55 Board[tr+s][tc+s] = t; 56 ChessBoard(tr+s, tc+s, tr+s, tc+s, s); 57 } 58 } 59 60 void DisplayBoard(int size) 61 { 62 for(int i=1; i<=size; ++i) 63 { 64 for(int j=1; j<=size; ++j) 65 printf("%2d ", Board[i][j]); 66 printf("\n"); 67 } 68 } 69 70 int main() 71 { 72 ChessBoard(1, 1, 1, 2, 4); 73 DisplayBoard(4); 74 return 0; 75 }