ZOJ 3209 Treasure Map (Dancing Links)

Treasure Map

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2015-04-09)

Description

Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now broken to many rectangular pieces, and what make it worse, he has lost some of the pieces. Luckily, it is possible to figure out the position of each piece in the original map. Now the boss asks you, the talent programmer, to make a complete treasure map with these pieces. You need to make only one complete map and it is not necessary to use all the pieces. But remember, pieces are not allowed to overlap with each other (See sample 2).

Input

The first line of the input contains an integer T (T <= 500), indicating the number of cases.

For each case, the first line contains three integers nmp (1 <= nm <= 30, 1 <= p <= 500), the width and the height of the map, and the number of pieces. Then p lines follow, each consists of four integers x1y1x2y2 (0 <= x1 < x2 <= n, 0 <= y1 < y2 <= m), where (x1, y1) is the coordinate of the lower-left corner of the rectangular piece, and (x2, y2) is the coordinate of the upper-right corner in the original map.

Cases are separated by one blank line.

ZOJ 3209 Treasure Map (Dancing Links)

Output

If you can make a complete map with these pieces, output the least number of pieces you need to achieve this. If it is impossible to make one complete map, just output -1.

Sample Input

3
5 5 1
0 0 5 5 5 5 2
0 0 3 5
2 0 5 5 30 30 5
0 0 30 10
0 10 30 20
0 20 30 30
0 0 15 30
15 0 30 30

Sample Output

1
-1
2

Hint

For sample 1, the only piece is a complete map.

For sample 2, the two pieces may overlap with each other, so you can not make a complete treasure map.

For sample 3, you can make a map by either use the first 3 pieces or the last 2 pieces, and the latter approach one needs less pieces.

*啊!!!!N和M弄反了啊!!!!!T了一下午啊!!!!!!!!找BUG都快找哭了啊!!!!!!!!!!

丧心病狂弄了个M行N列啊!!!!!!!!!!这尼玛反人类啊!!!!!!活生生由300ms优化到了80ms啊!!!!!!!!!

 #include <iostream>
#include <cstdio>
using namespace std; const int SIZE = * ;
const int HEAD = ;
int U[SIZE],D[SIZE],L[SIZE],R[SIZE],C[SIZE],S[SIZE];
int N,M;
int ANS = 0x7fffffff; void ini(void);
void dancing(int ans);
void remove(int);
void resume(int);
int main(void)
{
int t,p,count;
int x_1,y_1,x_2,y_2;
int col; scanf("%d",&t);
while(t --)
{
ANS = 0x7fffffff;
scanf("%d%d%d",&N,&M,&p);
ini();
count = N * M + ;
while(p --)
{
scanf("%d%d%d%d",&x_1,&y_1,&x_2,&y_2); int first = count;
for(int i = x_1;i < x_2;i ++)
for(int j = y_1;j < y_2;j ++)
{
col = i * M + j + ;
U[count] = U[col];
D[count] = col;
L[count] = count - ;
R[count] = count + ; D[U[col]] = count;
U[col] = count; C[count] = col;
++ S[col];
++ count;
}
R[count - ] = first;
L[first] = count - ;
}
dancing();
if(ANS == 0x7fffffff)
puts("-1");
else
printf("%d\n",ANS);
} return ;
} void ini(void)
{
L[HEAD] = N * M;
R[HEAD] = ;
U[HEAD] = D[HEAD] = S[HEAD] = C[HEAD] = HEAD; for(int i = ;i <= N * M;i ++)
{
U[i] = D[i] = i;
L[i] = i - ;
R[i] = i + ; C[i] = i;
S[i] = ;
}
R[N * M] = ;
} void dancing(int ans)
{
if(ans >= ANS)
return ; if(R[HEAD] == HEAD)
{
ANS = ans < ANS ? ans : ANS;
return ;
} int min_loc = R[HEAD];
for(int i = R[HEAD];i;i = R[i])
if(S[i] < S[min_loc])
min_loc = i; remove(min_loc);
for(int i = D[min_loc];i != min_loc;i = D[i])
{
for(int j = R[i];j != i;j = R[j])
remove(C[j]);
dancing(ans + );
for(int j = L[i];j != i;j = L[j])
resume(C[j]);
}
resume(min_loc); } void remove(int c)
{
R[L[c]] = R[c];
L[R[c]] = L[c]; for(int i = D[c];i != c;i = D[i])
for(int j = R[i];j != i;j = R[j])
{
D[U[j]] = D[j];
U[D[j]] = U[j];
-- S[C[j]];
}
} void resume(int c)
{
R[L[c]] = c;
L[R[c]] = c; for(int i = U[c];i != c;i = U[i])
for(int j = R[i];j != i;j = R[j])
{
D[U[j]] = j;
U[D[j]] = j;
++ S[C[j]];
}
}
上一篇:Java面试题整理二(侧重SSH框架)


下一篇:hdoj 2074 叠筐