ZOJ 3209 Treasure Map

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3209

dancing links 每个点作为一列。

每个块作为一行。

不知道为什么插节点的时候 如果把循环写成

for(int i = y1+1; i <= y2; i++){      //行
              for(int j = x1+1; j <= x2; j++){ //列
      
              }
          }然后col = (y-1)*n+x就会超时。
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <string>
  5 #include <iomanip>
  6 using namespace std;
  7 int T, n, m, p, x1, y1, x2, y2;
  8 #define maxn 510*35*35
  9 int R[maxn], L[maxn], U[maxn], D[maxn];
 10 int H[maxn], C[maxn], colsum[999];
 11 int head, cnt, ans;
 12 void addnode(int i, int j, int h, int rowhead, int sum, int pre){
 13     int col = j+(i-1)*m; //cout<<col<<" ";
 14     cnt++;
 15
 16     H[cnt] = h; C[cnt] = col;
 17     if(sum == 1){
 18         R[cnt] = cnt; L[cnt] = cnt;
 19     }
 20     else if(sum == 2){
 21         R[pre] = cnt; R[cnt] = pre;
 22         L[pre] = cnt; L[cnt] = pre;
 23     }
 24     else{
 25         R[cnt] = rowhead; R[pre] = cnt;
 26         L[cnt] = pre; L[rowhead] = cnt;
 27     }
 28
 29     D[U[col]] = cnt;
 30     U[cnt] = U[col];
 31     D[cnt] = col;
 32     U[col] = cnt;
 33
 34     colsum[col]++;
 35
 36 }
 37 void init(){
 38     head = cnt = 0;
 39     ans = 510;  //最多就是500块。
 40     R[head] = 1; L[head] = n*m; U[head] = head; D[head] = head;
 41     H[head] = 0; C[head] = 0;
 42     memset(colsum, 0, sizeof(colsum));
 43     for(int i = 1; i <= n*m; i++){
 44         cnt++;
 45         U[cnt] = D[cnt] = cnt;
 46         H[cnt] = 0; C[cnt] = i;
 47         if(i == n*m){
 48             R[cnt-1] = cnt; L[cnt] = cnt-1; R[cnt] = head;
 49         }
 50         else{
 51             R[cnt-1] = cnt; L[cnt] = cnt-1;
 52         }
 53     }
 54     for(int cc = 1; cc <= p; cc++){
 55         scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
 56         int sum = 0, rowhead = -1, pre;
 57         for(int i = x1+1; i <= x2; i++){      //行
 58             for(int j = y1+1; j <= y2; j++){ //列
 59                 sum++;
 60                 addnode(i, j ,cc, rowhead, sum, pre);
 61                 if(rowhead == -1) rowhead = cnt;
 62                 pre = cnt;
 63             }
 64         }
 65     }
 66 }
 67 void remove(int c){
 68     R[L[c]] = R[c]; L[R[c]] = L[c];
 69     for(int i = D[c]; i != c; i = D[i]){
 70         for(int j = R[i]; j != i; j = R[j]){
 71             U[D[j]] = U[j]; D[U[j]] = D[j];
 72             colsum[C[j]]--;
 73         }
 74     }
 75 }
 76 void resume(int c){
 77     R[L[c]] = c; L[R[c]] = c;
 78     for(int i = D[c]; i != c; i = D[i]){
 79         for(int j = R[i]; j != i; j = R[j]){
 80             U[D[j]] = j; D[U[j]] = j;
 81             colsum[C[j]]++;
 82         }
 83     }
 84 }
 85 void dance(int k){
 86     if(k >= ans) return;
 87     int c = R[head];
 88     if(c == head){
 89         ans = k;
 90         return;
 91     }
 92     int min = 9999999;
 93     for(int i = R[head]; i != head; i = R[i]){
 94         if(colsum[i] <= min){
 95             min = colsum[i]; c = i;
 96         }
 97     }
 98     remove(c);
 99     for(int i = D[c]; i != c; i = D[i]){
100         for(int j = R[i]; j != i; j = R[j]) remove(C[j]);
101         dance(k+1);
102         for(int j = L[i]; j != i; j = L[j]) resume(C[j]); //要写成L
103     }
104     resume(c);
105     return ;
106 }
107 int main(){
108     scanf("%d", &T);
109     while(T--){
110         scanf("%d%d%d", &n, &m, &p); //n是列,m是行。
111         init();
112         dance(0);
113         if(ans == 510) printf("-1\n");
114         else printf("%d\n", ans);
115     }
116
117     return 0;
118 }
时间: 2024-08-02 19:19:43

ZOJ 3209 Treasure Map的相关文章

ZOJ 3209 Treasure Map (Dancing Links)

Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB 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. Luck

ZOJ 3209 Treasure Map(舞蹈链)

题目链接:[kuangbin带你飞]专题三 Dancing Links B - Treasure Map 题意 给一矩形和k个小矩形,问选取最小数量为多少的小矩形可以对大矩形进行精确覆盖. 思路 仍然是个模版题,把二维的n*m的大矩形看作是一维的n*m的一条线.k个小矩形同理,那么就转化成01矩阵精确覆盖的问题了. 代码 #include <iostream> #include <algorithm> #include <cstring> #include <cs

ZOJ 3209 Treasure Map(DLX精确覆盖)

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

[ACM] ZOJ 3209 Treasure Map ( Dancing Links 精确覆盖,矩形覆盖)

Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB 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. Luck

(简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。

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 e

ZOJ 3209 Treasure Map DLX

用最少的矩阵覆盖n*m的地图,注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但由于矩阵拼接过程中,有公共的边,这里需要的技巧就是把矩阵的左边和下面截去一个单位. #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #

ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )

题意 :  给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m )  .然后给你 p 个小矩形 , 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选择最小的几个矩形 , 使得这些矩形可以覆盖整个矩形 , 并且互相不会重叠 .( n , m <= 30 ) 思路 : Dancing Links 的精确覆盖问题 . 我们将 n * m 的矩形分成 n * m 个小正方形 ,那么我们只要保证每个小正方形被覆盖且只被覆盖一次即可 . 那么列表示每个小正

zoj 3209.Treasure Map(DLX精确覆盖)

直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <vector> using namespace std; #define FOR(i,A,s) for(int i = A[s]; i != s; i = A[i]) #define exp 1e-8 const int MAX =

ZOJ 3209 Treasure Map 精确覆盖

题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 #define ll long long 5 #define mk(x, y) make_pair(x, y) 6 #define lson l, m, rt<<1 7 #define mem(a) memset(