1. 程式人生 > >ZOJ 3209 Treasure Map (Dancing Links)

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. 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 n m p (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 x1

 y1 x2 y2 (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.

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.

Author: HANG, Hang
Source: The 6th Zhejiang Provincial Collegiate Programming Contest

題目連結:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3372

就是簡單的精確覆蓋問題。

把每個格子當成一個列,要覆蓋所有格子。

寫一下Dancing Links模板就可以了

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2014/5/26 21:50:46
  4 File Name     :E:\2014ACM\專題學習\DLX\ZOJ3209.cpp
  5 ************************************************ */
  6 
  7 #include <stdio.h>
  8 #include <string.h>
  9 #include <iostream>
 10 #include <algorithm>
 11 #include <vector>
 12 #include <queue>
 13 #include <set>
 14 #include <map>
 15 #include <string>
 16 #include <math.h>
 17 #include <stdlib.h>
 18 #include <time.h>
 19 using namespace std;
 20 const int maxnode = 500010;
 21 const int MaxM = 1010;
 22 const int MaxN = 510;
 23 struct DLX
 24 {
 25     int n,m,size;
 26     int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
 27     int H[MaxN],S[MaxM];
 28     int ansd;
 29     void init(int _n,int _m)
 30     {
 31         n = _n;
 32         m = _m;
 33         for(int i = 0;i <= m;i++)
 34         {
 35             S[i] = 0;
 36             U[i] = D[i] = i;
 37             L[i] = i-1;
 38             R[i] = i+1;
 39         }
 40         R[m] = 0; L[0] = m;
 41         size = m;
 42         for(int i = 1;i <= n;i++)
 43             H[i] = -1;
 44     }
 45     void Link(int r,int c)
 46     {
 47         ++S[Col[++size]=c];
 48         Row[size] = r;
 49         D[size] = D[c];
 50         U[D[c]] = size;
 51         U[size] = c;
 52         D[c] = size;
 53         if(H[r] < 0)H[r] = L[size] = R[size] = size;
 54         else
 55         {
 56             R[size] = R[H[r]];
 57             L[R[H[r]]] = size;
 58             L[size] = H[r];
 59             R[H[r]] = size;
 60         }
 61     }
 62     void remove(int c)
 63     {
 64         L[R[c]] = L[c]; R[L[c]] = R[c];
 65         for(int i = D[c];i != c;i = D[i])
 66             for(int j = R[i];j != i;j = R[j])
 67             {
 68                 U[D[j]] = U[j];
 69                 D[U[j]] = D[j];
 70                 --S[Col[j]];
 71             }
 72     }
 73     void resume(int c)
 74     {
 75         for(int i = U[c];i != c;i = U[i])
 76             for(int j = L[i];j != i;j = L[j])
 77                 ++S[Col[U[D[j]]=D[U[j]]=j]];
 78         L[R[c]] = R[L[c]] = c;
 79     }
 80     void Dance(int d)
 81     {
 82         //剪枝下
 83         if(ansd != -1 && ansd <= d)return;
 84         if(R[0] == 0)
 85         {
 86             if(ansd == -1)ansd = d;
 87             else if(d < ansd)ansd = d;
 88             return;
 89         }
 90         int c = R[0];
 91         for(int i = R[0];i != 0;i = R[i])
 92             if(S[i] < S[c])
 93                 c = i;
 94         remove(c);
 95         for(int i = D[c];i != c;i = D[i])
 96         {
 97             for(int j = R[i];j != i;j = R[j])remove(Col[j]);
 98             Dance(d+1);
 99             for(int j = L[i];j != i;j = L[j])resume(Col[j]);
100         }
101         resume(c);
102     }
103 };
104 DLX g;
105 
106 int main()
107 {
108     //freopen("in.txt","r",stdin);
109     //freopen("out.txt","w",stdout);
110     int T;
111     int n,m,p;
112     scanf("%d",&T);
113     while(T--)
114     {
115         scanf("%d%d%d",&n,&m,&p);
116         g.init(p,n*m);
117         int x1,y1,x2,y2;
118         for(int k = 1;k <= p;k++)
119         {
120             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
121             for(int i = x1+1;i <= x2;i++)
122                 for(int j = y1+1;j <= y2;j++)
123                     g.Link(k,j + (i-1)*m);
124         }
125         g.ansd = -1;
126         g.Dance(0);
127         printf("%d\n",g.ansd);
128     }
129     return 0;
130 }

相關推薦

ZOJ 3209 Treasure Map Dancing Links

Treasure MapTime Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now

【POJ3074】Sudoku DLXDancing Links

puts struct pre i++ 能夠 ring include 為什麽 處理 數獨就要DLX,不然不樂意。 數獨的DLX構造:9*9個點每一個點有9種選擇,這構成了DLX的729行,每行、列、陣有限制,均為9行(/列/陣),然後每行(/列/陣)都有九

【POJ3740】Easy Finding DLXDancing Links精確覆蓋問題

ren .cn string 應該 進行 int 函數 操作 urn 題意:多組數據。每組數據給你幾行數,要求選出當中幾行,使得每一列都有且僅有一個1,詢問是可不可行,或者說能不能找出來。 題解:1、暴搜。2、DLX(Dancing links)。 本文寫的是DLX。算

演算法實踐——舞蹈鏈Dancing Links演算法求解數獨

本文介紹該演算法的實際運用,利用舞蹈鏈(Dancing Links)演算法求解數獨 在前文中可知,舞蹈鏈(Dancing Links)演算法在求解精確覆蓋問題時效率驚人。 那利用舞蹈鏈(Dancing Links)演算法求解數獨問題,實際上就是下面一個流程 1、把數獨問題轉換為精確覆蓋問題 2

跳躍的舞者,舞蹈鏈Dancing Links演算法——求解精確覆蓋問題

精確覆蓋問題的定義:給定一個由0-1組成的矩陣,是否能找到一個行的集合,使得集合中每一列都恰好包含一個1 例如:如下的矩陣 就包含了這樣一個集合(第1、4、5行) 如何利用給定的矩陣求出相應的行的集合呢?我們採用回溯法 矩陣1: 先假定選擇第1行,如下所示: 如上圖中所示,紅色

ZOJ 3209 Treasure Map (DLX精確覆蓋問題)

題目大意: 給出一個n*m的矩形, n, m <= 30, 從p <= 500個矩形中選擇一些矩形使得這些矩形不重合但剛好拼湊出n*m的這個矩形(矩形位置都不能移動), 求從給出的矩形中最少需要挑出幾個才能滿足這個條件 大致思路: 就是將n*m個小的1*1的正

Exact cover Dancing Links 模板題

1017 - Exact cover 時間限制:15秒 記憶體限制:128兆 自定評測 5584 次提交 2975 次通過 題目描述There is an

POJ1066 Treasure Hunt線段相交

代碼 under rpo get 描述 rop float over put 題目鏈接:   http://poj.org/problem?id=1066 題目描述: Treasure Hunt Description Archeologists from the An

【C++11】unoedered_map和map部分轉載

好的 hash 比較 綜合 per 百萬 一點 應該 .net 1.結論 新版的hash_map都是unordered_map了,這裏只說unordered_map和map. 運行效率:unordered_map最高,而map效率較低但提供了穩定效率和有序的序列。 占用內存

ZOJ 3329 Problem Set 期望dp

logs name multipl center pla inter follow there ati One Person Game There is a very simple and interesting one-person game. You have

1131 Subway Map30 分

sel 鄰接表 stop 圖片 passing integer %d otto ons In the big cities, the subway systems always look so complex to the visitors. To give you som

java List<對象> 轉 Set、Map高級

@override arr demo print orange range turn return sta package com.demo.entity; public class Book { private int id; private S

ZOJ - 3983 - Crusaders Quest思維 + 暴力

代碼 連續 std -- ios 補齊 思路 iostream aaa 題意: 給出一個字符串,長度為9,包含三種各三個字母"a","g","o",如果一次消除連續三個一樣的分數+1,消完自動向左補齊 其中可以消去

thinking in java (十八) ----- 集合之MapHashMap HashTable總結

Map框架圖 Map概括 Map是鍵值對對映的抽象介面 AbstractMap實現了Map中的大部分介面,減少了Map實現類的重複程式碼 HashMap是基於拉鍊法實現的散列表,一般使用在單執行緒程式中 HashTable是基於拉鍊法

ZOJ - 3469-Food Delivery 區間DP

When we are focusing on solving problems, we usually prefer to stay in front of computers rather than go out for lunch. At this time, we may call for

1111 Online Map 30 分兩次dij

1111 Online Map (30 分) Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two pat

PAT (Advanced Level) Practice 1111 Online Map 30 分

#include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=500+5; struct Edge { int v,l,t,next;

1131 Subway Map 30 分

In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the following figure shows the map of Beijing

1131 Subway Map 30 分(cj)

1131 Subway Map (30 分) In the big cities, the subway systems always look so complex to the visitors. To give you some sense, the followin

ZOJ——Course Selection System01揹包

題目連結: 因為c的值很小,所以以c看做重量,把h當做價值 01揹包,取儘可能大的h 因為當c固定時,這個式子只需要看前面 h*h-h*c=h*(h-c) h大於c時,肯定h越大越好 h小於c時,表示式的值必定小於零,肯定是不考慮的 #include <cs