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

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

 

Uncle Tom's Inherited Land*

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

Total Submission(s): 2055    Accepted Submission(s): 850
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)
 
 

这条是HDU比较老的,也比较经典的题目.

题目意思就是要求一些 土地能够与相邻的土地匹配,然后求一个最大匹配数 .

关键就在于把一个矩阵分为 x ,y 两个集合 。 然后一个点与相邻4个点所在集合不同,

其实就像国际象棋棋盘一样分为黑白,这样就就不会出现重边了。

构图的时候用数字表示一下点,能通过点查出数字,然后也能够通过数字找回点。

最后求一次匈牙利就行了。

 

 

#include 
#include
#include
#include
using namespace std;typedef long long LL;const int N = 210;int lx[N];bool vis[N];int n,d,m;int g[N][N] , mp[N][N] , id[N][N] ,num[N][N];int ID , tot ;struct node{ int x,y;}ans[55];bool dfs( int u ){ for(int v =1 ;v <= ID ;++v ){ if(!vis[v] && g[u][v]){ vis[v] = 1; if( lx[v] == -1 || dfs(lx[v]) ){ lx[v] = u ; return true; } } } return false;}int solve(){ int res=0 ; memset( lx , -1 ,sizeof lx ); for( int i =1 ; i<= ID ;++i){ memset( vis , false , sizeof vis ); if( dfs( i ) ) res ++; } return res;}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif ios::sync_with_stdio(0); int cas = 1 ,x , y ; for(int i =1 ; i <= 100 ; ++i ){ for(int j = 1; j <=100 ;++j ){ if( num[i][j] ) { num[i-1][j] = num[i+1][j] = num[i][j-1] = num[i][j+1] = 0; } else { num[i-1][j] = num[i+1][j] = num[i][j-1] = num[i][j+1] = 1; } } } while(~scanf("%d%d",&n,&m)){ if(!n)break; memset( g, 0 , sizeof g ); memset( mp , 0 , sizeof mp ); ID = 0 ; scanf("%d",&d); while(d--){ scanf("%d%d",&x,&y); mp[x][y] = 1; } for(int i = 1; i <= n ; ++i){ for(int j =1 ; j<= m ;++j ){ if( mp[i][j] )continue; id[i][j] = ++ID; ans[ID].x = i , ans[ID].y = j; } } for(int i =1 ; i<= n ;++i ){ for(int j =1 ; j <= m ;++j){ if( !mp[i][j] && num[i][j] ){ if( i > 1 && !mp[i-1][j] ) g[id[i][j]][id[i-1][j]] =1 ; if( i < n && !mp[i+1][j] ) g[id[i][j]][id[i+1][j]] =1 ; if( j > 1 && !mp[i][j-1] ) g[id[i][j]][id[i][j-1]] =1 ; if( j < m && !mp[i][j+1] ) g[id[i][j]][id[i][j+1]] =1 ; } } } printf("%d\n",solve()); for(int i =1 ; i <= ID ; ++i ){ if(lx[i] == -1 )continue; printf("(%d,%d)--(%d,%d)\n",ans[i].x,ans[i].y,ans[lx[i]].x, ans[lx[i]].y); } } return 0;}

 

转载于:https://www.cnblogs.com/hlmark/p/3976207.html

你可能感兴趣的文章
2014上半年大片早知道
查看>>
Android 6.0指纹识别App开发案例
查看>>
正文提取算法
查看>>
轻松学PHP
查看>>
Linux中的网络监控命令
查看>>
this的用法
查看>>
windows下安装redis
查看>>
CentOS7 yum 安装git
查看>>
启动日志中频繁出现以下信息
查看>>
httpd – 对Apache的DFOREGROUND感到困惑
查看>>
分布式锁的一点理解
查看>>
idea的maven项目,install下载重复下载本地库中已有的jar包,而且下载后jar包都是lastupdated问题...
查看>>
2019测试指南-web应用程序安全测试(二)指纹Web服务器
查看>>
树莓派3链接wifi
查看>>
js面向对象编程
查看>>
Ruby中类 模块 单例方法 总结
查看>>
jQuery的validate插件
查看>>
5-4 8 管道符 作业控制 shell变量 环境变量配置
查看>>
Enumberable
查看>>
开发者论坛一周精粹(第五十四期) 求购备案服务号1枚!
查看>>