题意:
一个n*m迷宫中,有一些Lizard(🦎)和柱子,每个柱子最多可以跳x次(0~3),求最多有多少只Lizard可以跳出迷宫。
- 输入
- T<=25表示样例组数
- 每组由n ,d(1~20)开始,n表示行数,d表示可以跳的最远的曼哈顿距离(题目貌似没说)
- 一个n*m矩阵,aij表示珠子可以跳多少次
- 一个n*m矩阵,bij==‘L'表示这个地方最开始有Lizard
- 输出:最少多少只不能跳出迷宫
题解:
网络流=建图难。
ps:很快写好之后找了一个半小时的BUGemmm。最后才发现是point_out->point_in那里的问题,如果权值为1表示只能跳一次,权值应该为INF。唉,刷专题的过程中,被卡了,果断到网上去找题解,别闭门造车,浪费时间,现在知识都尚未完备,很多东西自己想不出来的(害别不承认)。
代码:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
// #define int long long
#define read(x) scanf("%d", &x)
#define print(a, c) printf("%d%c", a, c)
#define pb push_back
#define dbg(x) cout << #x << "===" << x << endl
using namespace std;
const int N = 1e5 + 10;
const int M = 1e5 + 10;
const int INF = 1e9 + 10;
struct Edge {
int u, v, w, next;
Edge(int u = 0, int v = 0, int w = 0, int next = 0)
: u(u), v(v), w(w), next(next) {}
} e[M];
int head[N], cnt;
void add(int u, int v, int w) {
// dbg(w);
e[cnt] = Edge(u, v, w, head[u]), head[u] = cnt++;
e[cnt] = Edge(v, u, 0, head[v]), head[v] = cnt++;
}
int n, m, d, num; // num记录Lizard的数量
int s, t;
char a[25][25], b[25][25];
int dep[N], now[N];
bool bfs() {
for (int i = 0; i <= t; i++) dep[i] = INF, now[i] = head[i];
queue<int> q;
q.push(s), dep[s] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i = now[x]; ~i; i = e[i].next) {
int v = e[i].v;
if (dep[v] == INF && e[i].w > 0) {
dep[v] = dep[x] + 1;
if (v == t) return true;
q.push(v);
}
}
}
return false;
}
int dfs(int x, int flow) {
if (x == t) return flow;
int ans = 0;
for (int i = now[x]; ~i && flow; i = e[i].next) {
int v = e[i].v;
now[x] = i;
if (dep[x] + 1 == dep[v] && e[i].w > 0) {
int tmp = dfs(v, min(flow, e[i].w));
if (tmp == 0) dep[v] = INF;
e[i].w -= tmp;
e[i ^ 1].w += tmp;
ans += tmp;
flow -= tmp;
}
}
return ans;
}
int Dinic() {
int res = 0;
// cout << bfs() << endl;
while (bfs()) res += dfs(s, INF);
return res;
}
//在时间复杂度允许的情况下,怎么操作方便怎么来
void BFS(int sx, int sy) {
int il = max(0, sx - d), ir = min(n - 1, sx + d);
int jl = max(0, sy - d), jr = min(m - 1, sy + d);
for (int i = il; i <= ir; i++) {
for (int j = jl; j <= jr; j++) {
if (sx == i && sy == j) continue;
if (abs(sx - i) + abs(sy - j) <= d && a[i][j] != '0')
add(n * m + sx * m + sy, i * m + j, INF);
}
}
}
void build() {
m = strlen(a[0]);
// dbg(m);
s = n * m * 2 + 1, t = s + 1;
// point_in->point_out , s->point_in
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] != '0')
add(i * m + j, n * m + i * m + j, a[i][j] - '0');
if (b[i][j] == 'L') add(s, i * m + j, 1), num++;
}
}
// dbg(2);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '0') continue;
if (i - d < 0 || i + d >= n || j - d < 0 || j + d >= m)
add(n * m + i * m + j, t, INF);
// point_out->t
else
BFS(i, j);
// point_out->point_in 实现:增广路途中点都消耗一次跳跃次数
}
}
}
void init() {
memset(head, -1, sizeof(head)), cnt = 0;
num = 0;
}
signed main() {
int T;
read(T);
for (int _ = 1; _ <= T; _++) {
init();
read(n), read(d);
for (int i = 0; i < n; i++) scanf("%s", a[i]); //, puts(a[i]);
// dbg(tmp);
for (int i = 0; i < n; i++) scanf("%s", b[i]);
build();
// dbg(num);
int ans = num - Dinic();
// dbg(ans);
if (ans == 0)
printf("Case #%d: no lizard was left behind.\n", _);
else if (ans == 1)
printf("Case #%d: %d lizard was left behind.\n", _, ans);
else
printf("Case #%d: %d lizards were left behind.\n", _, ans);
}
return 0;
}
/*
input:::
4
3 1
1111
1111
1111
LLLL
LLLL
LLLL
3 2
00000
01110
00000
.....
.LLL.
.....
3 1
00000
01110
00000
.....
.LLL.
.....
5 2
00000000
02000000
00321100
02000000
00000000
........
........
..LLLL..
........
........
ouput:::
2
no
3
1
*/