1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <queue> #include <set> #include <vector> using namespace std;
const int INF = ~0u>>1; typedef pair <int,int> P; #define MID(x,y) ((x+y)>>1) #define iabs(x) ((x)>0?(x):-(x)) #define REP(i,a,b) for(int i=(a);i<(b);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define pb push_back #define mp make_pair #define print() cout<<"--------"<<endl #define maxn 55
int x[maxn * maxn],y[maxn * maxn]; int xs[maxn][maxn],ys[maxn][maxn]; int xnu,ynu; char s[maxn][maxn]; bool g[maxn * maxn][maxn * maxn]; bool vis[maxn * maxn];
bool path(int u){ for (int v = 1; v <= ynu; v ++){ if (g[u][v] && !vis[v]){ vis[v] = 1;
if (!y[v] || path(y[v])) { x[u] = v; y[v] = u; return 1; } } } return 0; }
void MaxMatch(){ int ans = 0; memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); for (int i = 1;i <= xnu; i ++){ if (!x[i]) { memset(vis,0,sizeof(vis)); if (path(i)){ ans ++; } } }
printf("%dn", ans); }
int main(){ int T,t = 0,m,n; cin >> T; while (t++ < T){ printf("Case :%dn", t); scanf("%d%d", &m, &n); for (int i = 0; i < m; i ++){ scanf("%s", s[i]); } memset(xs,0,sizeof(xs)); memset(ys,0,sizeof(ys)); int number = 0; for (int i = 0; i < m; i ++){ int flag = 1; for (int j = 0; j < n; j ++){ if (s[i][j] == 'o') { if (flag) number ++,flag = 0; xs[i][j] = number; } if (s[i][j] == '#') flag = 1; } } xnu = number,number = 0; for (int j = 0; j < n; j ++){ int flag = 1; for (int i = 0; i < m; i ++){ if (s[i][j] == 'o') { if (flag) number ++,flag = 0; ys[i][j] = number; } if (s[i][j] == '#') flag = 1; } } ynu = number;
memset(g,0,sizeof(g)); for (int i = 0; i < m; i ++){ for (int j = 0; j < n; j ++){ if(xs[i][j]) g[xs[i][j]][ys[i][j]] = 1; } }
MaxMatch(); } return 0; }
|