题目地址


易错点:

  • 稳定加边请使用n*m.
  • 点编号不重合就没必要双向设置匹配点.
  • 写代码的时候请朴实一点.

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N=105,MAXN=105*105;
struct Edge{
	int from,to,nxt;
}e[MAXN];
int head[MAXN],edgeCnt=1;
void addEdge(int u,int v){
	e[++edgeCnt].from=u;
	e[edgeCnt].to=v;
	e[edgeCnt].nxt=head[u];
	head[u]=edgeCnt;
}
bool vis[MAXN];
int match[MAXN];
bool dfs(int x){
	for(int i=head[x];i;i=e[i].nxt){
		int nowV=e[i].to;
		if(vis[nowV])continue;
		vis[nowV]=1;
		if(!match[nowV]||dfs(match[nowV])){
			match[nowV]=x;
			return 1;
		}
	}
	return 0;
}
bool a[N][N];
int pointCnt=0,landscape[N][N],vertical[N][N];
int n,m;
int landCnt=0;
void init(){	
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++){
		char tmp;
		cin>>tmp;
		if(tmp=='*')a[i][j]=1;
		else a[i][j]=0;
	}
	
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++){
		if(!a[i][j])continue;
		if(a[i][j-1])landscape[i][j]=pointCnt;
		else landscape[i][j]=++pointCnt;
	}
	landCnt=pointCnt;
	
	pointCnt=0;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++){
		if(!a[i][j])continue;
		if(a[i-1][j])vertical[i][j]=vertical[i-1][j];
		else vertical[i][j]=++pointCnt;
	}
			
	for(int i=1;i<=n;i++)
	for(int j=1;j<=m;j++)
		if(a[i][j])
			addEdge(landscape[i][j],vertical[i][j]+n*m);
}
int main(){
	init();
	int ans=0;
	for(int i=1;i<=landCnt;i++){
		memset(vis,0,sizeof(vis));
		ans+=dfs(i);
	}
	printf("%d\n",ans);
	return 0;
}