//残缺棋盘的问题要求用3个方格的板(三格板)(triominoes)覆盖残缺棋盘。
//在此覆盖中,两个三格板不能重叠,三格板不能覆盖残缺方格,但必须覆盖其他所有的方格。
//在这种限制条件下,所需要的三格板总数为(2^(2k-1))/3。
//可以验证(2^(2k-1))/3是一个整数。k为0的残缺棋盘很容易被覆盖,
//因为它没有非残缺的方格,用于覆盖的三格板的数目为0。当k=1时,
//正好存在3个非残缺的方格,并且这三个方格某一方 向的三格板来覆盖。
//小残缺棋盘
// 1
//1 1
//2
//2 2
//3 3
// 3
//4 4
//4
int amount = 0; int array[16][16] = { 0 }; void Print(int size) { for (int i = 0; i < size; ++i) { for (int j = 0; j < size; ++j) printf("%-3d ", array[i][j]); cout << endl; } cout << endl; } void tileBoard(int tr,int tc,int dr,int dc,int size) { int s, t; if(size <2) return; amount = amount + 1; t = amount; s = size / 2; if (dr < tr + s&&dc < tc + s) { tileBoard(tr, tc, dr, dc, s); array[tr + s - 1][tc + s] = t; array[tr + s][tc + s - 1] = t; array[tr + s][tc + s] = t; tileBoard(tr, tc+s, dr+s-1, dc+s, s); tileBoard(tr+s, tc, dr+s, dc+s-1, s); tileBoard(tr+s, tc+s, dr+s, dc+s, s); } else if (dr < tr + s&&dc >= tc + s) { tileBoard(tr, tc+s, dr, dc, s); array[tr + s - 1][tc + s-1] = t; array[tr + s][tc + s - 1] = t; array[tr + s][tc + s] = t; tileBoard(tr, tc , dr + s - 1, dc + s-1, s); tileBoard(tr + s, tc, dr + s, dc + s - 1, s); tileBoard(tr + s, tc + s, dr + s, dc + s, s); } else if (dr >=tr + s&&dc < tc + s) { tileBoard(tr+s, tc, dr, dc, s); array[tr + s - 1][tc + s-1] = t; array[tr + s -1][tc + s] = t; array[tr + s][tc + s] = t; tileBoard(tr, tc , dr + s - 1, dc + s-1, s); tileBoard(tr, tc+s, dr + s-1, dc + s , s); tileBoard(tr + s, tc + s, dr + s, dc + s, s); } else if (dr >=tr + s&&dc >= tc + s) { tileBoard(tr+s, tc+s, dr, dc, s); array[tr + s - 1][tc + s-1] = t; array[tr + s-1][tc + s] = t; array[tr + s][tc + s-1] = t; tileBoard(tr, tc, dr + s - 1, dc + s-1, s); tileBoard(tr + s, tc+s, dr + s-1, dc + s , s); tileBoard(tr + s, tc + s, dr + s, dc + s-1, s); } } void Test1() { /*int a = 1; char b = 2; short c = 4;*/ tileBoard(0, 0,0,0,8); Print(8); }
运行结果:
时间: 2024-10-27 07:30:44