标签:
/* Author: Tanky Woo Blog: www.WuTianQi.com 棋盘覆盖问题 分治法 2010-12-3 */ #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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/h1021456873/article/details/47978455