棋盘覆盖问题【分治】

棋盘覆盖问题
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 95, Accepted users: 36
Problem 10432 : No special judgement
Problem description
  在一个2k x 2k ( 即:2^k x 2^k )个方格组成的棋盘中,恰有一个方格与其它方格不同,称该方格为一特殊方格,且称该棋盘为一特殊棋盘。在棋盘覆盖问题中,要用图示的4种不同形态的L型骨牌覆盖给定的特殊棋盘上除特殊方格以外的全部方格,且不论什么2L型骨牌不得重叠覆盖。


Input
  输入文件第一行是一个整数T,表示有多少组測试数据,接下来是T组測试数据,共2T行,每组第一行为整数n,2n次幂(1<=n<=64),表示棋盘的大小为n*n,第二行是两个整数,代表特殊方格所在行号和列号。
Output
  先输出“CASE:i,然后按例子输出。数据间用制表符隔开(‘t’),每行最后一个数据后无制表符。
Sample Input
2
2
0 0
8
2 2
Sample Output
CASE:1
0       1
1       1
CASE:2
3       3       4       4       8       8       9       9
3       2       2       4       8       7       7       9
5       2       0       6       10      10      7       11
5       5       6       6       1       10      11      11
13      13      14      1       1       18      19      19
13      12      14      14      18      18      17      19
15      12      12      16      20      17      17      21
15      15      16      16      20      20      21      21
Judge Tips
  要求遍历顺序按从左到右,从上到下。
Problem Source
  qshj

#include <stdio.h>
#define maxn 66

int map[maxn][maxn], count;

void chessBoard(int r, int c, int dr, int dc, int size)
{
	if(size == 1) return;
	size >>= 1;
	int t = size, countt = count++;
	//is it in to-left
	if(dr < r + t && dc < c + t) //is
		chessBoard(r, c, dr, dc, size);
	else{ //not
		map[r+t-1][c+t-1] = countt;
		chessBoard(r, c, r+t-1, c+t-1, size);
	}
	//is it in top-right
	if(dr < r + t && dc >= c + t)
		chessBoard(r, c + t, dr, dc, size);
	else{
		map[r+t-1][c+t] = countt;
		chessBoard(r, c + t, r+t-1, c+t, size);
	}
	//is it in buttom-left
	if(dr >= r + t && dc < c + t)
		chessBoard(r + t, c, dr, dc, size);
	else{
		map[r+t][c+t-1] = countt;
		chessBoard(r + t, c, r+t, c+t-1, size);
	}
	//is it in buttom-right
	if(dr >= r + t && dc >= c + t)
		chessBoard(r+t, c+t, dr, dc, size);
	else{
		map[r+t][c+t] = countt;
		chessBoard(r+t, c+t, r+t, c+t, size);
	}
}

void PrintBoard(int n)
{
	int i, j;
	for(i = 0; i < n; ++i)
		for(j = 0; j < n; ++j)
			if(j != n - 1)
				printf("%d	", map[i][j]);
			else printf("%d
", map[i][j]);
}

int main()
{
	int t, n, dr, dc, cas = 1;
	scanf("%d", &t);
	while(t--){
		scanf("%d%d%d", &n, &dr, &dc);
		count = 1;
		map[dr][dc] = 0;
		chessBoard(0, 0, dr, dc, n);
		printf("CASE:%d
", cas++);
		PrintBoard(n);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/bhlsheji/p/4259856.html