ZOJ 3209 Treasure Map (Dancing Links)

Source: Internet
Author: User
Tags x2 y2

Treasure MapTime limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld &%llu< /c7> SubmitStatusPracticeZOJ 3209Appoint Description:System Crawler (2015-04-09)

Description

Your boss once had got many copies of a treasure map. Unfortunately, all the copies is now broken to many rectangular pieces, and what do it worse, he has lost some of the P Ieces. Luckily, it's 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's not necessary to use all the pieces. But remember, pieces is not allowed to overlap with each other (see Sample 2).

Input

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

For each case, the first line contains three integers nmp (1 <= n, m <= 1 <= P <=), the width and the height of the map, and the number of pieces. Then p lines follow, each consists of four integers x1 y1 x2 y2 (0 <= X1 < x2 <= n, 0 <= y1 < y2 <= m), where (x1, y 1) 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 is separated by one blank line.

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-make one complete map, just output-1.

Sample Input

35 5 10 0 5 55 5 20 0 3 52 0 5 530 30 50 0 30 100 10 30 200 20 30 300 0 15 3015 0 30 30

Sample Output

1-12

Hint

For Sample 1, the only piece are a complete map.

For Sample 2, the both pieces may overlap with each of the other so that you can do a complete treasure map.

For Sample 3, you can make a map by either with the first 3 pieces or the last 2 pieces, and the latter approach one needs Less pieces.

Grass Mud Horse!!!! N and M are reversed AH!!!!! T had an afternoon AH!!!!!!!! Looking for bugs, crying,!!!!!!!!!!.

I got a M-row N-column,!!!!!!!!!!. This Nima anti-human,!!!!!!. 300ms optimized to 80ms,!!!!!!!!!.

1#include <iostream>2#include <cstdio>3 using    namespacestd;4 5 Const    intSIZE =1005*505;6 Const    intHEAD =0;7 intU[size],d[size],l[size],r[size],c[size],s[size];8 intn,m;9 intANS =0x7fffffff;Ten  One voidInivoid); A voidDancingintans); - voidRemoveint); - voidResumeint); the intMainvoid) - { -     intT,p,count; -     intx_1,y_1,x_2,y_2; +     intCol; -  +scanf"%d",&t); A      while(T--) at     { -ANS =0x7fffffff; -scanf"%d%d%d",&n,&m,&p); - ini (); -Count = N * M +1; -          while(P--) in         { -scanf"%d%d%d%d",&x_1,&y_1,&x_2,&y_2); to  +             intFirst =count; -              for(inti = X_1;i < X_2;i + +) the                  for(intj = Y_1;j < Y_2;j + +) *                 { $Col = i * M + j +1;Panax NotoginsengU[count] =U[col]; -D[count] =Col; theL[count] = count-1; +R[count] = count +1; A  theD[u[col]] =count; +U[col] =count; -  $C[count] =Col; $++S[col]; -++count; -                 } theR[count-1] =First ; -L[first] = count-1;Wuyi         } theDancing0); -         if(ANS = =0x7fffffff) WuPuts"-1"); -         Else Aboutprintf"%d\n", ANS); $     } -  -     return    0; - } A  + voidInivoid) the { -L[head] = N *M; $R[head] =1; theU[head] = D[head] = S[head] = C[head] =HEAD; the  the      for(inti =1; I <= N * m;i + +) the     { -U[i] = D[i] =i; inL[i] = i-1; theR[i] = i +1; the  AboutC[i] =i; theS[i] =0; the     } theR[n * M] =0; + } -  the voidDancingintans)Bayi { the     if(Ans >=ANS) the         return    ; -  -     if(R[head] = =HEAD) the     { theAns = ans < ans?Ans:ans; the         return    ; the     } -  the     intMin_loc =R[head]; the      for(inti = R[head];i;i =R[i]) the         if(S[i] <S[min_loc])94Min_loc =i; the  the Remove (min_loc); the      for(inti = d[min_loc];i! = Min_loc;i =D[i])98     { About          for(intj = r[i];j! = I;j =R[j]) - Remove (c[j]);101Dancing (ans +1);102          for(intj = l[i];j! = I;j =L[j])103 Resume (c[j]);104     } the Resume (min_loc);106 107 }108 109 voidRemoveintc) the {111R[L[C]] =R[c]; theL[R[C]] =L[c];113  the      for(inti = d[c];i! = C;i =D[i]) the          for(intj = r[i];j! = I;j =R[j]) the         {117D[U[J]] =D[j];118U[D[J]] =U[j];119--S[c[j]]; -         }121 }122 123 voidResumeintc)124 { theR[L[C]] =C;126L[R[C]] =C;127  -      for(inti = u[c];i! = C;i =U[i])129          for(intj = r[i];j! = I;j =R[j]) the         {131D[U[J]] =J; theU[D[J]] =J;133++S[c[j]];134         }135}

ZOJ 3209 Treasure Map (Dancing Links)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.