传送门:【HDU】3209 Treasure Map
题目分析:精确覆盖模板题。。。将矩形的行*列转化为列。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; #define REP( i , a , b ) for ( int i = a ; i < b ; ++ i ) #define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i ) #define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i ) #define REC( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] ) #define CLR( a , x ) memset ( a , x , sizeof a ) const int MAXN = 1000 ; const int MAXNODE = 460000 ; const int INF = 0x3f3f3f3f ; struct DLX { int U[MAXNODE] , D[MAXNODE] , L[MAXNODE] , R[MAXNODE] ; int row[MAXNODE] , col[MAXNODE] ; int S[MAXN] , H[MAXN] ; int deep , ans[MAXN] ; int n , m , q ; int size ; void init () { CLR ( H , -1 ) ; FOR ( i , 0 , n ) { S[i] = 0 ; U[i] = i ; D[i] = i ; L[i] = i - 1 ; R[i] = i + 1 ; } L[0] = n ; R[n] = 0 ; size = n ; deep = INF ; } void link ( int r , int c ) { ++ size ; ++ S[c] ; row[size] = r ; col[size] = c ; U[size] = U[c] ; D[size] = c ; D[U[c]] = size ; U[c] = size ; if ( ~H[r] ) { L[size] = L[H[r]] ; R[size] = H[r] ; L[R[size]] = size ; R[L[size]] = size ; } else H[r] = L[size] = R[size] = size ; } void remove ( int c ) { L[R[c]] = L[c] ; R[L[c]] = R[c] ; REC ( i , D , c ) REC ( j , R , i ) { U[D[j]] = U[j] ; D[U[j]] = D[j] ; -- S[col[j]] ; } } void resume ( int c ) { REC ( i , U , c ) REC ( j , L , i ) { ++ S[col[j]] ; D[U[j]] = j ; U[D[j]] = j ; } R[L[c]] = c ; L[R[c]] = c ; } void dance ( int d ) { if ( R[0] == 0 ) { deep = min ( deep , d ) ; return ; } int c = R[0] ; REC ( i , R , 0 ) if ( S[c] > S[i] ) c = i ; remove ( c ) ; REC ( i , D , c ) { //ans[d] = row[i] ; REC ( j , R , i ) remove ( col[j] ) ; dance ( d + 1 ) ; REC ( j , L , i ) resume ( col[j] ) ; } resume ( c ) ; } void solve () { int x1 , x2 , y1 , y2 ; scanf ( "%d%d%d" , &n , &m , &q ) ; n *= m ; init () ; FOR ( r , 1 , q ) { scanf ( "%d%d%d%d" , &x1 , &y1 , &x2 , &y2 ) ; FOR ( i , x1 + 1 , x2 ) FOR ( j , y1 + 1 , y2 ) link ( r , ( i - 1 ) * m + j ) ; } dance ( 0 ) ; printf ( "%d\n" , deep == INF ? -1 : deep ) ; } } dlx ; int main () { int T ; scanf ( "%d" , &T ) ; while ( T -- ) dlx.solve () ; return 0 ; }