
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
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)
/*=============================================================================
#
# Author: liangshu - cbam
#
# QQ : 756029571
#
# School : 哈尔滨理工大学
#
# Last modified: 2015-08-27 10:36
#
# Filename: E.cpp
#
# Description:
# The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF=201;
int cnt[INF][INF];
int dic[INF][INF];
int from[INF],tot;
int pre[INF][2];
bool use[INF];
int dir[][2] = {{1,0},{0,1},{0,-1},{-1,0}};
vector<int>G[INF * INF];
int n, m;
int num ;
bool match(int x)
{
for(int i=0;i<G[x].size();i++)
{
if(!use[G[x][i]])
{
use[G[x][i]]=1;
if(from[G[x][i]]==-1||match(from[G[x][i]]))
{
from[G[x][i]]=x;
return 1;
}
}
}
return 0;
}
int hungary( )
{
tot=0;
memset(from,255,sizeof(from));
for(int i=1;i<= num - 1;i++)
{
memset(use,0,sizeof(use));
if(match(i))
{
++tot;
}
}
return tot;
}
int main(){
while(scanf("%d%d",&n, &m) != EOF && n + m){
int k ;scanf("%d",&k);
memset(dic, 0, sizeof(dic));
memset(cnt, 0, sizeof(cnt));
memset(pre, 0, sizeof(pre));
for(int i = 1; i <= k; i++){
int x, y;
scanf("%d%d",&x, &y);
cnt[x][y] = 1;
}
num = 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <=m; j++){
if(!cnt[i][j]){
pre[num][0] = i;
pre[num][1] = j;
dic[i][j] = num++;
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(dic[i][j])
if(((i + j) & 1))
for(int k = 0; k < 4; k++)
{
int x = i + dir[k][0];
int y = j + dir[k][1];
if(x >= 1 && x <= n && y >=1 && y <=m && dic[x][y])
G[dic[i][j]].push_back(dic[x][y]);
}
}
}
int ans = hungary();
cout<<ans<<endl;
for(int i = 1; i <= num - 1; i++){
if(from[i] != -1){
cout<<"("<<pre[i][0]<<","<<pre[i][1]<<")--"
<<"("<<pre[from[i]][0]<<","<<pre[from[i]][1]<<")\n";
}
}
for(int i = 1; i <= INF * INF; i++)
G[i].clear();
cout<<endl;
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
杭电HDU ACM Uncle Tom's Inherited Land*(二分图匹配 建模)
原文地址:http://blog.csdn.net/lsgqjh/article/details/48023459