
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)
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxv = 100 + 5;
struct Edge{
int x;
int y;
}edge[maxv];
int N,M;
int K,X,Y;
int cnt,sum;
int match[maxv];
bool vis[maxv];
int num[maxv][maxv];
bool graph[maxv][maxv];
bool trueGraph[maxv][maxv];
void init(){
cnt=0; sum=0;
memset(num,0,sizeof(num));
memset(match,-1,sizeof(match));
memset(graph,true,sizeof(graph));
memset(trueGraph,false,sizeof(trueGraph));
}
void getGraph(int x,int y){
if(x-1>=1&&graph[x-1][y]) trueGraph[num[x][y]][num[x-1][y]]=true;
if(x+1<=N&&graph[x+1][y]) trueGraph[num[x][y]][num[x+1][y]]=true;
if(y-1>=1&&graph[x][y-1]) trueGraph[num[x][y]][num[x][y-1]]=true;
if(y+1<=M&&graph[x][y+1]) trueGraph[num[x][y]][num[x][y+1]]=true;
}
void mem(){
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
if(graph[i][j]){
num[i][j]=++cnt;
edge[cnt].x=i;
edge[cnt].y=j;
}
}
}
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
if(graph[i][j]&&((i+j)&1)) getGraph(i,j);
}
bool dfs(int u){
for(int v=1;v<=cnt;v++){
if(!vis[v]&&trueGraph[u][v]){
vis[v]=true;
if(match[v]==-1 || dfs(match[v])){
match[v]=u;
return true;
}
}
}return false;
}
void getMap(){
for(int u=1;u<=cnt;u++){
memset(vis,false,sizeof(vis));
if(dfs(u)) sum++;
}
}
void print(){
printf("%d\n",sum);
for(int i=1;i<=cnt;i++){
int j=match[i];
if(match[i]!=-1){
printf("(%d,%d)--(%d,%d)\n",edge[i].x,edge[i].y,edge[j].x,edge[j].y);
}
}
}
void doit(){
mem();
getMap();
print();
}
int main(){
while(scanf("%d%d",&N,&M)!=EOF){
if(N==0&&M==0) break;
init();
scanf("%d",&K);
for(int i=0;i<K;i++){
scanf("%d%d",&X,&Y);
graph[X][Y]=false;
}
doit();
}return 0;
}
HDU_1507_Uncle Tom's Inherited Land*(二分图匹配)
原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/45605485