Leapin' Lizards
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3664 Accepted Submission(s): 1498
Problem Description
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below... Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
Input
The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is
always 1 ≤ d ≤ 3.
always 1 ≤ d ≤ 3.
Output
For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.
Sample 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..
........
........
Sample Output
Case #1: 2 lizards were left behind.
Case #2: no lizard was left behind.
Case #3: 3 lizards were left behind.
Case #4: 1 lizard was left behind.
Source
【思路】
题目给了一个“棋盘”,每个棋盘格子里可能有一只蜥蜴,棋盘格子允许被离开若干次,而蜥蜴只能跳到距离(距离定义:dist = 横坐标差的绝对值 + 纵坐标差的绝对值)不大于d的格子上,问最后会有几只蜥蜴被剩下。
这可以转化为一个最大流问题,即求从源点s(棋盘)到终点t(外面)能流过多少只蜥蜴。
做法:先定义一个超级源点s。由于每个点只允许被经过若干次,我们拆点,每个点的点1到点2之间建立一条容量为它使用次数的边。若棋盘格子上i有蜥蜴,则在s和i的点1建立一条容量为1的边。若格子i能直接跳出棋盘,则i的点2和t建立一条容量为无穷大的边。若点i无法跳出棋盘,才在它的点2和它能到达的其它点的点1之间建立一条容量为无穷大的边。最后用Dinic算法跑一遍最大流,把答案处理出来。
【代码】
//******************************************************************************
// File Name: HDU_2732.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年07月12日 星期四 20时31分19秒
//******************************************************************************
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int MAXN = 1005, INF = 0x3f3f3f3f;
struct edge {
int from, to, cap, flow;
edge() {}
edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};
int t, n, rows, len, leap;
vector<edge> e;
vector<int> g[MAXN];
queue<int> q;
int d[MAXN], cur[MAXN];
int step[25][25];
bool lizard[25][25], reach[25][25];
void init()
{
e.clear();
for (int i = 0; i <= n; i++) g[i].clear();
}
void add_edge(int u, int v, int c, int f)
{
e.push_back(edge(u, v, c, f));
g[u].push_back(e.size() - 1);
}
bool bfs(int s, int t)
{
while (!q.empty()) q.pop();
memset(d, 0, sizeof(d));
d[s] = 1; q.push(s);
while (!q.empty()) {
int u = q.front(); q.pop();
for (int i = 0; i < g[u].size(); i++) {
edge &now = e[g[u][i]];
if (d[now.to] || now.cap - now.flow == 0) continue;
d[now.to] = d[u] + 1;
q.push(now.to);
}
}
return (d[t] != 0);
}
int dfs(int u, int t, int f)
{
if (u == t || f == 0) return f;
int ans = 0, add;
for (int &i = cur[u]; i < g[u].size(); i++) {
edge &now = e[g[u][i]];
if (d[now.to] == d[u] + 1 && (add = dfs(now.to, t, min(f, now.cap - now.flow))) > 0) {
e[g[u][i]].flow += add;
e[g[u][i] ^ 1].flow -= add;
ans += add;
f -= add;
if (f == 0) break;
}
}
return ans;
}
int dinic(int s, int t)
{
int ans = 0;
while (bfs(s, t)) {
memset(cur, 0, sizeof(cur));
ans += dfs(s, t, INF);
}
return ans;
}
bool ok(int x, int y)
{
return (1 <= x && x <= rows && 1 <= y && y <= len);
}
bool get(int i, int j, int k, int l)
{
return (abs(i - k) + abs(j - l) <= leap);
}
int main()
{
scanf("%d", &t);
for (int kase = 1; kase <= t; kase++) {
printf("Case #%d: ", kase);
scanf("%d %d", &rows, &leap);
char str[25];
int number = 0;
for (int i = 1; i <= rows; i++) {
scanf("%s", str);
len = strlen(str);
for (int j = 1; j <= len; j++) step[i][j] = str[j - 1] - '0';
}
for (int i = 1; i <= rows; i++) {
scanf("%s", str);
for (int j = 1; j <= len; j++)
if (str[j - 1] == '.')
lizard[i][j] = false;
else
lizard[i][j] = true, number++;
}
n = len * rows * 2 + 1;
init();
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= len; j++)
reach[i][j] = (get(i, j, 0, j) || get(i, j, rows + 1, j) || get(i, j, i, 0) || get(i, j, i, len + 1));
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= len; j++) {
if (step[i][j] != 0) {
add_edge((i - 1) * len + j, rows * len + (i - 1) * len + j, step[i][j], 0);
add_edge(rows * len + (i - 1) * len + j, (i - 1) * len + j, 0, 0);
}
if (lizard[i][j]) {
add_edge(0, (i - 1) * len + j, 1, 0);
add_edge((i - 1) * len + j, 0, 0, 0);
}
if (reach[i][j] && step[i][j] != 0) {
add_edge(rows * len + (i - 1) * len + j, n, INF, 0);
add_edge(n, rows * len + (i - 1) * len + j, 0, 0);
}
else {
for (int k = i - leap; k <= i + leap; k++)
for (int l = j - leap; l <= j + leap; l++)
if (ok(k, l) && (k != i || l != j) && step[i][j] && step[k][l] && get(i, j, k, l)) {
add_edge(rows * len + (i - 1) * len + j, (k - 1) * len + l, INF, 0);
add_edge((k - 1) * len + l, rows * len + (i - 1) * len + j, 0, 0);
}
}
}
}
int ans = number - dinic(0, n);
if (ans == 0) printf("no lizard was left behind.\n");
if (ans == 1) printf("1 lizard was left behind.\n");
if (ans >= 2) printf("%d lizards were left behind.\n", ans);
}
return 0;
}