原文链接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. python学习第9-10天,函数。

    函数初识 为什么要使用函数? 函数最重要的目的是方便我们重复使用相同的一段程序. 将一些操作隶属于一个函数,以后你想实现相同的操作的时候,只用调用函数名就可以,而不需要重复敲所有的语句. 函数的定义与 ...

  2. JS 手机端多张图片上传

    代码如下 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="u ...

  3. 使用Node.js+Hexo+Github搭建个人博客(续)

    一.写在前面 在我的上一篇博客<使用Nodejs+Hexo+Github搭建个人博客>中,已经介绍了如何使用 Hexo 在 Github Pages 上搭建一个简单的个人博客.该篇博文将在 ...

  4. 开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐!

    作者:编辑部的故事   <  开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐!   > 开源巨献:年度最佳 JavaScript 和 CSS 开源库推荐! Tutoria ...

  5. selenium之实现多窗口切换到自己想要的窗口

    #coding=utf-8 from selenium import webdriver import time from selenium.webdriver.support import expe ...

  6. kafka 的安装部署

    Kafka 的简介: Kafka 是一款分布式消息发布和订阅系统,具有高性能.高吞吐量的特点而被广泛应用与大数据传输场景.它是由 LinkedIn 公司开发,使用 Scala 语言编写,之后成为 Ap ...

  7. SpringBoot图片上传(二)

    需求简介:做新增的时候,需要上传图片.(⊙o⊙)…这需求描述也太简单了吧,限制文件大小60*60 512kb ,第一次做,记录一下嗷,废话就不啰嗦了 上代码 代码: //html代码<div c ...

  8. 【python】mongo删除数据

    参考:https://stackoverflow.com/questions/23334743/setting-justone-limiter-for-pymongo-remove-throws-ty ...

  9. 【python】gevent协程例子

    说在前面:用协程还是多线程需要仔细考量.我在做实验时请求了100w个ip,分别用pool为1000的协程和64个线程来跑,结果是多线程的速度是协程的10倍以上. 一个简单的协程例子 #!/usr/bi ...

  10. ubuntu MySQL的安装

    https://i.cnblogs.com/EditPosts.aspx?opt=1 https://juejin.im/entry/5adb5deff265da0b9d77cb3b MySQL Co ...