hdu 1507(二分图匹配)

时间:2022-11-30 08:55:37

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3232    Accepted Submission(s): 1354
Special Judge

Problem Description
Your
old uncle Tom inherited a piece of land from his great-great-uncle.
Originally, the property had been in the shape of a rectangle. A long
time ago, however, his great-great-uncle decided to divide the land into
a grid of small squares. He turned some of the squares into ponds, for
he loved to hunt ducks and wanted to attract them to his property. (You
cannot be sure, for you have not been to the place, but he may have made
so many ponds that the land may now consist of several disconnected
islands.)

Your uncle Tom wants to sell the inherited land, but
local rules now regulate property sales. Your uncle has been informed
that, at his great-great-uncle's request, a law has been passed which
establishes that property can only be sold in rectangular lots the size
of two squares of your uncle's property. Furthermore, ponds are not
salable property.

Your uncle asked your help to determine the
largest number of properties he could sell (the remaining squares will
become recreational parks).
hdu 1507(二分图匹配)

 
Input
Input
will include several test cases. The first line of a test case contains
two integers N and M, representing, respectively, the number of rows
and columns of the land (1 <= N, M <= 100). The second line will
contain an integer K indicating the number of squares that have been
turned into ponds ( (N x M) - K <= 50). Each of the next K lines
contains two integers X and Y describing the position of a square which
was turned into a pond (1 <= X <= N and 1 <= Y <= M). The
end of input is indicated by N = M = 0.
 
Output
For
each test case in the input your program should first output one line,
containing an integer p representing the maximum number of properties
which can be sold. The next p lines specify each pair of squares which
can be sold simultaneity. If there are more than one solution, anyone is
acceptable. there is a blank line after each test case. See sample
below for clarification of the output format.
 
Sample Input
4 4
6
1 1
1 4
2 2
4 1
4 2
4 4
4 3
4
4 2
3 2
2 2
3 1
0 0
 
Sample Output
4
(1,2)--(1,3)
(2,1)--(3,1)
(2,3)--(3,3)
(2,4)--(3,4)

3
(1,1)--(2,1)
(1,2)--(1,3)
(2,3)--(3,3)

 
Source
 
题意:在一个矩阵里面找到最多的1*2的矩形填这个矩阵的白色区域。
题解:这个题可以利用棋盘染色法将其化成二分图,因为二分图的X一个点只能与Y的一个点进行匹配,所以这样的话就避免了重复计数。然后根据linker数组找到相邻端点,取模输出就好,输出时要讨论 y 是否为 m ,不然的话会取模变成 0。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
struct Edge{
int v,next;
}edge[];
int head[N],tot;
int n,m;
int mp[][];
int linker[N];
bool vis[N];
int dir[][] = {{,},{-,},{,},{,-}};
void addEdge(int u,int v,int &k){
edge[k].v = v,edge[k].next = head[u],head[u] = k++;
}
void init(){
memset(head,-,sizeof(head));
tot = ;
}
int P(int x,int y){
return (x-)*m+y;
}
bool check(int x,int y){
if(x<||x>n||y<||y>m||mp[x][y]) return false;
return true;
}
bool dfs(int u){
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
if(!vis[v]){
vis[v] = true;
if(linker[v]==-||dfs(linker[v])){
linker[v] = u;
return true;
}
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF,n+m){
init();
memset(mp,,sizeof(mp));
int a,b,k;
scanf("%d",&k);
while(k--){
scanf("%d%d",&a,&b);
mp[a][b] = ;
}
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(!mp[i][j]&&(i+j)%){
for(int k=;k<;k++){
int x = i + dir[k][];
int y = j + dir[k][];
if(check(x,y)){
addEdge(P(i,j),P(x,y),tot);
}
}
}
}
}
memset(linker,-,sizeof(linker));
int res = ;
for(int i=;i<=n*m;i++){
memset(vis,false,sizeof(vis));
if(dfs(i)) res++;
}
printf("%d\n",res);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
int x1,y1,x2,y2;
int now = P(i,j);
if(linker[now]!=-){
if(linker[now]%m==){
x1 = linker[now]/m;
y1 = m;
}else{
x1 = linker[now]/m+;
y1 = linker[now]%m;
}
if(now%m==){
x2 = now/m;
y2 = m;
}else{
x2 = now/m+;
y2 = now%m;
}
printf("(%d,%d)--(%d,%d)\n",x1,y1,x2,y2);
}
}
}
printf("\n");
}
return ;
}

