标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6991 | Accepted: 3466 |
Description
Input
Output
Sample Input
2 7 9 ooo**oooo **oo*ooo* o*oo**o** ooooooooo *******oo o*o*oo*oo *******oo 10 1 * * * o * * * * * *
Sample Output
17 5
Source
1 #include<stdio.h> 2 #include<iostream> 3 #include<queue> 4 #include<string.h> 5 using namespace std; 6 bool map[500][500] ; 7 int vis[500][500] ; 8 int a[500][500] ; 9 int girl [500] ; 10 bool sta[500] ; 11 int cnt ; 12 int row , col ; 13 char st[500] ; 14 int move[][2] = {1 , 0 , 0 , 1 , -1 , 0 , 0 , -1} ; 15 16 bool hungary (int x) 17 { 18 for (int i = 1 ; i <= cnt ; i++) { 19 if (map[x][i] && sta[i] == false) { 20 sta[i] = true ; 21 if (girl[i] == 0 || hungary (girl[i])) { 22 girl[i] = x ; 23 return true ; 24 } 25 } 26 } 27 return false ; 28 } 29 30 int main () 31 { 32 // freopen ("a.txt" , "r" , stdin) ; 33 int T ; 34 cin >> T ; 35 while (T--) { 36 scanf ("%d%d" , &row , &col) ; 37 getchar () ; 38 cnt = 0 ; 39 memset (vis , -1 , sizeof(vis)) ; 40 memset (map , 0 , sizeof(map)) ; 41 memset (a , -1 , sizeof(a)) ; 42 memset (girl , 0 , sizeof(girl)) ; 43 for (int i = 0 ; i < row ; i++) { 44 gets (st) ; 45 for (int j = 0 ; j < col ; j++) { 46 if (st[j] == ‘*‘) { 47 a[i + 1][j + 1] = 1 + cnt++; 48 } 49 } 50 } 51 /* for (int i = 1 ; i <= row ; i++) { 52 for (int j = 1 ; j <= col ; j++) { 53 printf ("%d " , a[i][j]); 54 } 55 puts ("") ; 56 }*/ 57 for (int i = 1 ; i <= row ; i++) { 58 for (int j = 1 ; j <= col ; j++) { 59 if (a[i][j] != -1) { 60 for (int k = 0 ; k < 4 ; k++) { 61 int x = i + move[k][0] ; 62 int y = j + move[k][1] ; 63 if (a[x] [y] != -1) 64 map[ a[i][j] ] [ a[x][y] ] = 1 ; 65 } 66 } 67 } 68 } 69 /* for (int i = 1 ; i <= cnt ; i++) { 70 for (int j = 1 ; j <= cnt ; j++) { 71 printf ("%d " , map[i][j]) ; 72 } 73 puts ("") ; 74 }*/ 75 int all = 0 ; 76 for (int i = 1 ; i <= cnt ; i++) { 77 memset (sta , 0 , sizeof(sta)) ; 78 if (hungary (i)) 79 all ++ ; 80 } 81 // printf ("cnt = %d , all = %d\n" , cnt , all) ; 82 printf ("%d\n" , cnt - all / 2) ; 83 } 84 return 0 ; 85 }
Antenna Placement(匈牙利算法 ,最少路径覆盖)
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4316657.html