分割矩形

探讨了一个有趣的几何问题:在L*L的正方形中分布着n个点,如何通过切割将蛋糕分割成n个单独的小正方形,每个小正方形内部恰好包含1颗枣。该问题涉及到回溯算法的运用,寻找可能的切割方案。

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

在一个L*L的正方形里有n个点,问是否有一种切割方法把蛋糕切成n个小正方形,满足每个小正方形里面恰好有1颗枣。

#include<string.h>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;

const int maxn=100;
long tot[maxn][maxn];
long isin[maxn][maxn];
long L;
int fill(long x,long y)
{
	long tx,ty,i,j,k,nNode,flag;
	while(1)
	{
		if(y>L)
		{
			x++;
			y=1;
		}
		if(x>L)return 1;
		if(!isin[x][y])break;
		else y++;
	}
	for(k=1;;k++)
	{
		tx=x+k-1;
		ty=y+k-1;
		if(tx>L||ty>L)break;
		nNode=tot[tx][ty]-tot[tx][y-1]-tot[x-1][ty]+tot[x-1][y-1];
		if(nNode>1)break;
		if(nNode==1)
		{
			for(i=x;i<x+k;i++)
			for(j=y;j<y+k;j++)
			isin[i][j]=1;
			if(fill(x,y+1))return 1;
			for(i=x;i<x+k;i++)
			for(j=y;j<y+k;j++)
			isin[i][j]=0;
		}
	}
	return 0;
}

int main()
{
	long cases;
	int i,j,N;
	long x,y,a;
	scanf("%ld",&cases);
	while(cases--)
	{
		scanf("%ld%ld",&L,&N);
		memset(tot,0,sizeof(tot));
		while(N--)
		{
			scanf("%ld%ld",&x,&y);
			tot[x][y]=1;
		}
		for(i=1;i<=L;i++)
		for(j=1;j<=L;j++)
		tot[i][j]=tot[i-1][j]+tot[i][j-1]-tot[i-1][j-1]+tot[i][j];
		memset(isin,0,sizeof(isin));
		if(fill(1,1))printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值