Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2114    Accepted Submission(s): 867

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
 
Recommend
LL   |   We have carefully selected several similar problems for you:  1281 1528 1498 1533 1532 
 

二分匹配。建边的根据是枚举全部可行的点,然后枚举周围四个方向。输出方案根据mark数组

#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn = 10010;
struct node
{
int to;
int next;
}edge[4 * maxn]; int vis[maxn];
int head[maxn];
int mark[maxn];
bool used[maxn];
bool mat[110][110];
int cnt[110][110];
int tot;
int n, m;
int index; void addedge(int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
} bool dfs(int x)
{
for (int i = head[x]; i != -1; i = edge[i].next)
{
if (!used[edge[i].to])
{
used[edge[i].to] = 1;
if (mark[edge[i].to] == -1 || dfs(mark[edge[i].to]))
{
mark[edge[i].to] = x;
return true;
}
}
}
return false;
} int hungary()
{
memset(mark, -1, sizeof(mark));
int ans = 0;
for (int i = 0; i < index; i++)
{
memset(used, 0, sizeof(used));
if (dfs(i))
ans++;
}
return ans;
} int main()
{
int k;
while (~scanf("%d%d", &n, &m))
{
if (!n && !m)
{
break;
}
scanf("%d", &k);
map<int, int>qu;
qu.clear();
memset(head, -1, sizeof(head));
memset(mat, false, sizeof(mat));
memset(vis, -1, sizeof(vis));
tot=0;
int x, y;
for (int i = 0; i < k; i++)
{
scanf("%d%d", &x, &y);
x--;
y--;
mat[x][y] = true;
}
index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (!mat[i][j])
{
cnt[i][j] = index++;
qu[index - 1] = i * m + j;
} for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
if (!mat[i][j])
{
if (i > 0 && !mat[i - 1][j])
addedge(cnt[i][j], cnt[i - 1][j]);
if (i < n-1 && !mat[i + 1][j])
addedge(cnt[i][j], cnt[i + 1][j]);
if (j > 0 && !mat[i][j - 1])
addedge(cnt[i][j], cnt[i][j - 1]);
if (j < m - 1 && !mat[i][j + 1])
addedge(cnt[i][j], cnt[i][j + 1]);
}
}
int res = hungary();
printf("%d\n", res / 2);
for (int i = 0; i < index; ++i)
{
if (mark[i] != -1)
{
int a = qu[i];
int b = qu[mark[i]];
int x1 = a / m + 1;
int y1 = a % m + 1;
int x2 = b / m + 1;
int y2 = b % m + 1;
if (vis[a] == -1 && vis[b] == -1)
{
printf("(%d,%d)--(%d,%d)\n", x1, y1, x2, y2);
vis[a] = b;
vis[b] = a;
}
}
}
printf("\n");
}
return 0;
}

hdu1507——Uncle Tom&#39;s Inherited Land*的更多相关文章

  1. HDOJ 1507 Uncle Tom&#39;s Inherited Land*

    直接对每一个格子进行dfs结果除以2能够得到答案可是有大量反复的结果,不好输出答案. 能够仅仅对横纵坐标相加是奇数的格子dfs.... Uncle Tom's Inherited Land* Time ...

  2. ZOJ 1516 Uncle Tom&#39;s Inherited Land(二分匹配 最大匹配 匈牙利啊)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=516 Your old uncle Tom inherited a p ...

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

  4. HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色

    原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...

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

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

  6. HDU1507 Uncle Tom's Inherited Land*

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

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

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

  9. Uncle Tom's Inherited Land*

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

随机推荐

  1. 条款4:确定对象被使用前已被初始化(Make sure that objects are initialized before they're used)

    其实 无论学何种语言 ,还是觉得要养成先声明后使用,先初始化再使用. 1.永远在使用对象之前先将其初始化. 内置类型: 必须手工完成. 内置类型以外的:使用构造函数完成.确保每一个构造函数都将对象的一 ...

  2. windows 上使用virtualenv进行python多版本转换

    近期因为需要在python2.7和Python3.6上进行工作学习,可是笔记本只配置了python3.6环境. 所以打算使用virtualenv这个强大的工具进行多版本转换: 一.首先,默认已经配置好 ...

  3. python书籍推荐:量化投资:以Python为工具

    所属网站分类: 资源下载 > python电子书 作者:mimi 链接:http://www.pythonheidong.com/blog/article/451/ 来源:python黑洞网 内 ...

  4. jar包、war包、ear包傻傻分不清?

    在工作中,需要在jboss上deploy一个health check的war包,因此了解一下: Jar文件(扩展名为. Jar,Java Application Archive)包含Java类的普通库 ...

  5. Triangular Pastures (二维01背包)

    描述Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectang ...

  6. 服务器架构及实战(架构篇)- PHP建站

    概述 详解 PHP的环境搭建 PHP的帮助使用和配置文件 PHP的Hello World PHP的库函数调用 PHP的Web程序 PHP的函数和面向对象使用 PHP的数据库访问 Nginx安装和配置访 ...

  7. Web性能压力测试工具——Siege

    最近需要对Istio里的bookinfo进行分析,老是手去刷新太慢了,就找了个自动点的工具——Siege Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一 ...

  8. Linux vi替换字符串

    1. 基本的替换 :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n,$s/vivian/sky/ ...

  9. Django 的信号 & Flask 的信号

    信号:框架内部已帮助开发者预留的可扩展的位置 一.Django 的信号 项目目录结构: django_signal |--- app01 |--- models.py |--- views.py .. ...

  10. 【Tomcat】如何优化tomcat配置(从内存、并发、缓存4个方面)优化

    一.Tomcat内存优化 ** Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 java_OPTS 参数. JAVA_O ...