HDU1507-二分图行列匹配
Uncle Tom's Inherited Land*
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3423 Accepted Submission(s): 1438
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).
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)
给你一个n*m的矩阵,其中有些格子不能填东西,然后问你在空白处最多能填多少个1*2的方块
题目思路:
首先我们想到1*2的方块就是相邻两个格子的匹配,所以我们可以考虑将相邻格子建边,做最大匹配,最后的答案就是我们要求的,然后我们考虑如何构造二分图,应为这是个二维坐标,所以我们为了方便可以把二维坐标转换成一维坐标,然后枚举所有格子,分成奇偶,行列相加为偶数的和行列相加为奇数的连边,因为两个相邻格子的奇偶不同,然后进行最大匹配,集合的大小就是n*m,最后最大匹配的答案就是我们要求的,对于输出边,我们可以从link数组当中记录的连接边的情况来输出,我们枚举i,
如果link[i]不等于-1,这时候我们就知道i和link[i]之间有一条边
AC代码:
#include<cstring>
#include<cstdio>
#include<vector>
using std::vector;
const int maxn = 2e4+100;
bool vis[maxn],mp[205][205];
int link[maxn];
int n,m,k;
vector<int>edge[maxn];
bool dfs(int u){
for(int i=0;i<edge[u].size();i++){
int v = edge[u][i];
if(!vis[v]){
vis[v]=true;
if(link[v]==-1||dfs(link[v])){
link[v]=u;
return true;
}
}
}
return false;
}
void graph(){
for(int i=1;i<=n*m;i++)edge[i].clear();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int x = (i-1)*m+j;
if(!mp[i][j]&&(i+j)%2){
if(j+1<=m&&!mp[i][j+1]){ //枚举偶数点的四个方向
edge[x].push_back(x+1);
}
if(i+1<=n&&!mp[i+1][j]){
edge[x].push_back(x+m);
}
if(j-1>=1&&!mp[i][j-1]){
edge[x].push_back(x-1);
}
if(i-1>=1&&!mp[i-1][j]){
edge[x].push_back(x-m);
}
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m),n+m){
scanf("%d",&k);
memset(link,-1,sizeof(link));
memset(mp,false,sizeof(mp));
for(int j=1;j<=k;j++)
{
int x,y;scanf("%d%d",&x,&y);
mp[x][y]=true;
}
graph();
int ans = 0;
for(int i=1;i<=n*m;i++){
memset(vis,false,sizeof(vis));
if(dfs(i))ans++;
}
printf("%d\n",ans);
if(ans>0){
for(int i=1;i<=n*m;i++){ //枚举连接的边
int j = link[i];
if(link[i]!=-1){
int x = i,y = j;
printf("(%d,%d)--(%d,%d)\n",(x-1)/m+1,(x-1)%m+1,(y-1)/m+1,(y-1)%m+1);
}
}
}
printf("\n");
}
return 0;
}