http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3209
去年L队友写的舞蹈链 今年W队友写的搜索。。再水一次省赛
把右上角都存到左下角 按格子搜索 先行后列扫到的第一个空格子是必须要现在填满的 看有没有对应图案能填 搜满就更新答案
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N=0x3f3f3f3f;
const int maxn=50;
const int maxm=5e2+10;
struct node
{
vector <int> x,y;
};
node pre[maxn][maxn];
int book[maxn][maxn];
int n,m,p,ans;
void dfs(int cnt)
{
int k,i,j,x1,y1,x2,y2,flag;
if(ans<=cnt) return;
flag=1;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(!book[i][j]){
x1=i,y1=j,flag=0;
break;
}
}
if(!flag) break;
}
if(flag){
ans=cnt;
return;
}
for(k=0;k<pre[x1][y1].x.size();k++){
x2=pre[x1][y1].x[k];
y2=pre[x1][y1].y[k];
flag=1;
for(i=x1;i<=x2;i++){
for(j=y1;j<=y2;j++){
if(book[i][j]){
flag=0;
break;
}
}
if(!flag) break;
}
if(flag){
for(i=x1;i<=x2;i++){
for(j=y1;j<=y2;j++){
book[i][j]=1;
}
}
dfs(cnt+1);
for(i=x1;i<=x2;i++){
for(j=y1;j<=y2;j++){
book[i][j]=0;
}
}
}
}
}
int main()
{
int t,i,j,x1,y1,x2,y2;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&p);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
pre[i][j].x.clear();
pre[i][j].y.clear();
}
}
for(i=1;i<=p;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++,y1++;
pre[x1][y1].x.pb(x2);
pre[x1][y1].y.pb(y2);
}
memset(book,0,sizeof(book));
ans=N;
dfs(0);
if(ans==N) printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}