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只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.

建图:

源点S编号0,网格的每个格子分成两个点i和i+n*m(n和m为网格的行和列数,其实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(列号之差)

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

 
网络流的拆点操作是为了解决有些题目要求每个点的选择次数是有限制的.
比如 id 这个点的被选择次数最大是3,那么我们把它拆成id1 跟 id2两个点分别来表示入id和出id
再在id1和id2之间建一条容量为3的边,这样每次进入id的时候我们就可以让它其实进入id1然后原地跳一下跳到id2这样就控制了某个点的选择的次数
 #include <bits/stdc++.h>

 using namespace std;
const int maxn = ;
const int inf = 0x3f3f3f3f;
char s[][];
char ss[][];
struct Edge
{
int from,to,cap,flow;
Edge (){}
Edge (int f,int t,int c,int fl){from=f,to=t,cap=c,flow=fl;}
};
struct Dinic
{
int n,m,s,t;
vector <Edge> edges;
vector <int> G[maxn];
int cur[maxn];
int dep[maxn];
bool vis[maxn];
void init (int n,int s,int t)
{
this->n=n;this->s=s;this->t=t;
edges.clear();
for (int i=;i<n;++i)
G[i].clear();
}
void addedge (int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool bfs ()
{
queue <int> q;
while (!q.empty()) q.pop();
memset(vis,false,sizeof vis);
vis[s] = true;
dep[s] = ;
q.push(s);
while (!q.empty()){
int u = q.front();
q.pop();
for (int i=;i<G[u].size();++i){
Edge e = edges[G[u][i]];
int v = e.to;
if(!vis[v]&&e.cap>e.flow){
vis[v] = true;
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return vis[t];
}
int dfs (int x,int mi){
if (x==t||mi==) return mi;
int flow = ,f;
for (int &i=cur[x];i<G[x].size();++i){
Edge &e = edges[G[x][i]];
int y = e.to;
if (dep[y]==dep[x]+&&(f=dfs(y,min(mi,e.cap-e.flow)))>){
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
mi-=f;
if (mi==) break;
}
}
return flow;
}
int max_flow ()
{
int ans = ;
while (bfs()){
memset(cur,,sizeof cur);
ans+=dfs(s,inf);
}
return ans;
}
}dinic;
int full_flow;
bool check (int x,int y,int i,int j,int d)
{
if (abs(x-i)+abs(y-j)<=d) return true;
else return false;
}
bool out (int x,int y,int d)
{
if (x-d) return true;
else return false;
}
int main()
{
//freopen("de.txt","r",stdin);
int casee = ;
int T;
scanf("%d",&T);
while (T--){
int n,m,src,dst,d;
full_flow = ;
scanf("%d%d",&n,&d);
for (int i=;i<=n;++i)
scanf("%s",s[i]+);
int len = strlen(s[]+);
m = len;
src = ; dst = *n*m+;
dinic.init(*n*m+,src,dst);
for (int i=;i<=n;++i){
for (int j=;j<=len;++j){
if (s[i][j]-''>){
int id = (i-)*m+j;
dinic.addedge(id,id+n*m,s[i][j]-'');
if (i<=d || i+d>n || j<=d || j+d>m){//这个点能直接跳出去
dinic.addedge(id+n*m,dst,inf);
}
else{
for (int x=;x<=n;++x){
for (int y=;y<=m;++y){
if (x==i&&y==j) continue;
if (check(x,y,i,j,d)){
int id2 = (x-)*m+y;
dinic.addedge(id+n*m,id2,inf);//这个点的出连向能到达点的入
//dinic.addedge(id2+n*m,id,inf);
}
}
}
}
} }
}
for (int i=;i<=n;++i){
scanf("%s",ss[i]+);
for (int j=;j<=len;++j){
if (ss[i][j]=='L'){
full_flow++;
int id = (i-)*m+j;
dinic.addedge(src,id,);
}
}
}
int ans = full_flow-dinic.max_flow();
if(ans==) printf("Case #%d: no lizard was left behind.\n",++casee);
else if(ans==) printf("Case #%d: 1 lizard was left behind.\n",++casee);
else printf("Case #%d: %d lizards were left behind.\n",++casee,ans);
}
return ;
}
 

hdu 2732 Leapin' Lizards (最大流 拆点建图)的更多相关文章

  1. hdu 2732 Leapin' Lizards 最大流 拆点 建图

    题目链接 题意 给定一张网格,格子中有些地方有柱子,有些柱子上面有蜥蜴. 每个柱子只能承受有限只蜥蜴从上面经过.每只蜥蜴每次能走到相距曼哈顿距离\(\leq k\)的格子中去. 问有多少只蜥蜴能走出网 ...

  2. POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)

    POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...

  3. HDU - 2732 Leapin' Lizards (拆点最大流)

    题意:有N*M的矩形,每个格点有一个柱子,每根柱子有高度c,允许蜥蜴经过这根柱子c次,开始有一些蜥蜴在某些柱子上,它们要跳出这个矩形,每步最大能跳d个单位,求最少有多少蜥蜴不能跳出这个矩形. 分析:转 ...

  4. HDU 2732 Leapin' Lizards(最大流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...

  5. HDU 2732 Leapin' Lizards

    网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...

  6. hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005

    废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...

  7. hdu2732 Leapin' Lizards 最大流+拆点

    Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As ...

  8. HDU 2732 Leapin&#39; Lizards(拆点+最大流)

    HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...

  9. HDU-2732-leapin'Lizards(最大流, 拆点)

    链接: https://vjudge.net/problem/HDU-2732 题意: Your platoon of wandering lizards has entered a strange ...

随机推荐

  1. 题解1433. 数码问题 (Standard IO)

    Description Alice有一个N*N的格子,把1-N^2按照从上到下从左到右的顺序填进表格中,允许在表格上进行两种操作: (1) 旋转行——这一行的数向右移动一个位置,而最后一列的数会移到第 ...

  2. Git003--创建版本库

    Git--创建版本库 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 ...

  3. Mac003--Maven安装与环境变量配置

    Mac--Maven安装 一.应用brew安装maven及安装位置 打开终端,输入命令:brew install maven 参考博客:https://www.jianshu.com/p/230e0b ...

  4. python数据类型补充

    四.元组 #为何要有元组,存放多个值,元组不可变,更多的是用来做查询 t=(1,[1,3],'sss',(1,2)) #t=tuple((1,[1,3],'sss',(1,2))) # print(t ...

  5. eclipse的maven配置及本地仓库配置

    一.下载maven并解压 下载地址:http://maven.apache.org/download.cgi 解压后如下: 二.配置环境变量 配置MAVEN_HOME 再path中添加 安装成功 三. ...

  6. 16、NumPy ——字节交换

    NumPy 字节交换 在几乎所有的机器上,多字节对象都被存储为连续的字节序列.字节顺序,是跨越多字节的程序对象的存储规则. 大端模式:指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地 ...

  7. python 丰田经销商

    import requests import json from dbutil.pgsql import PgsqlPipeline from datetime import date headers ...

  8. [fw]Real Mode addressing

    Real Mode 在 real mode 中,memory 的使用被限制在 1 MByte(220 bytes) 內,可用的 address 範圍為 0x00000 ~ 0xFFFFF. 由 mem ...

  9. Little Sub and Mr.Potato's Math Problem (构造法)

    题目传送门Little Sub and Mr.Potato's Math Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB Littl ...

  10. JVM(16)之 双亲委派模型

    开发十年,就只剩下这套架构体系了! >>>   在上一篇博文中,我们知道了如何获得二进制的字节流,并根据获得的字节流去装载一个类.同时也了解到类加载器的存在,每个加载器对应着不同的加 ...