# bzoj 4554 [Tjoi2016&Heoi2016]游戏

### Output

#### Sample Input

4 4
#***
*#**
**#*
xxx#


5

### 题解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 10010
using namespace std;
const int inf = 0x3f3f3f3f;
struct flows {
struct node {
int to, next, flow;
} e[N * 20];
int tot, st[N], dis[N], cur[N];
void init() {
tot = -1, memset(e, -1, sizeof e),
memset(st, -1, sizeof st);
}
void add(int x, int y, int z) {
e[++tot].to = y;
e[tot].flow = z;
e[tot].next = st[x];
st[x] = tot;
}
void add_edge(int x, int y, int z) {
//      printf("add:%d %d %d\n", x, y, z);
}
queue <int> que;
int bfs(int S, int T) {
memcpy(cur, st, sizeof st);
memset(dis, 0, sizeof dis);
while (!que.empty()) que.pop();
que.push(S);
dis[S] = 1;
while(!que.empty()) {
int now = que.front();
que.pop();
for (int i = st[now]; ~i; i = e[i].next)
if (e[i].flow && !dis[e[i].to]) {
dis[e[i].to] = dis[now] + 1;
if (e[i].to == T) return 1;
que.push(e[i].to);
}
}
return 0;
}
int finds(int now, int T, int flow) {
if (now == T)
return flow;
int f;
for (int i = cur[now]; ~i; i = e[i].next) {
cur[now] = i;
if (e[i].flow && dis[e[i].to] == dis[now] + 1 &&
(f = finds(e[i].to, T, min(flow, e[i].flow)))) {
e[i].flow -= f;
e[i^1].flow += f;
return f;
}
}
return 0;
}
int dinic(int S, int T) {
int flow = 0, x;
while(bfs(S, T)) {
while(x = finds(S, T, inf)) {
flow += x;
}
}
return flow;
}
}flow;
int n, m, S, T, x, y, z, c, cnt;
const int K = 60;
char s[K];
int map[K][K], vis1[K][K], vis2[K][K];
main() {
scanf("%d%d", &n, &m);
flow.init();
for (int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (int j = 1; j <= m; ++j)
if (s[j] == '*') map[i][j] = 0;
else if (s[j] == 'x') map[i][j] = 1;
else map[i][j] = 2;
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (!vis1[i][j] && map[i][j] != 2) {
cnt++;
for (int k = j; k <= m && !vis1[i][k] && map[i][k] != 2; ++k)
vis1[i][k] = cnt;
}
int gap = cnt;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
if (!vis2[i][j] && map[i][j] != 2) {
cnt++;
for (int k = i; k <= n && !vis2[k][j] && map[k][j] != 2; ++k)
vis2[k][j] = cnt;
}
S = 0, T = cnt + 1;
for (int i = 1; i <= gap; ++i)