棋盘覆盖(分治算法)
2017-03-08 18:25
211 查看
#include <iostream> using namespace std; /** *tr:棋盘左上角行号,tc:棋盘左上角列号 *dr:特殊棋子行号, dc:特殊棋子列号 *二维数组board表示棋盘,edgeLength = 2^k,棋盘规格2^k * 2^k *cnt 为全局变量 *zzuwenjie 2017-3-8 18:25:25 **/ const int maxn = (1<<3) + 1;//修改这里-----------------------------!!!!!!!!!! int cnt = 0, board[maxn][maxn] ; void coverBroad(int tr, int tc, int dr, int dc, int edgeLength, int board[][maxn]){ if (edgeLength <= 1){ return; } int tmp = ++cnt; edgeLength >>= 1; if (dr < tr + edgeLength && dc < tc + edgeLength){ coverBroad(tr, tc, dr, dc , edgeLength, board); }else{ board[tr + edgeLength - 1][tc +edgeLength -1] = tmp; coverBroad(tr, tc, tr +edgeLength -1, tc + edgeLength -1, edgeLength, board); }//左上 if (dr < tr + edgeLength && dc >= tc + edgeLength){ coverBroad(tr, tc + edgeLength, dr, dc , edgeLength, board); }else{ board[tr + edgeLength -1][tc +edgeLength] = tmp; coverBroad(tr, tc + edgeLength, tr + edgeLength -1, tc + edgeLength, edgeLength, board); }//右上 if (dr >= tr + edgeLength && dc < tc + edgeLength){ coverBroad(tr + edgeLength, tc, dr, dc , edgeLength, board); }else{ board[tr + edgeLength][tc +edgeLength -1] = tmp; coverBroad(tr + edgeLength, tc, tr +edgeLength, tc + edgeLength -1, edgeLength, board); }//左下 if (dr >= tr + edgeLength && dc >= tc + edgeLength){ coverBroad(tr + edgeLength, tc + edgeLength, dr, dc , edgeLength, board); }else{ board[tr + edgeLength][tc +edgeLength] = tmp; coverBroad(tr +edgeLength, tc + edgeLength, tr +edgeLength, tc + edgeLength, edgeLength, board); }//右下 } int main(){ //freopen("stdout.txt", "w", stdout); int tr = 2, tc = 3;//可修改,tr tc < maxn board[tc] =0; coverBroad(1, 1, tr, tc, maxn - 1, board); //cout << maxn <<endl; for (int i = 1; i < maxn; ++i){ for (int j = 1; j < maxn; ++j){ cout << board[i][j]<<"\t"; } cout << endl << endl; } //cout << cnt <<endl; return 0; } 相关文章推荐
- 分治算法--棋盘覆盖
- 分治算法;骨牌覆盖棋盘问题;
- 分治算法---棋盘覆盖问题java代码实现
- 棋盘覆盖问题(C实现)——分治算法的思想
- 分治算法--棋盘覆盖
- 分治算法--L型骨牌棋盘覆盖
- 分治算法---棋盘覆盖
- 分治算法-棋盘覆盖
- 用Visual C++6.0实现棋盘覆盖分治算法
- 棋盘覆盖递归与分治算法
- 分治算法——棋盘覆盖
- 五大常用算法(一) 分治算法(4) 棋盘覆盖
- Java基于分治算法实现的棋盘覆盖问题示例
- 棋盘覆盖(分治算法)
- 分治算法——棋盘覆盖
- NYOJ 45-棋盘覆盖:大数问题
- 棋盘覆盖->分治-分而治之
- HDU4185Oil Skimming(行列匹配||棋盘匹配||黑白染色||1X2矩形覆盖)
- 南阳oj 棋盘覆盖 题目45
- 棋盘的完美覆盖问题,c++代码实现