Description
在2016年,佳缘姐姐喜欢上了一款游戏,叫做泡泡堂。简单的说,这个游戏就是在一张地图上放上若干个炸弹,看是否能炸到对手,或者躲开对手的炸弹。在玩游戏的过程中,小H想到了这样一个问题:当给定一张地图,在这张地图上最多能放上多少个炸弹能使得任意两个炸弹之间不会互相炸到。炸弹能炸到的范围是该炸弹所在的一行和一列,炸弹的威力可以穿透软石头,但是不能穿透硬石头。给定一张n*m的网格地图:其中代表空地,炸弹的威力可以穿透,可以在空地上放置一枚炸弹。x代表软石头,炸弹的威力可以穿透,不能在此放置炸弹。#代表硬石头,炸弹的威力是不能穿透的,不能在此放置炸弹。例如:给出1*4的网格地图*xx,这个地图上最多只能放置一个炸弹。给出另一个1*4的网格地图x#,这个地图最多能放置两个炸弹。现在小H任意给出一张n*m的网格地图,问你最多能放置多少炸弹。
Solution
其实一开始我是束手无策的( ̄▽ ̄)”
由于一个点放置的炸弹会影响相关行列的放置情况,那么久类似vijos的柯南那题了。不同之处在于我们要把一行分成多个由硬石头隔开的连续区间,然后顺序标号。列的话同理,枚举一下放置然后连边就是裸的二分图了,匈牙利和网络流都能A掉这题
Code
#include <stdio.h>
#include <string.h>
#include <queue>
#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)
#define erg(i, st) for (int i = ls[st]; i; i = e[i].next)
#define fill(x, t) memset(x, t, sizeof(x))
#define min(x, y) (x)<(y)?(x):(y)
#define INF 0x3f3f3f3f
#define L 101
#define N L * L + 1
#define E N * 21 + 1
struct edge{int x, y, w, next;}e[E];
int cur[N], ls[N];
inline void addEdge(int &cnt, int x, int y, int w){
cnt += 1; e[cnt] = (edge){x, y, w, ls[x]}; ls[x] = cnt;
cnt += 1; e[cnt] = (edge){y, x, 0, ls[y]}; ls[y] = cnt;
}
using std:: queue;
int dis[N];
inline int bfs(int st, int ed){
queue<int> que;
que.push(st);
fill(dis, -1);
dis[st] = 1;
while (!que.empty()){
int now = que.front(); que.pop();
erg(i, now){
if (e[i].w > 0 && dis[e[i].y] == -1){
dis[e[i].y] = dis[now] + 1;
que.push(e[i].y);
if (e[i].y == ed){
return 1;
}
}
}
}
return 0;
}
inline int find(int now, int ed, int mn){
if (now == ed || !mn){
return mn;
}
int ret = 0;
erg(i, now){
// for (int &i = cur[now]; i; i = e[i].next){
if (e[i].w > 0 && dis[now] + 1 == dis[e[i].y]){
int d = find(e[i].y, ed, min(mn - ret, e[i].w));
e[i].w -= d;
e[i ^ 1].w += d;
ret += d;
if (ret == mn){
break;
}
}
}
return ret;
}
inline int dinic(int st, int ed){
int tot = 0;
while (bfs(st, ed)){
tot += find(st, ed, INF);
}
return tot;
}
int rc[L][L], idx[L][L][2];
char st[L];
int main(void){
int n, m;
scanf("%d%d", &n, &m);
rep(i, 1, n){
scanf("%s", st);
rep(j, 1, m){
if (st[j - 1] == '*'){
rc[i][j] = 0;
}else if (st[j - 1] == 'x'){
rc[i][j] = 1;
}else if (st[j - 1] == '#'){
rc[i][j] = 2;
}
}
}
int edgeCnt = 1;
int ST = 0, ED = n * m + 1;
idx[0][0][0] = 0;
rep(i, 1, n){
rep(j, 1, m){
while (rc[i][j] == 2 && j <= m){
j += 1;
}
idx[0][0][0] += 1;
addEdge(edgeCnt, ST, idx[0][0][0], 1);
while (rc[i][j] != 2 && j <= m){
idx[i][j][0] = idx[0][0][0];
j += 1;
}
}
}
rep(j, 1, m){
rep(i, 1, n){
while (rc[i][j] == 2 && i <= n){
i += 1;
}
idx[0][0][0] += 1;
addEdge(edgeCnt, idx[0][0][0], ED, 1);
while (rc[i][j] != 2 && i <= n){
idx[i][j][1] = idx[0][0][0];
i += 1;
}
}
}
rep(i, 1, n){
rep(j, 1, m){
if (rc[i][j] == 0){
addEdge(edgeCnt, idx[i][j][0], idx[i][j][1], 1);
}
}
}
int ans = dinic(ST, ED);
printf("%d\n", ans);
return 0;
}

本文探讨了一个基于泡泡堂游戏的地图问题,旨在确定在给定地图上最多能放置多少个互不影响的炸弹。通过将问题转化为二分图匹配问题,并使用匈牙利算法或网络流算法求解。
334

被折叠的 条评论
为什么被折叠?



