首页 > 其他分享 >HDU 2589 正方形划分

HDU 2589 正方形划分

时间:2022-11-09 20:36:14浏览次数:45  
标签:sz HDU return int 2589 正方形 include true


Problem Description

一个边长为L的正方形可以分成 L*L个小正方形. 有N个石子放在 N个小正方形里,能否将它分成 N个 正方形,使得每个正方形里恰有一个石子且这N 个正方形恰好构成整个正方形 .


 


Input


输入数据首先包含一个整数T,表示测试实例的个数,然后是T组数据,每组第一行包含2个整数L,N,接下来有N行每行2个整数 r,c,表示第r行c列的小正方形里有一个石子 .1<L<=20;1<N<=L*L; 1<=r,c<=L.


 



Output


对于每个测试实例,如能将它分成 N个 正方形输出YES, 否则输出 NO


 



Sample Input


3

5 8
2 4
3 3
3 4
3 5
4 2
4 4
4 5
5 5

3 2
1 1
3 3

2 4
1 1
1 2
2 1
2 2


 



Sample Output


YES
NO
YES

 



没什么好说的,直接暴力的枚举每个正方形大小盖上去。

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int sz=25;
int T,n,m,x,y,s;
int c[sz][sz];
bool f[sz][sz];

bool check(int x,int y,int r)
{
return c[x+r][y+r]-c[x+r][y-1]-c[x-1][y+r]+c[x-1][y-1]==1;
}

bool dfs()
{
if (!s) return true;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (f[i][j])
{
for (int k=0;i+k<=n&&j+k<=n;k++)
{
if (check(i,j,k))
{
s-=k*k;
for (int r=0;r<=k;r++)
{
for (int w=0;w<=k;w++)
{
f[i+r][j+w]=false;
}
}
if (dfs()) return true;
s+=k*k;
for (int r=0;r<=k;r++)
{
for (int w=0;w<=k;w++)
{
f[i+r][j+w]=true;
}
}
}
}
return false;
}
}
}
}

int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
s=n*n;
memset(c,0,sizeof(c));
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
c[x][y]=1;
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
c[i][j]+=c[i-1][j]+c[i][j-1]-c[i-1][j-1];
f[i][j]=true;
}
}
printf("%s\n",dfs()?"YES":"NO");
}
return 0;
}



标签:sz,HDU,return,int,2589,正方形,include,true
From: https://blog.51cto.com/u_15870896/5838730

相关文章

  • HDU 3085 Nightmare Ⅱ
    ProblemDescriptionLastnight,littleerriyuehadahorriblenightmare.Hedreamedthatheandhisgirlfriendweretrappedinabigmazeseparately.Mo......
  • HDU 3313 Key Vertex
    ProblemDescriptionYouneedwalkingfromvertexStovertexTinagraph.IfyouremoveonevertexwhichstopsyoufromwalkingfromStoT,thatverte......
  • HDU 3316 Mine sweeping
    ProblemDescriptionIthinkmostofyouareusingsystemnamedofxporvistaorwin7.Andthesesystemisconsistofafamousgamewhatisminesweeping......
  • HDU 3355 Hop — Don’t Walk!
    ProblemDescriptionKERMITTHEFROGisaclassicvideogamewithasimplecontrolandobjectivebutrequiresagooddealofthinking.Youcontrolanani......
  • HDU 3427 Clickomania
    ProblemDescriptionClickomaniaisapuzzleinwhichonestartswitharectangulargridofcellsofdifferentcolours.Ineachstep,aplayerselects("......
  • HDU 2209 翻纸牌游戏
    ProblemDescription有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,......
  • HDU 2216 Game III
    ProblemDescriptionZjtandSarawilltakepartinagame,namedGameIII.ZjtandSarawillbeinamaze,andZjtmustfindSara.Therearesomestrang......
  • HDU 1010 Tempter of the Bone
    DescriptionThedoggiefoundaboneinanancientmaze,whichfascinatedhimalot.However,whenhepickeditup,themazebegantoshake,andthedoggie......
  • HDU 1017 A Mathematical Curiosity
    DescriptionGiventwointegersnandm,countthenumberofpairsofintegers(a,b)suchthat0<a<b<nand(a^2+b^2+m)/(ab)isaninteger. Thi......
  • HDU 5605 geometry
    ProblemDescriptionThereisapoint  atcoordinate .Alinegoesthroughthepoint,andintersectswiththepostivepartof  axesatpoint .Plea......