﻿﻿

# Leapin' Lizards

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

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.

AC代码：

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 1000+10//最多800个点
#define MAXM 40000
#define INF 100000+10
using namespace std;
struct Edge
{
int from, to, cap, flow, next;
}edge[MAXM];
int N, M, D;
int head[MAXN], cur[MAXN], top;
int dist[MAXN];
bool vis[MAXN];
char Map[21][21];
char val[21][21];//记录每个位置最多可以跳几次
int source = 900, sink = 1000;//超级汇点
int sum;//蜥蜴总数目
bool judge(int x, int y)//判断是否越界
{
return x >= 0 && x < N && y >= 0 && y < M;
}
void init()
{
top = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
Edge E1 = {u, v, w, 0, head[u]};
edge[top] = E1;
head[u] = top++;
Edge E2 = {v, u, 0, 0, head[v]};
edge[top] = E2;
head[v] = top++;
}
void getMap()
{
int a, b, c;
int x, y;
sum = 0;
int move1[4][2] = {0,1, 0,-1, 1,0, -1,0};//D为1 可以跳4个点
int move2[8][2] = {0,2, 0,-2, 2,0, -2,0, 1,1, 1,-1, -1,1, -1,-1};//D为2 可以跳着8个点加上D为1的4个点  下同
int move3[16][2] = {0,3, 0,-3, 3,0, -3,0, 1,2, 1,-2, -1,2, -1,-2, 2,1, 2,-1, -2,1, -2,-1, 2,2, 2-2, -2,2, -2,-2};//D为3
int move4[20][2] = {0,4, 0,-4, 4,0, -4,0, 1,3, 1,-3, -1,3, -1,-3, 2,3, 2,-3, -2,3, -2,-3, 3,1, 3,-1, -3,1, -3,-1, 3,2, 3,-2, -3,2, -3,-2};
for(int i = 0; i < N; i++)
{
scanf("%s", val[i]);
M = strlen(val[i]);
for(int j = 0; j < M; j++)
{
if(val[i][j] == '0') continue;
a = j + i * M;
b = 400 + a;
addEdge(a, b, val[i][j]-'0');//拆点建边
}
}
for(int i = 0; i < N; i++)
{
scanf("%s", Map[i]);
for(int j = 0; j < M; j++)
{
if(Map[i][j] == 'L')//有蜥蜴
{
sum++;
a = j + i * M;
addEdge(source, a, 1);//从源点引一条 容量为一的边
}
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
if(val[i][j] > '0')
{
a = j + i * M;
b = 400 + a;
for(int k = 0; k < 4; k++)
{
x = i + move1[k][0];
y = j + move1[k][1];
c = y + x * M;
if(judge(x, y))//没有越界
{
if(val[x][y] != '0')//当前位置必须 可跳跃至少一次
addEdge(b, c, INF);//直接连接
}
else//越界说明可以直接跳出去
addEdge(b, sink, INF);//连接汇点
}
if(D == 1)
continue;
for(int k = 0; k < 8; k++)
{
x = i + move2[k][0];
y = j + move2[k][1];
c = y + x * M;
if(judge(x, y))//没有越界
{
if(val[x][y] != '0')
addEdge(b, c, INF);//直接连接
}
else
addEdge(b, sink, INF);//连接汇点
}
if(D == 2)
continue;
for(int k = 0; k < 16; k++)
{
x = i + move3[k][0];
y = j + move3[k][1];
c = y + x * M;
if(judge(x, y))//没有越界
{
if(val[x][y] != '0')
addEdge(b, c, INF);//直接连接
}
else
addEdge(b, sink, INF);//连接汇点
}
if(D == 3)
continue;
for(int k = 0; k < 20;  k++)
{
x = i + move4[k][0];
y = j + move4[k][1];
c = y + x * M;
if(judge(x, y))//没有越界
{
if(val[x][y] != '0')
addEdge(b, c, INF);//直接连接
}
else
addEdge(b, sink, INF);//连接汇点
}
}
}
}
}
bool BFS(int start, int end)//寻找是否存在增广路
{
queue<int> Q;
memset(dist, -1, sizeof(dist));
memset(vis, false, sizeof(vis));
Q.push(start);
dist[start] = 0;
vis[start] = true;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.cap - E.flow > 0)
{
dist[E.to] = dist[u] + 1;
vis[E.to] = true;
if(E.to == end)
return true;
Q.push(E.to);
}
}
}
return false;
}
int DFS(int x, int a, int end)//增广路
{
if(x == end || a == 0) return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next)
{
Edge &E = edge[i];
if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), end)) > 0)
{
E.flow += f;
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0)
break;
}
}
return flow;
}
int Maxflow(int start, int end)
{
int flow = 0;
while(BFS(start, end))
{
memcpy(cur, head, sizeof(head));
flow += DFS(start, INF, end);
}
return flow;
}
int main()
{
int t;
int k = 1;
int ans;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &N, &D);
init();
getMap();
ans = sum - Maxflow(source, sink);
printf("Case #%d: ", k++);
if(ans == 0)
printf("no lizard was left behind.\n");
else if(ans == 1)
printf("1 lizard was left behind.\n");
else
printf("%d lizards were left behind.\n", ans);
}
return 0;
}

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

2015-08-30 17:13:39

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

2014-08-31 22:21:11

#### HDU 2732 Leapin' Lizards

2014-08-31 16:57:03

#### HDU 2732 Leapin' Lizards

2017-02-26 10:35:37

#### 【HDU】 2732 Leapin' Lizards

2016-05-06 16:20:45

#### HDU 2732 —— Leapin' Lizards

2016-05-08 14:51:31

#### hdu 2732 Leapin' Lizards

2015-07-05 20:12:18

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

2017-08-20 18:16:03

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

2015-04-18 21:50:37

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

2014-11-12 15:09:54