原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html


题目传送门 - HDU1507


题意概括

  有一个n*m的棋盘,有些点是废的。

  现在让你用1*2的矩形覆盖所有的不废的点,并且不重叠,问最多可以覆盖多少个1*2的矩形,输出方案,有SPJ。

  输入描述:

  多组数据,每组首先两个数n,m(如果n和m为0,则结束程序)

  然后给出k

  然后给出k个二元组(x,y)表示废点的坐标。


题解

  按照前两片博文的算法已经不行了,因为方案不对了。

  所以我们要进行黑白染色。

  仅从(x,y)(x+y为奇数)向四连通方向连边,然后二分图匹配即可。

  其中match数组的值就是匹配对象。


代码

#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstdio>
using namespace std;
const int N=105,K=55;
int dx[4]={ 0, 0,-1, 1};
int dy[4]={-1, 1, 0, 0};
int n,m,k,cnt,pl[N][N],tn[N][N],bj[K],x[K],y[K];
int g[K][K],vis[K],match[K];
bool check(int x,int y){
return 1<=x&&x<=n&&1<=y&&y<=m&&tn[x][y];
}
bool Match(int x){
for (int i=1;i<=cnt;i++)
if (!vis[i]&&g[x][i]){
vis[i]=1;
if (!match[i]||Match(match[i])){
match[i]=x;
return 1;
}
}
return 0;
}
int hungary(){
int res=0;
memset(match,0,sizeof match);
for (int i=1;i<=cnt;i++){
if (!bj[i])
continue;
memset(vis,0,sizeof vis);
if (Match(i))
res++;
}
return res;
}
int main(){
while (~scanf("%d%d",&n,&m)&&(n||m)){
scanf("%d",&k);
memset(pl,0,sizeof pl);
for (int i=1,x,y;i<=k;i++)
scanf("%d%d",&x,&y),pl[x][y]=1;
memset(tn,0,sizeof tn);
cnt=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (!pl[i][j]){
bj[tn[i][j]=++cnt]=(i+j)&1;
x[cnt]=i,y[cnt]=j;
}
memset(g,0,sizeof g);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if ((i+j)&1)
for (int d=0;d<4;d++){
int a=i+dx[d],b=j+dy[d];
if (check(a,b))
g[tn[i][j]][tn[a][b]]=1;
}
int ans=hungary();
printf("%d\n",ans);
for (int i=1;i<=cnt;i++)
if (match[i])
printf("(%d,%d)--(%d,%d)\n",x[i],y[i],x[match[i]],y[match[i]]);
}
return 0;
}

  

HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色的更多相关文章

  1. HDU 1507 Uncle Tom's Inherited Land*(二分图匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  2. hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  3. hdu1507 Uncle Tom's Inherited Land* 二分匹配

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1507 将i+j为奇数的构成x集合中 将i+j为偶数的构成y集合中 然后就是构建二部图 关键就是构图 然 ...

  4. HDU1507 Uncle Tom's Inherited Land*

    题目是跟 zoj1516是一样的,但多了匹配后的输出 详解zoj1516可见http://www.cnblogs.com/CSU3901130321/p/4228057.html #include & ...

  5. HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. XTU 二分图和网络流 练习题 B. Uncle Tom's Inherited Land*

    B. Uncle Tom's Inherited Land* Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I ...

  7. Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. Uncle Tom's Inherited Land*

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...

  9. HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

    Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. 【原创】大数据基础之Flume(2)kudu sink

    kudu中的flume sink代码路径: https://github.com/apache/kudu/tree/master/java/kudu-flume-sink kudu-flume-sin ...

  2. 配置nginx php上传大文件

    配置nginx php上传大文件: 1. 修改PHP配置文件中的三项:vim /usr/local/php/etc/php.ini 1.file_uploads 设为On,允许通过HTTP上传文件 2 ...

  3. 经典JS闭包面试题(来理解闭包)(转)

    转载地址:http://www.cnblogs.com/xxcanghai/p/4991870.html 先看代码: function fun(n,o) { console.log(o) return ...

  4. LuoGu P2420 让我们异或吧

    其实......这就是个SB题,本来看到这个题,和树上路径有关 于是--我就欣喜地打了一个树剖上去,结果嘞,异或两遍等于没异或 所以这题和LCA屁关系都没有,所以这题就是个树上DFS!!!! 所以它为 ...

  5. Oauth2.0 QQ&微信&微博实现第三方登陆

    一.写在前面 目前对于大多数的App或Web网站都支持有第三方登陆这个功能,用户可使用 QQ/ 微信/ 微博 帐号快速登录你的网站,降低注册门槛,为你的网站带来海量新用户.最近在新项目上刚好用到了,在 ...

  6. Confluence 6 手动备份站点

    Confluence 被配置自动备份数据,使用压缩的 XML 格式.同时你也可以通过 Confluence 的 管理员控制台(Administration Console)手动进行备份. 你需要具有 ...

  7. 使用应用链接来连接 Jira 和 Confluence 6

    请参考 Linking to Another Application 页面中的内容来设置如何让 Confluence 连接到你的 Jira 应用,这个过程只需要一次就可以了. 如果你计划使用 Jira ...

  8. Confluence 6 编辑自定义 Decorators

    希望对 Confluence 的 decorator 进行编辑的话,你需要具有良好的 HTML 知识和能够理解  Velocity 模板语言. 希望编辑 decorator 文件: 进入  Confl ...

  9. STL的基本操作指令

    list :Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. assign() 给list赋值 back() 返回最后一个元素 b ...

  10. Ubuntu 安装google 拼音

    一.安装fcitx apt-get install fcitx 二.安装google pinyin sudo apt install fcitx-googlepinyin 三. 安装 fcitx-co ...