hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)
Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2013 Accepted Submission(s): 830 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
这道题,怎么说呢,比较的抽象,就是将一个点作为一个匹配的对象来处理,比较容易想到最大匹配,至于这个点的坐标该如何来存放
可以开四维的数组,貌似这样会到10e8+的数据规模 达到250M的样子,所以放弃这个念头...转而用结构体来定义自己的变量
然后就是一个一如既往的匈牙利算法啦,但是在遍历所有的坐标时,虽然可以求出正确的匹配数,貌似在输出那个谁匹配谁的时候,又不好处理
比如 你在 (1,2)这个点的时候,假如你和(1,3)匹配了,那么在(1,3)那个点时,又会有(1,3)匹配(1,2)这样就不好处里了...
于是呼,你会发现这个地方的匹配有些奇特,那就是貌似他临近的点的坐标的和总和所在点呈现奇偶互异的状态,所以我们不妨这样搞,取这个点就不在取那个点
就这样跳跃着,取点,然后就可以了....这样的话,一切就都ok了....
代码:
1 #include<cstring>
2 #include<cstdio>
3 #include<cstdlib>
4 #define init(a) memset(a,0,sizeof(a))
5 const int maxn=105;
6 bool map[maxn][maxn];
7 bool vis[maxn][maxn];
8 struct node{
9 int x,y;
10 };
11 node mat[maxn][maxn];
12 int n,m;
13 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
14 int match(node aa){
15
16 node tem;
17 if(!map[aa.x][aa.y]){
18 for(int ll=0;ll<4;ll++) //扫描它的四周...
19 {
20 tem=(node){aa.x+dir[ll][0],aa.y+dir[ll][1]};
21 if(tem.y>0&&tem.x>0&&tem.x<=n&&tem.y<=m&&!map[tem.x][tem.y]&&!vis[tem.x][tem.y]){
22 vis[tem.x][tem.y]=1;
23 if(!mat[tem.x][tem.y].x||match(mat[tem.x][tem.y])){
24 mat[tem.x][tem.y]=aa;
25 return 1;
26 }
27 }
28 }
29 }
30 return 0;
31 }
32 int main()
33 {
34 int k,x,y;
35 //freopen("test.in","r",stdin);
36 while(scanf("%d%d",&n,&m)!=EOF,n+m){
37 scanf("%d",&k);
38 init(map);
39 while(k--){
40 scanf("%d%d",&x,&y);
41 map[x][y]=1; //代表一个池塘
42 }
43 init(mat);
44 int ans=0;
45 for(int i=1;i<=n;i++){
46 for(int j=1;j<=m;j++){
47 if(((i+j)&1)){ //不娶相邻的点...取奇数或者偶数都一样,,
48 init(vis);
49 ans+=match((node){i,j});
50 }
51 }
52 }
53 printf("%dn",ans);
54 for(int i=1;i<=n;i++){
55 for(int j=1;j<=m;j++){
56 if(mat[i][j].x){
57 printf("(%d,%d)--(%d,%d)n",i,j,mat[i][j].x,mat[i][j].y);
58 }
59 }
60 }
61 }
62 return 0;
63 }
- Asp.Net webconfig中使用configSections的用法
- AI能从根本上解决紧张的医患关系吗?专访美国国家工程院院士何志明:“机器人根本不能独立看病!”
- Python函数之一切皆对象
- Java缓存深入理解
- 使用group by rollup和group by cube后的辅助函数
- UML快速入门
- Js框架设计之DomReady
- PyTorch实现自由的数据读取
- Java线程池深入理解
- 分布式事务的总结与思考
- 单例模式深入理解
- Oracle 中运用rollup和cube实现汇总运算
- JS实现OO机制
- SqlHelper中SqlHelperParameterCache类的用法介绍
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- PHP设计模式之适配器模式(Adapter)原理与用法详解
- laravel5.5框架的上传图片功能实例分析【仅传到服务器端】
- Laravel5.1框架自带权限控制系统 ACL用法分析
- php使用filter_var函数判断邮箱,url,ip格式示例
- Python json格式化打印实现过程解析
- PHP使用DOM对XML解析处理操作示例
- OpenCV Python实现图像指定区域裁剪
- Laravel框架实现多个视图共享相同数据的方法详解
- python如何快速生成时间戳
- 从python读取sql的实例方法
- PHP+iframe模拟Ajax上传文件功能示例
- Centos7 Yum安装PHP7.2流程教程详解
- PHP session垃圾回收机制实例分析
- thinkphp5框架调用其它控制器方法 实现自定义跳转界面功能示例
- Python常用库Numpy进行矩阵运算详解