4554: [Tjoi2016&Heoi2016]游戏
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 397 Solved: 246
[ Submit][ Status][ Discuss]
Description
在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂。简单的说,这个游戏就是在一张地图上放上若干个炸弹,看
是否能炸到对手,或者躲开对手的炸弹。在玩游戏的过程中,小H想到了这样一个问题:当给定一张地图,在这张
地图上最多能放上多少个炸弹能使得任意两个炸弹之间不会互相炸到。炸弹能炸到的范围是该炸弹所在的一行和一
列,炸弹的威力可以穿透软石头,但是不能穿透硬石头。给定一张n*m的网格地图:其中*代表空地,炸弹的威力可
以穿透,可以在空地上放置一枚炸弹。x代表软石头,炸弹的威力可以穿透,不能在此放置炸弹。#代表硬石头,炸
弹的威力是不能穿透的,不能在此放置炸弹。例如:给出1*4的网格地图*xx*,这个地图上最多只能放置一个炸弹
。给出另一个1*4的网格地图*x#*,这个地图最多能放置两个炸弹。现在小H任意给出一张n*m的网格地图,问你最
多能放置多少炸弹
Input
第一行输入两个正整数n,m,n表示地图的行数,m表示地图的列数。1≤n,m≤50。接下来输入n行m列个字符,代表网
格地图。*的个数不超过n*m个
Output
输出一个整数a,表示最多能放置炸弹的个数
Sample Input
4 4
#∗∗∗
∗#∗∗
∗∗#∗
xxx#
#∗∗∗
∗#∗∗
∗∗#∗
xxx#
Sample Output
5
HINT
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 5000;
const int maxm = 1E6 + 10;
const int INF = ~0U>>1;
struct E{
int to,cap,flow; E(){}
E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}
}edgs[maxm];
int n,m,cnt,tot,S,T,bc[55][55],br[55][55],cur[maxn],L[maxn];
char p[55][55];
queue <int> Q;
vector <int> v[maxn];
void Add(int x,int y,int cap)
{
v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0);
v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}
bool BFS()
{
memset(L,-1,sizeof(L));
Q.push(S); L[S] = 1;
while (!Q.empty())
{
int k = Q.front(); Q.pop();
for (int i = 0; i < v[k].size(); i++)
{
E e = edgs[v[k][i]];
if (e.cap == e.flow) continue;
if (L[e.to] != -1) continue;
L[e.to] = L[k] + 1;
Q.push(e.to);
}
}
return L[T] != -1;
}
int Dinic(int x,int a)
{
if (x == T) return a;
int flow = 0;
for (int &i = cur[x]; i < v[x].size(); i++)
{
E &e = edgs[v[x][i]];
if (e.cap == e.flow) continue;
if (L[e.to] != L[x] + 1) continue;
int f = Dinic(e.to,min(a,e.cap - e.flow));
if (!f) continue;
flow += f;
a -= f;
e.flow += f;
edgs[v[x][i]^1].flow -= f;
if (!a) return flow;
}
if (!flow) L[x] = -1;
return flow;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m; S = 1; T = tot = 2;
for (int i = 1; i <= n; i++) scanf("%s",p[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (p[i][j] == '#') continue;
else if (j == 1 || p[i][j-1] == '#') br[i][j] = ++tot,Add(S,tot,1);
else br[i][j] = br[i][j-1];
for (int j = 1; j <= m; j++)
for (int i = 1; i <= n; i++)
if (p[i][j] == '#') continue;
else if (i == 1 || p[i-1][j] == '#') bc[i][j] = ++tot,Add(tot,T,1);
else bc[i][j] = bc[i-1][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (p[i][j] == '*') Add(br[i][j],bc[i][j],1);
int MaxFlow = 0;
while (BFS())
{
for (int i = 1; i <= tot; i++) cur[i] = 0;
MaxFlow += Dinic(S,INF);
}
cout << MaxFlow;
return 0;
}