算法竞赛宝典 分治算法 残缺棋盘

//从整体的角度,一步一步递归,一步一步分块分治解决

#include<iostream>
using namespace std;

int chess[1050][1050];
int k, x, y;
int tmp;

void lt(int x, int y)
{
	chess[x][y + 1] = 4;
	chess[x + 1][y + 1] = 4;
	chess[x + 1][y] = 4;
}
void lb(int x, int y)
{
	chess[x - 1][y] = 2;
	chess[x - 1][y + 1] = 2;
	chess[x][y + 1] = 2;
}
void rt(int x, int y)
{
	chess[x][y - 1] = 3;
	chess[x + 1][y - 1] = 3;
	chess[x + 1][y] = 3;
}
void rb(int x, int y)
{
	chess[x - 1][y - 1] = 1;
	chess[x][y - 1] = 1;
	chess[x - 1][y] = 1;
}
void  work(int x1,int y1,int x2,int y2,int kk)
{
	int px, py;
	if (kk == 0)
		return;
	for (int i = x1; i <= x2; i++)
	{
		for (int j = y1; j <= y2; j++)
			if (chess[i][j])
			{
				px = i;
				py = j;
			}
	}
	if ((x1 + x2) / 2 >= px)
	{
		if ((y1 + y2) / 2 >= py)
			lt((x1 + x2) / 2, (y1 + y2) / 2);
		else
			rt((x1 + x2) / 2, (y1 + y2) / 2+1);
	}
	else
	{
		if ((y1 + y2) / 2 >= py)
			lb((x1 + x2) / 2+1, (y1 + y2) / 2);
		else
			rb((x1 + x2) / 2+1, (y1 + y2) / 2+1);
	}
	work(x1, y1, (x1 + x2) / 2, (y1 + y2) / 2, kk - 1);
	work(x1, (y1 + y2) / 2+1, (x1 + x2) / 2, y2, kk - 1);
	work((x1 + x2) / 2 + 1, y1, x2, (y1 + y2) / 2, kk - 1);
	work((x1 + x2) / 2 + 1, (y1 + y2) / 2 + 1, x2, y2, kk - 1);
}

int main()
{
	while (cin >> k >> y >> x)
	{
		memset(chess, 0, sizeof(chess));
		chess[x][y] = 7;
		tmp = pow(2, k);
		work(1,1,tmp,tmp,k);
		for (int i = 1; i <= tmp; i++)
		{
			int j;
			for (j = 1; j < tmp; j++)
				printf("%d ", chess[i][j]);
			printf("%d\n", chess[i][j]);
		}
	}
	return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值