1 问题描述
在一个2^kx2^k(k≥0)个方格组成的棋盘中,恰有一一个方格与其他方格不同,称该方格为特殊方格。显然,特殊方格在棋盘中可能出现的位置有4k种,因而有4k种不同的棋盘。棋盘覆盖问题要求用4种不同形状的L型骨牌覆盖给定棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖。
题目来源:《算法设计与分析》
2求解思路
2.1 实例分析
- 当k=0时,如图(a),只有一个特殊方格;当k=1时如图(b),(c),(d),(e)有四种不同的棋盘。
(2) 当k较大时,可以利用分治法的思想,将棋盘从中间一分为四,为了将其划分为四个相同的子问题,可以在分界处给无特殊方格的三个子问题补充一个特殊方格(即补充一个三格骨牌),然后继续按上述规则对子问题进行划分,直到k=1时,问题解便很容易得出。
例:
2.2 算法实现
(1)棋盘:整形二维数组cboard[side][side],其中side等于pow(2,k)。
(2)子棋盘:棋盘左上角的下标由lx,ly和secs来表示。
(3)特殊方格:cboard[sx][sy]表示特殊方格。
(4)三格骨牌:用全局整型变量num表示,从1开始连续编号1~(4 k 4^k4 k-1)/3,cboard中3个相同的整数表示一个三格骨牌。
3 源代码
#include<iostream>
using namespace std;
typedef long long ll;
const int N = 10001;
int k;
int x, y;
int num = 1;
ll cboard[N][N];
void CboardCoverage(ll lx, ll ly, ll sx, ll sy, ll side)
{
if (side == 1)return;//递归出口
int secs = side / 2; //分割棋盘
int t = num++; //取一个三格骨牌,其牌号为t
//处理左上角象限的子棋盘
if (sx < lx + secs && sy < ly + secs)
{
CboardCoverage(lx, ly, sx, sy, secs); //特殊方格在此象限中
}
else
{
cboard[lx + secs - 1][ly + secs - 1] = t; //此象限中无特殊方格
CboardCoverage(lx, ly, lx + secs - 1, ly + secs - 1, secs);
}
//处理右上角象限的子棋盘
if (sx < lx + secs && sy >= ly + secs)
{
CboardCoverage(lx, ly + secs, sx, sy, secs);
}
else
{
cboard[lx + secs - 1][ly + secs] = t;
CboardCoverage(lx, ly + secs, lx + secs - 1, ly + secs, secs);
}
//处理左下角象限的子棋盘
if (sx >= lx + secs && sy < ly + secs)
{
CboardCoverage(lx + secs, ly, sx, sy, secs);
}
else
{
cboard[lx + secs][ly + secs - 1] = t;
CboardCoverage(lx + secs, ly, lx + secs, ly + secs - 1, secs);
}
//处理右下角象限的子棋盘
if (sx >= lx + secs && sy >= ly + secs)
{
CboardCoverage(lx + secs, ly + secs, sx, sy, secs);
}
else
{
cboard[lx + secs][ly + secs] = t;
CboardCoverage(lx + secs, ly + secs, lx + secs, ly + secs, secs);
}
}
signed main()
{
scanf("%d", &k);
ll side = 1 << k;
scanf("%lld%lld", &x, &y);//(x,y)为特殊方格的下标
cboard[x][y] = 0;//(x,y)为特殊方格的下标
CboardCoverage(0, 0, x, y, side);
for (int i = 0; i < side; i++)
{
for (int j = 0; j < side; j++)
printf("%lld ", cboard[i][j]);
puts(" ");
}
return 0;
}
代码说明
当k=2,x=2,y=2时所得结果如上图不同的数字代表不同的三格骨牌。
4 小结
棋盘覆盖问题主要涉及到分治的思想,把原棋盘中有没有方格的问题不断的通过递归缩小成子问题,通过解决每个组成的子棋盘上的问题,最终使多米诺骨牌将棋盘,完全覆盖。
进阶问题
P1228 地毯填补问题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<iostream>
#define MAXSIZE 1025
using namespace std;
int k;
int x, y;
int board[MAXSIZE][MAXSIZE];
int tile = 1;
void TileBoard(int tr, int tc, int dr, int dc, int size)
{
if (size == 1) return;
int t = tile++;
int s = size / 2;
//处理左上角象限的子棋盘
if (dr < tr + s && dc < tc + s)
TileBoard(tr, tc, dr, dc, s);
else {
board[tr + s - 1][tc + s - 1] = t;
TileBoard(tr, tc, tr + s - 1, tc + s - 1, s);
}
//处理右上角象限的子棋盘
if (dr < tr + s && dc >= tc + s)
TileBoard(tr, tc + s, dr, dc, s);
else {
board[tr + s - 1][tc + s] = t;
TileBoard(tr, tc + s, tr + s - 1, tc + s, s);
}
//处理左下角象限的子棋盘
if (dr >= tr + s && dc < tc + s)
TileBoard(tr + s, tc, dr, dc, s);
else {
board[tr + s][tc + s - 1] = t;
TileBoard(tr + s, tc, tr + s, tc + s - 1, s);
}
//处理右下角象限的子棋盘
if (dr >= tr + s && dc >= tc + s)
TileBoard(tr + s, tc + s, dr, dc, s);
else {
board[tr + s][tc + s] = t;
TileBoard(tr + s, tc + s, tr + s, tc + s, s);
}
}
signed main()
{
int k;
scanf("%d", &k);
int size = 1 << k;
int x, y;
scanf("%d", &x);
scanf("%d", &y);
board[x][y] = 0;
TileBoard(0, 0, x - 1, y - 1, size);
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
{
if (j - 1 >= 0 && i - 1 >= 0)
{
if (board[i][j - 1] == board[i][j] && board[i - 1][j] == board[i][j])
cout << i + 1 << " " << j + 1 << " " << 1 << endl;
}
if (i - 1 >= 0 && j + 1 < size)
{
if (board[i - 1][j] == board[i][j] && board[i][j + 1] == board[i][j])
cout << i + 1 << " " << j + 1 << " " << 2 << endl;
}
if (i + 1 < size && j - 1 >= 0)
{
if (board[i + 1][j] == board[i][j] && board[i][j] == board[i][j - 1])
cout << i + 1 << " " << j + 1 << " " << 3 << endl;
}
if (i + 1 < size && j + 1 < size)
{
if (board[i][j] == board[i + 1][j] && board[i][j] == board[i][j + 1])
cout << i + 1 << " " << j + 1 << " " << 4 << endl;
}
}
return 0;
}