hdu 1507(二分图匹配)的更多相关文章

  1. hdu 2063 二分图匹配

    题意:一些女的和一些男的有好感,有好感的能一起坐过山车,问最多能组成多少对 hdu 11 页上少有的算法题,二分图匹配问题,匈牙利算法,对于每一个汉子,看和他有好感的妹子有没有配对了,没有配对过就可以 ...

  2. hdu 1281 二分图匹配

    题目:在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下.但是某些格子若不放子,就 无法保证放尽量多的“车”,这样的格子被称做重要点 ...

  3. hdu 4185 二分图匹配

    题意用1*2的木板覆盖矩阵中的‘#’,(木板要覆盖的只能是‘#’),问最多能用几个木板覆盖 将#抽象为二分图的点,一个木板就是一个匹配,注意最后结果要除以2 Sample Input 1 6 .... ...

  4. 过山车 HDU 2063 &lpar;二分图匹配裸题&rpar;

    Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生 ...

  5. Land of Farms HDU - 5556 二分图匹配

    Farmer John and his brothers have found a new land. They are so excited and decide to build new farm ...

  6. Fire Net HDU - 1045 &lpar;二分图匹配&rpar;

    题意: 给出一张图,图中'X'表示wall,'.'表示空地,可以放置blockhouse同一条直线上只能有一个blockhouse,除非有wall 隔开,问在给出的图中最多能放置多少个blockhou ...

  7. Girls and Boys HDU - 1068 二分图匹配(匈牙利)&plus;最大独立集证明

    最大独立集证明参考:https://blog.csdn.net/qq_34564984/article/details/52778763 最大独立集证明: 上图,我们用两个红色的点覆盖了所有边.我们证 ...

  8. HDU 1507 Uncle Tom&&num;39&semi;s Inherited Land&ast;&lpar;二分图匹配&rpar;

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

  9. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

随机推荐

  1. ASP&period;NET MVC系列:Area

    1. Area简介 ASP.NET MVC Area机制构建项目,可以将相对独立的功能模块切割划分,降低项目的耦合度. 2. Area设置Routing 新建Admin Area后,自动创建Admin ...

  2. crontab不执行perl脚本分析

    在新装的Linux服务器上部署了一个作业监控磁盘空间并提前告警,在shell脚本里面调用了一个perl脚本发送告警邮件.结果出现了一个很奇怪的现象:如果手工执行该脚本/home/oracle/scri ...

  3. input固定在底部

    // input固定在底部//isFocusing获取焦点:true 失去焦点:false _onTouchInput(isFocusing){ this.phone_width = screen.w ...

  4. 深入理解CSS定位中的偏移

    × 目录 [1]定位 [2]包含块 [3]偏移属性[4]绝对定位[5]格式化 [6]auto 前面的话 CSS有三种基本的定位机制:普通流.浮动和绝对定位.利用定位,可以准确地定义元素框相对于其正常位 ...

  5. 什么时候用copy什么时候用retain &lpar;一&rpar;

    在声明一个property的时候总是搞不清什么时候用retain,什么时候用copy,用上去了感觉也不会错,但是又没有安全感: Copy:顾名思义,复制,将对象复制一份,ios内部的操作时,先copy ...

  6. 整理收藏一份PHP高级工程师的笔试题

    整理了一份PHP高级工程师的笔试题,问题很全面.嗯,基本上这些题都答得不错,那么你应该可以胜任大部分互联网企业的PHP职位了.下面直接上题. 1. 基本知识点 HTTP协议中几个状态码的含义:503, ...

  7. Info&period;plist与Prefix&period;pch修改文件位置遇到的问题及解决方法

    如果要更改Info.plist与Prefix.pch文件实际路径,也就是实际文件的位置(不是在工程中的组织路径),需要到Build Settings中修改对应的配置,不然工程就找不到对应的Info.p ...

  8. &lpar;转&rpar;没有IE就没有伤害!浏览器兼容性问题解决方案汇总

    普及:浏览器的兼容性问题,往往是个别浏览器(没错,就是那个与众不同的浏览器)对于一些标准的定义不一致导致的.俗话说:没有IE就没有伤害. 贴士:内容都是自己总结的,不免会出现错误或者bug,欢迎更正和 ...

  9. oracle 之 如何链接别人电脑的oracle

    1.首先确保两台电脑是在同一个局域网内,可以通过cm命令窗口 ping 对方电脑的ID,若是没问题则表示可以连接 2.接下来通过配置来首先连接对方的电脑 其实在后面还有一个是否创建新的额服务名的操作, ...

  10. 关闭TCP中135、139、445、593、1025 等端口的操作方法 &lpar;转&rpar;(记录下)

    操作要领:封闭端口,杜绝网络病毒对这些端口的访问权,以保障计算机安全,减少病毒对上网速度的影响. 近日发现有些人感染了新的网络蠕虫病毒,该病毒使用冲击波病毒专杀工具无法杀除,请各位尽快升级计算机上的杀 ...