# Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2846    Accepted Submission(s): 1171

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.

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.

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.

给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d,如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x,即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.

本题类似POJ3498:

http://blog.csdn.net/u013480600/article/details/38900315

建图:

源点S编号0,网格的每个格子分成两个点i和i+n*m(nm为网格的行和列数,其实i编号点是表示蜥蜴进来,i+n*m编号的点是表示蜥蜴出去).汇点t编号n*m*2+1.

如果格子i上有蜥蜴,那么从s到i有边(s,i,1).

如果格子i能承受x次跳出,那么有边(i,i+n*m,x)

如果从格子i能直接跳出网格边界,那么有边(i+n*m,t,INF)

如果从格子i不能直接跳出网格,那么从i到离i距离<=d的网格j有边(i+n*m,j,INF). 注意这里的距离是abs(行号之差)+abs(列号之差)

最终我们求出的最大流就是能跳出网格的蜥蜴数.

原题中提到:任意时刻每个柱子上最多只有1只蜥蜴在上面,那么我们上面的解法会不会与这个要求冲突呢?

不会的,假设有k只蜥蜴能出去,那么一定存在一个符合上面要求的解,使得这k只蜥蜴按顺序出去,在任意时刻每个柱子上最多只有1只蜥蜴.(假设在某个时刻,蜥蜴j想出去,它跳到了柱子h上,但是柱子h上已经有蜥蜴了,那么这样就违反了上面的要求. 其实我们可以这么想,我们为什么不让柱子h上的蜥蜴先按照蜥蜴j以前的逃跑路线出去,然后再让蜥蜴j到柱子h上去替代之前的蜥蜴,那么这样既不违反规则,也得到了解)

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 1000;
const int INF = 0x3f3f3f3f;
int head[maxn], cur[maxn], d[maxn], n, m, s, t, k, sum;
char maze[25][25], val[25][25];
struct node
{
int v, w, next;
}edge[maxn*maxn];
void addEdge(int u, int v, int w)
{
edge[k].v = v;
edge[k].w = w;
edge[k].v = u;
edge[k].w = 0;

}
int bfs()
{
memset(d, 0, sizeof(d));
d[s] = 1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int u = q.front();
if(u == t) return 1;
q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
int to = edge[i].v, w = edge[i].w;
if(w && d[to] == 0)
{
d[to] = d[u] + 1;
if(to == t) return 1;
q.push(to);
}
}
}
return 0;
}
int dfs(int u, int maxflow)
{
if(u == t) return maxflow;
int ret = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int to = edge[i].v, w = edge[i].w;
if(w && d[to] == d[u]+1)
{
int f = dfs(to, min(maxflow-ret, w));
edge[i].w -= f;
edge[i^1].w += f;
ret += f;
if(ret == maxflow) return ret;
}
}
return ret;
}
int Dinic()
{
int ans = 0;
while(bfs() == 1)
{
ans += dfs(s, INF);
}
return ans;
}
int main()
{
int T, ca = 1;
scanf("%d", &T);
while(T--)
{
k = 0; sum = 0;
int step;
scanf("%d%d", &n, &step);
for(int i = 1; i <= n; i++)
scanf(" %s", val[i]);
for(int i = 1; i <= n; i++)
scanf(" %s", maze[i]);
m = strlen(val[1]);
s = 0; t = n*m*2+1;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < m; j++)
{
int in = (i-1)*m+j+1, out = (i-1)*m+j+1+n*m;
if(maze[i][j] == 'L')
{
sum++;
}
if(val[i][j] == '0') continue;
if(i+step > n || i-step < 1 || j+step >= m || j-step < 0)
else
{
for(int x = 1; x <= n; x++)
for(int y = 0; y < m; y++)
{
if(val[x][y] == '0') continue;
if(x == i && y == j) continue;
if(abs(x-i) + abs(y-j) <= step)
}
}
}
}
int ans = Dinic();
//        cout << ans << endl;
if(sum-ans == 0)
printf("Case #%d: no lizard was left behind.\n", ca++);
else if(sum-ans == 1)
printf("Case #%d: 1 lizard was left behind.\n", ca++, sum-ans);
else
printf("Case #%d: %d lizards were left behind.\n", ca++, sum-ans);
}
return 0;
}

#### HDU 2732 Leapin' Lizards(最大流)

2014-08-31 22:21:11

#### HDU 2732 Leapin' Lizards(拆点+最大流)

2017-08-20 18:16:03

#### HDU 2732 Leapin' Lizards （最大流，拆点）

2016-10-02 01:35:59

#### HDU 2732 Leapin' Lizards(拆点+最大流)

2014-08-17 17:10:04

#### hdu 2732 Leapin' Lizards (拆点，最大流)

2015-03-30 19:58:03

#### hdu 2732 Leapin' Lizards 拆点最大流 isap

2015-05-26 17:27:30

#### HDU 2732 Leapin' Lizards(拆点+最大流）

2014-11-12 15:09:54

#### HDU 2732 Leapin' Lizards(最大流+拆点)

2014-04-25 20:17:22

#### hdu 2732 Leapin' Lizards (最大流)

2015-08-13 15:19:02

#### hdu 2732 Leapin' Lizards(最大流)

2014-03-05 19:40:20