y2823774827y @ 2018-08-08 14:58:46
#include<cstdio>
#include<cstring>
using namespace std;
struct node{
int to,next;
}dis[1000005];
int dx[9]={0,-1,-2,0,0,1,2,0,0},dy[9]={0,0,0,1,2,0,0,-1,-2};
int n,m,cnt,shu,ans; int num[105][15],mat[1005],head[1005]; bool visit[1005];
char c[105][15];
inline void add(int u,int v){
dis[++shu].to=v; dis[shu].next=head[u]; head[u]=shu;
}
int dfs(int x){
for(int i=head[x];i;i=dis[i].next){
int v=dis[i].to;
if(!visit[v]){
visit[v]=true;
if(!mat[v]){
mat[v]=x;
mat[x]=v;
return 1;
}else if(dfs(mat[v])){
mat[v]=x;
mat[x]=v;
return 1;
}
}
}
return 0;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j){
scanf(" %c",&c[i][j]);
if(c[i][j]=='P')
num[i][j]=++cnt;
}
/*for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j)
printf("%d ",num[i][j]);
printf("\n");
}*/
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(num[i][j]){
printf("%d:",num[i][j]);
for(int k=1;k<=8;++k){
int x1=i+dx[k],y1=j+dy[k];
if(x1<1||x1>n||y1<1||y1>m) continue;
if(num[x1][y1])
add(num[i][j],num[x1][y1]);
}
}
for(int i=1;i<=cnt;++i)
if(!mat[i]){
memset(visit,0,sizeof(visit));
ans+=dfs(i);
}
printf("%d",cnt-ans);
return 0;
}/*
5 4
PHPP
PPHH
PPPP
PHPP
PHHP
6
*/