zoj 3209 Treasure Map dancing links
来源:互联网 发布:c语言 void 编辑:程序博客网 时间:2024/06/06 08:55
题意:在给定的矩形中,选取最少的矩形数量,使之给定的能覆盖(n*m)矩形
思路:我们把题目给的q个矩形,对与每一个矩形,划分成1*1的小矩形,面积为a*b的矩形就能划分成a*b个(1*1)的小矩形,(n*m)矩形看成所有的列,每一个给定的矩形都看成行,那么就是一个精确覆盖问题。直接上白书模板,还有些小细节,看代码
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3372
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 30 * 30;const int maxnode = 500010;const int maxr = 505;struct DLX{int n, sz;int s[maxn];int row[maxnode], col[maxnode];int l[maxnode], r[maxnode], d[maxnode], u[maxnode];int ansd, ans[maxr];void init(int n){this->n = n;for (int i = 0; i <= n; i++){u[i] = i, d[i] = i, l[i] = i - 1, r[i] = i + 1;}r[n] = 0, l[0] = n;sz = n + 1;ansd = -1;memset(s, 0, sizeof(s));}void addrow(int R, vector<int> column){int first = sz;for (int i = 0; i < (int)column.size(); i++){int c = column[i];l[sz] = sz - 1, r[sz] = sz + 1, d[sz] = c, u[sz] = u[c];d[u[c]] = sz, u[c] = sz;row[sz] = R, col[sz] = c;sz++, s[c]++;}r[sz - 1] = first, l[first] = sz - 1;}void Remove(int c){l[r[c]] = l[c];r[l[c]] = r[c];for (int i = d[c]; i != c; i = d[i]){for (int j = l[i]; j != i; j = l[j]){u[d[j]] = u[j];d[u[j]] = d[j];--s[col[j]];}}}void Restore(int c){for (int i = u[c]; i != c; i = u[i]){for (int j = r[i]; j != i; j = r[j]){++s[col[j]];d[u[j]] = j;u[d[j]] = j;}}l[r[c]] = c;r[l[c]] = c;}void dfs(int dep){ if(ansd!=-1 && ansd<dep) return ;if (r[0] == 0){ if(ansd == -1) ansd=dep;else if(ansd > dep) ansd=dep;//这里不能只搜一次,因为要找到最少的return ;}int c = r[0];for (int i = r[0]; i != 0; i = r[i]){if (s[i] < s[c]) c = i;}Remove(c);for (int i = d[c]; i != c; i = d[i]){ans[dep] = row[i];for (int j = r[i]; j != i; j = r[j])Remove(col[j]); dfs(dep+1);for (int j = l[i]; j != i; j = l[j])Restore(col[j]);}Restore(c);}void solve(){ dfs(0);printf("%d\n",ansd);}};DLX dlx;int main(){int t, n, m, p;scanf("%d", &t);while (t--){scanf("%d%d%d", &n, &m, &p);int x1, y1, x2, y2;dlx.init(n*m);vector<int> v;for (int k = 1; k <= p; k++){scanf("%d%d%d%d", &x1, &y1, &x2, &y2);v.clear();for (int i = x1+1; i <= x2; i++)//这里统计的是每个矩形的格子个数,所以起始位置要+1,{for (int j = y1+1; j <= y2; j++)v.push_back((i-1)*m + j);//这里是(i-1),有些代码可能是i,这就跟编码方式有关,}dlx.addrow(k,v);}dlx.solve();}return 0;}
0 0
- zoj 3209 Treasure Map (dancing links)
- ZOJ 3209 Treasure Map(Dancing Links)
- zoj 3209 Treasure Map dancing links
- ZOJ 3209 Treasure Map (Dancing Links)
- zoj 3209 Treasure Map 最小覆盖问题 dancing links
- ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
- [ACM] ZOJ 3209 Treasure Map ( Dancing Links 精确覆盖,矩形覆盖)
- 舞蹈链(Dancing Links) 解决精确覆盖问题 hustoj 1017 Exact cover zoj 3209 Treasure Map
- ZOJ 3209 Treasure Map(Dancing Links)(精确覆盖问题)
- zoj-3209 Treasure Map【Dancing link x】
- ZOJ3209 Treasure Map(Dancing Links)
- ZOJ 3209 Dancing Links
- ZOJ 3209 Treasure Map 解题报告(Dancing Link)
- zoj 3209(Dancing Links应用)
- ZOJ - 3209 Treasure Map
- ZOJ 3209 Treasure Map
- ZOJ 3209 Treasure Map
- zoj 3209 Treasure Map
- 【VLC核心一】播放流程梳理->live555收流+ffmpeg:AVCodec解码
- C++第三次实验——项目1-静态成员应用
- Sublime3 Terminal UBuntu install
- Linux学习笔记11
- JspSmartUpload中文乱码问题解决
- zoj 3209 Treasure Map dancing links
- Android-->原生保存Bitmap(图片),压缩图片,创建图片缩略图和视频缩略图(任意大小)
- iOS ShareSDK实现分享——微信分享
- Python 练习实例8
- static变量与static函数
- freemarker之生成静态页面
- 房屋建筑和市政基础设施工程竣工验收备案管理办法
- android 打 release包解决字节对其问题
- 转载:css隐藏元素的几种方案