ZOJ 1516 Uncle Tom's Inherited Land (二分图匹配)

2023-11-21 21:18

本文主要是介绍ZOJ 1516 Uncle Tom's Inherited Land (二分图匹配),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1516

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.


<b< dd="">

Output


For each test case in the input your program should produce one line of output, containing an integer value representing the maximum number of properties which can be sold.


<b< dd="">

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


<b< dd="">

Sample Output

4
3


   题意:n*m的土地上,求有多少1*2 或者 2*1 的块数 ,池塘不能算在内.

   思路:每一个不是池塘的土地,都可以跟他的上下左右四个方向上不是池塘的块构成一个1*2或者2*1的长方形块,把所有连通的块存起来,构成一个二部图,剩下的就是求最大匹配.

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 110
int g[MAXN][MAXN];
int mp[MAXN][MAXN];
int node[MAXN][MAXN];
int cx[MAXN],cy[MAXN];
int sx[MAXN],sy[MAXN];
int vis[MAXN];
int n,m,num,ans;
int dir[4][2]= {0,1,1,0,0,-1,-1,0};
void bulid()
{int i,j,k;num=1;for(i=1; i<=n; i++)for(j=1; j<=m; j++){if(!mp[i][j])node[i][j]=num++;}for(i=1; i<=n; i++)for(j=1; j<=m; j++){if(!mp[i][j]){for(k=0; k<4; k++){int tx=i+dir[k][0];int ty=j+dir[k][1];if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&mp[tx][ty]==0){g[node[i][j]][node[tx][ty]]=1;g[node[tx][ty]][node[i][j]]=1;}}}}
}
int path(int u)
{sx[u]=1;int v;for(v=1; v<=num; v++){if(g[u][v]&&!sy[v]){sy[v]=1;if(!cy[v]||path(cy[v])){cx[u]=v;cy[v]=u;return 1;}}}return 0;
}
int MaxMatch()
{int i;ans=0;memset(cx,0,sizeof(cx));memset(cy,0,sizeof(cy));for(i=1; i<=num; i++){if(!cx[i]){memset(sx,0,sizeof(sx));memset(sy,0,sizeof(sy));ans+=path(i);}}return 0;
}
int main()
{int i,j,k,u,v;while(~scanf("%d%d",&n,&m)&&n+m){memset(mp,0,sizeof(mp));memset(g,0,sizeof(g));memset(node,0,sizeof(node));scanf("%d",&k);for(i=0; i<k; i++){scanf("%d%d",&u,&v);mp[u][v]=1;}bulid();MaxMatch();printf("%d\n",ans/2);}return 0;
}

这篇关于ZOJ 1516 Uncle Tom's Inherited Land (二分图匹配)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/405157

相关文章

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

关于Gateway路由匹配规则解读

《关于Gateway路由匹配规则解读》本文详细介绍了SpringCloudGateway的路由匹配规则,包括基本概念、常用属性、实际应用以及注意事项,路由匹配规则决定了请求如何被转发到目标服务,是Ga... 目录Gateway路由匹配规则一、基本概念二、常用属性三、实际应用四、注意事项总结Gateway路由

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que