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).

 
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 (二分图匹配裸题)

    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 (二分图匹配)

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

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

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

  8. 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 ...

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

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

随机推荐

  1. json.dumps错误:'utf8' codec can't decode byte解决方案

    一次在使用json.dumps()过程中,出现错误提示: ERROR:"UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in ...

  2. 个人作业Week3-案例分析(201521123103 吴雅娟)

    根据博客要求,写一篇个人随笔 参考来自: http://www.cnblogs.com/xinz/archive/2012/03/26/2417699.html: http://www.cnblogs ...

  3. 有关于PHP的基础知识

    (1) l  长字符串表示,必须放在“<<<heredoc”和 “heredoc;”之间.主要是<<<,其次是也可以不使用heredoc. l  “<< ...

  4. 在fslook

    fslook让我们从内核看文件系统而不是从用户态,从这个工具中发现了很多之前忽略过的点. 1)overlay从内核中看到的文件的ino为什么和用户态stat中看到的ino不是一样的?

  5. 在 Linux 安装 JDK 和 tomcat(菜鸡级别)

    安装JDK 卸载 OPENJDK rpm -qa|grep jdk  // 查看当前的jdk情况 yum -y remove java java-1.7.0-openjdk* // 卸载openjdk ...

  6. 【bzoj3931】[CQOI2015]网络吞吐量 最短路+最大流

    题目描述 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器.为了使数据包最快的到达目的地,路由器需要选择最优的路径转发 ...

  7. BZOJ4544 椭圆上的整点(数论)

    https://www.cnblogs.com/Gloid/p/9538413.html 基本思路没有太大差别.得到2n=d(a2+3b2),其中d=gcd(n-x,n+x),n-x==a2& ...

  8. LeetCode--Factorial Trailing Zeroes(注意)

    Given an integer n, return the number of trailing zeroes in n!. 问题描述:给出一个正整数n,计算n!结构后面有几个0.要求:在多项式时间 ...

  9. BZOJ 1023: [SHOI2008]cactus仙人掌图 | 在仙人掌上跑DP

    题目: 求仙人掌直径 http://www.lydsy.com/JudgeOnline/problem.php?id=1023 题解: 首先给出仙人掌的定义:满足所有的边至多在一个环上的无向联通图 我 ...

  10. Mockito中@Mock与@InjectMock

    Mockito是java单元测试中,最常用的mck工具之一,提供了诸多打桩方法和注解.其中有两个比较常用的注解,@Mock和@InjectMock,名字和在代码中使用 的位置都很像,对于初学者,很容易 ...