symons

___________每一天都是幸福的!!

  博客园  ::  :: 新随笔  ::  :: 订阅 订阅  :: 管理

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 char gird[50][25];
 4 int number[50][25];
 5 int graph[1000][1000];
 6 int in[1000];
 7 int already[1000];
 8 int cnt;
 9 
10 struct node{
11     int x,y;
12 }p[1000];
13 int dir[4][2]={ 0,1,0,-1,-1,0,1,0};
14 
15 int find(int a){
16     int i;
17     for(i=0;i<cnt;++i){
18         if( graph[a][i]==1 && in[i]==0 ){
19             in[i]=1;
20             if( already[i]==-1 || find( already[i] )){
21                 already[i]=a;
22                 return 1;
23             }
24         }
25     }
26     return 0;
27 }
28 
29 int main(){
30     int t;
31     int n,m,i,j;
32     int tx,ty,tnum;
33     int res;
34     while(~scanf("%d",&t)){
35         while(t--){
36             res=0;
37             cnt=0;
38             memset(in,0,sizeof(in));
39             memset(graph,0,sizeof(graph));
40             for(i=0;i<1000;++i)    already[i]=-1;
41             scanf("%d%d",&n,&m);
42             for(i=0;i<n;++i){
43                 scanf("%s",gird[i]);
44                 for(j=0;j<m;++j)
45                     if(gird[i][j]=='*'){
46                         number[i][j]=cnt;
47                         p[cnt].x=i;
48                         p[cnt].y=j;
49                         cnt++;
50                     }
51             }
52             for(i=0;i<cnt;++i){
53                 for(j=0;j<4;++j){
54                     tx=p[i].x+dir[j][0];
55                     ty=p[i].y+dir[j][1];
56                     if((!(tx>=0&&tx<n&&ty>=0&&ty<m))||gird[tx][ty]!='*')    continue;
57                     tnum=number[tx][ty];
58                     graph[i][tnum]=1;
59                 }
60             }
61             for(i=0;i<cnt;++i){
62                 memset(in,0,sizeof(in));
63                 if(find(i)==1)
64                     res++;
65             }
66             printf("%d\n",cnt-res/2);
67         }
68     }
69     return 0;
70 }
71             

 

 

 

posted on 2014-03-18 14:54  symons  阅读(151)  评论(0编辑  收藏  举报