题意:一张网格图,有N个柱子,有些柱子上有蜥蜴,有的没有,每只蜥蜴一次最多跳d距离,一个柱子只能被跳有限次,问最后有几只蜥蜴跳不出来网格。
题解:最大流
拆点,将每个点拆成进来和出去两个。因为这里是每个点有流量,这个很关键。
对于每一个柱子
(
i
,
j
)
,
i
d
=
(
i
−
1
)
∗
c
o
l
+
j
(i,j),id = (i - 1) * col + j
(i,j),id=(i−1)∗col+j:
①maze[id][id + row * col] = 能跳的次数
,表示跳出去。
②maze[id + row * col][T] = inf
,表示跳出网格到汇点。
③maze[id + row * col][id2] = inf
,表示跳出来的点再跳到周围距离为d的点。
对于每一个蜥蜴
(
i
,
j
)
,
i
d
=
(
i
−
1
)
∗
c
o
l
+
j
(i,j),id = (i - 1) * col + j
(i,j),id=(i−1)∗col+j:
①maze[0][id] = 1
,表示从源点出发。
这里用的sap。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
const int inf = 0x3f3f3f3f;
/*
* SAP 邻接矩阵形式
* 点的编号从 0 开始
* 增加个 flow 数组,保留原矩阵 maze, 可用于多次使用最大流
*/
const int MAXN = 1100;
int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN];//存最大流的流量
int sap(int start, int end, int nodenum) {
memset(cur, 0, sizeof(cur));
memset(dis, 0, sizeof(dis));
memset(gap, 0, sizeof(gap));
memset(flow, 0, sizeof(flow));
int u = pre[start] = start, maxflow = 0, aug = -1;
gap[0] = nodenum;
while (dis[start] < nodenum) {
loop:
for (int v = cur[u]; v < nodenum; v++)
if (maze[u][v] - flow[u][v] && dis[u] == dis[v] + 1) {
if (aug == -1 || aug > maze[u][v] - flow[u][v]) aug = maze[u][v] - flow[u][v];
pre[v] = u;
u = cur[u] = v;
if (v == end) {
maxflow += aug;
for (u = pre[u]; v != start; v = u, u = pre[u]) {
flow[u][v] += aug;
flow[v][u] -= aug;
}
aug = -1;
}
goto loop;
}
int mindis = nodenum - 1;
for (int v = 0; v < nodenum; v++)
if (maze[u][v] - flow[u][v] && mindis > dis[v]) {
cur[u] = v;
mindis = dis[v];
}
if ((--gap[dis[u]]) == 0) break;
gap[dis[u] = mindis + 1]++;
u = pre[u];
}
return maxflow;
}
int t, row, col, d;
char pil[MAXN][22], liz[MAXN][22];
int main() {
scanf("%d", &t);
int cas = 0;
while (t--) {
memset(maze, 0, sizeof(maze));
scanf("%d%d", &row, &d);
for (int i = 1; i <= row; i++) {
scanf("%s", pil[i] + 1);
if(i == 1) col = strlen(pil[1] + 1);
for (int j = 1; j <= col; j++) {
if (pil[i][j] == '0') continue;
int id = (i - 1) * col + j;
maze[id][id + row * col] = pil[i][j] - '0';
if (i <= d || i + d > row || j <= d || j + d > col) {
maze[id + row * col][2 * row * col + 1] = inf;
}
else {
for (int x = 1; x <= row; x++) {
for (int y = 1; y <= col; y++) {
if (abs(x - i) + abs(y - j) <= d) {
int id2 = (x - 1) * col + y;
if (id == id2) continue;
maze[id + row * col][id2] = inf;
}
}
}
}
}
}
int sum = 0;
for (int i = 1; i <= row; i++) {
scanf("%s", liz[i] + 1);
for (int j = 1; j <= col; j++) {
if (liz[i][j] == 'L') {
sum++;
maze[0][(i - 1) * col + j] = 1;
}
}
}
int ans = sum - sap(0, 2 * row * col + 1, 2 * row * col + 2);
if(ans == 0) printf("Case #%d: no lizard was left behind.\n", ++cas);
else if(ans == 1) printf("Case #%d: %d lizard was left behind.\n", ++cas, ans);
else printf("Case #%d: %d lizards were left behind.\n", ++cas, ans);
}
return 0;
}