define n the number of ' * '
define d the number of couple of two points
define s the single point that can't link to others ( means no points around it in 4 directions )
n = 2 * d + s ;
d + s = n - d
use Hungary finding out the d
#include <stdio.h>
#include <string.h>
char gird[][];
int number[][];
int graph[][];
int in[];
int already[];
int cnt; struct node{
int x,y;
}p[];
int dir[][]={ ,,,-,-,,,}; int find(int a){
int i;
for(i=;i<cnt;++i){
if( graph[a][i]== && in[i]== ){
in[i]=;
if( already[i]==- || find( already[i] )){
already[i]=a;
return ;
}
}
}
return ;
} int main(){
int t;
int n,m,i,j;
int tx,ty,tnum;
int res;
while(~scanf("%d",&t)){
while(t--){
res=;
cnt=;
memset(in,,sizeof(in));
memset(graph,,sizeof(graph));
for(i=;i<;++i) already[i]=-;
scanf("%d%d",&n,&m);
for(i=;i<n;++i){
scanf("%s",gird[i]);
for(j=;j<m;++j)
if(gird[i][j]=='*'){
number[i][j]=cnt;
p[cnt].x=i;
p[cnt].y=j;
cnt++;
}
}
for(i=;i<cnt;++i){
for(j=;j<;++j){
tx=p[i].x+dir[j][];
ty=p[i].y+dir[j][];
if((!(tx>=&&tx<n&&ty>=&&ty<m))||gird[tx][ty]!='*') continue;
tnum=number[tx][ty];
graph[i][tnum]=;
}
}
for(i=;i<cnt;++i){
memset(in,,sizeof(in));
if(find(i)==)
res++;
}
printf("%d\n",cnt-res/);
}
}
return ;
}