博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1507 Uncle Tom's Inherited Land*
阅读量:7201 次
发布时间:2019-06-29

本文共 4096 字,大约阅读时间需要 13 分钟。

Uncle Tom's Inherited Land*

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 7
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
South America 2002 - Practice
 
 
 
 
题意:
    给定一个n*m的矩阵,去除里面的黑色小方格,两个相邻的白色小方格为一组,问一共能找多少组。
很明显的最大匹配,遍历矩阵的每一个可行点,把它和上下左右的可行点连起来,然后就得到了一个二分图,但这题纠结的地方是如果直接这样建图,每对节点之间就有两条边了,比如(1,2)--(1,3)和(1,3)--(1,2),这两点之间就有两条边,因为这地方卡了我一上午······其实仔细想一下就能发现只要跳过相邻的节点建图就可以了,而相邻节点之间的下标和存在奇偶关系,于是根据下标和的奇偶性建图。
 
 
#include
#include
#include
using namespace std;int n,m,k,map[110][110],vis[110][110];struct node{ int x,y;}linker[110][110];int dir[4][2]={
{
1,0},{-1,0},{
0,1},{
0,-1}};int DFS(int x,int y){ for(int i=0;i<4;i++){ int sx=x+dir[i][0]; int sy=y+dir[i][1]; if(sx>=1 && sx<=n && sy>=1 && sy<=m && !vis[sx][sy] && !map[sx][sy]){ vis[sx][sy]=1; if(linker[sx][sy].x==-1 || DFS(linker[sx][sy].x,linker[sx][sy].y)){ linker[sx][sy].x=x; linker[sx][sy].y=y; return 1; } } } return 0;}int Hungary(){ int ans=0; memset(linker,-1,sizeof(linker)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(!map[i][j] && (i+j)&1){ //根据下标和的奇偶性建图 memset(vis,0,sizeof(vis)); if(DFS(i,j)) ans++; } return ans;}int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&m)){ if(n==0 && m==0) break; memset(map,0,sizeof(map)); scanf("%d",&k); int u,v; while(k--){ scanf("%d%d",&u,&v); map[u][v]=1; } int ans=Hungary(); printf("%d\n",ans); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(linker[i][j].x!=-1) printf("(%d,%d)--(%d,%d)\n",linker[i][j].x,linker[i][j].y,i,j); printf("\n"); } return 0;}

 

 

转载地址:http://dbzum.baihongyu.com/

你可能感兴趣的文章
使用Symantec Backup Exec 对Exchange 2010 进行备份还原和灾难恢复系列之一
查看>>
xhprof php性能测试
查看>>
基于MVC+EasyUI的Web开发框架经验总结(10)--在Web界面上实现数据的导入和导出...
查看>>
Mysql数据库视图操作
查看>>
[培训]薛大龙@福建福州(2008.7.24-25)
查看>>
Linux运维学习历程-第十四天-磁盘管理(一)磁盘分区表类型与文件系统
查看>>
2016年4月20日作业
查看>>
4、Ansible配置和使用
查看>>
如何设计EDM邮件内容
查看>>
java_类型转换(转)
查看>>
EMC 存储 故障转移模式(Failover Mode)简介
查看>>
解决iis服务器 Server Application Error
查看>>
wget
查看>>
openjdk安装二
查看>>
信息、信息化与信息化营销
查看>>
https的网站用了百度分享后网站在浏览器中不安全解决方法
查看>>
我的友情链接
查看>>
Oracle shutdown immediate无法关闭数据库解决方法
查看>>
MySQL5.7杀手级新特性:GTID原理与实战
查看>>
iOS 分类思想(1)
查看>>