残缺棋盘 【分治】

本文介绍了一道关于N*N棋盘中,有一格残缺,需用L形状的3连格填充的题目。通过采用分治算法,将大棋盘划分为4个部分,并对含有残缺格的部分进行特殊处理,逐步解决棋盘填充问题。当棋盘大小为2*2时,问题得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:

  

题目大意:

  N*N的棋盘,有一格残缺(R,C),问用L形状的3连格如何填满除了残缺格的棋盘。

题目思路:

  【分治】

  把问题一般化,左上角为(x,y),棋盘大小为s*s,残缺格位置为(r,c)的填充。

  现在考虑,每一步把大的棋盘等分成4块

  A B

  C D 

  不妨假设残缺格(r,c)在A中,那么B C D人为定义残缺格为靠近中心点的位置。

  这样就凑了一个L格出来,接下来只要继续对A B C D做残缺格填充即可。

  出口是棋盘大小为2*2



//
//by coolxxx
/*
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<bitset>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
//#include<stdbool.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
*/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 104
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int r,c;
int a[N][N];
void work(int x,int y,int s,int r,int c)
{
	int xx,yy;
	if(s==2)
	{
		lll++;
		if(r!=x || c!=y)a[x][y]=lll;
		if(r!=x || c!=y+1)a[x][y+1]=lll;
		if(r!=x+1 || c!=y)a[x+1][y]=lll;
		if(r!=x+1 || c!=y+1)a[x+1][y+1]=lll;
		return;
	}
	s/=2;
	xx=x+s;
	yy=y+s;
	if(r<xx && c<yy)
	{
		work(x,y,s,r,c);
		work(x,yy,s,xx-1,yy);
		work(xx,y,s,xx,yy-1);
		work(xx,yy,s,xx,yy);
		a[xx-1][yy]=a[xx][yy-1]=a[xx][yy]=++lll;
	}
	else if(r<xx && c>=yy)
	{
		work(x,y,s,xx-1,yy-1);
		work(x,yy,s,r,c);
		work(xx,y,s,xx,yy-1);
		work(xx,yy,s,xx,yy);
		a[xx-1][yy-1]=a[xx][yy-1]=a[xx][yy]=++lll;
	}
	else if(r>=xx && c<yy)
	{
		work(x,y,s,xx-1,yy-1);
		work(x,yy,s,xx-1,yy);
		work(xx,y,s,r,c);
		work(xx,yy,s,xx,yy);
		a[xx-1][yy-1]=a[xx-1][yy]=a[xx][yy]=++lll;
	}
	else//r>=xx && c>=yy
	{
		work(x,y,s,xx-1,yy-1);
		work(x,yy,s,xx-1,yy);
		work(xx,y,s,xx,yy-1);
		work(xx,yy,s,r,c);
		a[xx-1][yy-1]=a[xx-1][yy]=a[xx][yy-1]=++lll;
	}
}
void print()
{
	int i,j;
	for(i=1;i<=n;i++,puts(""))
		for(j=1;j<=n;j++)
			printf("%d\t",a[i][j]);
}
int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k,l;
	int x,y,z;
//	for(scanf("%d",&cass);cass;cass--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s))
	while(~scanf("%d",&n))
	{
		lll=0;
		scanf("%d%d",&x,&y);
		work(1,1,n,x,y);
		print();
	}
	return 0;
}
/*
//

//
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值