4 .X.. .... XX.. .... 2 XX .X 3 .X. X.X .X. 3 ... .XX .XX 4 .... .... .... .... 0
5 1 5 2 4
#include<stdio.h> #include<string.h> char map[10][10]; int ans[20][20],vis[20],link[20],n,x[10][10],y[10][10],n1,n2; int dfs(int x) { int i; for(i=1;i<n2;i++) { if(!vis[i]&&ans[x][i]) { vis[i]=1; if(link[i]==-1||dfs(link[i])) { link[i]=x; return 1; } } } return 0; } int main() { while(scanf("%d",&n)!=EOF,n) { int i,j; for(i=1;i<=n;i++) { scanf("%s",map[i]+1); } //int n1,n2; n1=n2=1; memset(ans,0,sizeof(ans)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(map[i][j]=='.') { x[i][j]=n1; } if(map[i][j]=='X') n1++; } n1++; } for(j=1;j<=n;j++) { for(i=1;i<=n;i++) { if(map[i][j]=='.') y[i][j]=n2; if(map[i][j]=='X') n2++; } n2++; } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(map[i][j]=='.') { ans[x[i][j]][y[i][j]]=1; } } } memset(link,-1,sizeof(link)); int res=0; for(i=1;i<n1;i++) { memset(vis,0,sizeof(vis)); if(dfs(i)) res++; } printf("%d\n",res); } }
NYOJ 题目587 blockhouses(二分图最大匹配)
原文:http://blog.csdn.net/yu_ch_sh/article/details/44901957