D - Leapin' Lizards

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

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(哈密顿距离),给出迷宫的每个柱子承受的次数以及蜥蜴的位置还有弹跳范围d,求最少有多少个蜥蜴跳不出来
 
思路:最大流,建图方法是:对每个柱子的点拆开 i → i' 容量为承受次数,i' 连其他与这个柱子距离小于d的柱子容量为无限大(这里的无限大设10就已经够了,或者承受次数也可以,反正数据不大),如果这根柱子可以跳出边界了那就 i' 连 汇点t 容量无限大或承受次数。 然后对蜥蜴的连边是,源点s连蜥蜴容量为1,蜥蜴连蜥蜴所在的点的柱子 i 容量为1。这样跑一下最大流就得到最大可跳出迷宫的数量,用总数减一下就得到答案。
题目不难,有几个比较坑的点:  
1.  输出 如果答案是0或1,lizard不用加s并且后面用was,否则要有s后面用were
2.  输入数据:  题目说d范围0~3,但discuss上有人说数据有d=4的情况;  题目说保证蜥蜴在的点的柱子承受次数不会是0,但实际上有这种情况,而且如果蜥蜴能立刻跳出边界则算他能逃出,否则算不能逃出。。
坑爹囧rz
 
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int maxn = ;
int cas = ; struct Edge{
int from,to,cap,flow;
Edge() {}
Edge(int a,int b,int c,int d)
{
from=a,to=b,cap=c,flow=d;
}
}; struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
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-);
}
void init(int x)
{
memset(d,,sizeof(d));
edges.clear();
for(int i=;i<=x;i++)
G[i].clear();
}
bool BFS()
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();i++)
{
Edge &e = edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow = , f;
for(int &i=cur[x];i<G[x].size();i++)
{
Edge &e=edges[G[x][i]];
if(d[x]+==d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if(a==) break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
this->s=s; this->t=t;
int flow = ;
while(BFS())
{
memset(cur,,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
}; Dinic dinic;
int n,m,d;
char g1[][],g2[][];
inline int id_p(int x,int y) {return (x*m+y)*;}
inline int id_l(int x,int y) {return x*m+y+;}
inline bool inside(int x,int y) {return x>= && x<=n && y>= && y<=m;}
int s = , t = ;
inline bool canout(int i,int j)
{
// cout<<i<<' '<<j<<endl;
for(int x=i-d;x<=i+d;x++)
for(int y=j-d;y<=j+d;y++)
{
if(abs(x-i)+abs(y-j)>d || (x==i && y==j)) continue;
if(!inside(x,y)) return ;
}
return ;
}
void run()
{
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
scanf("%s",g1[i]+);
for(int i=;i<=n;i++)
scanf("%s",g2[i]+);
m=strlen(g1[]+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
g1[i][j]-='';
dinic.init(t);
int sum = ;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(g1[i][j]==) continue;
int u1 = id_p(i,j);
int u2 = u1^;
dinic.AddEdge(u1,u2,g1[i][j]);
for(int x=i-d;x<=i+d;x++)
for(int y=j-d;y<=j+d;y++)
{
if(abs(x-i)+abs(y-j)>d || (x==i && y==j)) continue;
if(!inside(x,y))
{
dinic.AddEdge(u2,t,g1[i][j]);
goto bk;
}
else
{
dinic.AddEdge(u2,id_p(x,y),g1[i][j]);
}
}
bk:;
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
if(g2[i][j]!='L') continue;
if(g1[i][j]== && canout(i,j)) continue;
sum++;
dinic.AddEdge(s,id_l(i,j),);
dinic.AddEdge(id_l(i,j),id_p(i,j),);
}
int ans = sum - dinic.Maxflow(s,t);
printf("Case #%d: ",cas++); //cout<<sum<<' ';
if(ans==) puts("no lizard was left behind.");
else if(ans==) puts("1 lizard was left behind.");
else printf("%d lizards were left behind.\n",ans);
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}
 

hdu2732 Leapin' Lizards (网络流dinic)的更多相关文章

  1. HDU2732 Leapin' Lizards 网络流 最大流 SAP

    原文链接http://www.cnblogs.com/zhouzhendong/p/8362002.html 题目传送门 - HDU2732 题意概括 给你一个网格,网格上的一些位置上有一只蜥蜴,所有 ...

  2. HDU2732 Leapin' Lizards —— 最大流、拆点

    题目链接:https://vjudge.net/problem/HDU-2732 Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    M ...

  3. HDU2732 Leapin' Lizards

    Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

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

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

  5. HDU2732 Leapin' Lizards 最大流

    题目 题意: t组输入,然后地图有n行m列,且n,m<=20.有一个最大跳跃距离d.后面输入一个n行的地图,每一个位置有一个值,代表这个位置的柱子可以经过多少个猴子.之后再输入一个地图'L'代表 ...

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

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

  7. 【解题报告】 Leapin' Lizards HDU 2732 网络流

    [解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...

  8. HDU2732:Leapin' Lizards(最大流)

    Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  9. Leapin' Lizards

    Leapin' Lizards 题目大意: 在一个网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥蜴逃到边界外. 每行每列中相邻石柱的距离为1,蜥蜴的跳跃距离是d,即蜥蜴 ...

随机推荐

  1. linux rz sz命令

    rz是receive zmodem的缩写,sz是send zmodem的缩写. 传输文件使用的是zmodem协议,所以叫zmodem. r和s是以服务器为主体的,服务器接收就是r,服务器发送就是s.

  2. Webpack探索【10】--- 懒加载详解

    本文主要讲懒加载方面相关内容.

  3. Java实参和形参与传值和传引用

    实参和形参的定义: 形参出现函数定义中,在整个函数体内都可以使用,离开函数则不能使用. 实参出现在主函数中,进入被调函数后,实参变量也不能使用. 形参和实参的功能是做数据传送.发生函数调用时,主调函数 ...

  4. matlab使用usb和gige 网口相机

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 辛苦原创所得,转载请注明出处 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...

  5. NLP数据集大放送,再也不愁数据了!【上百个哦】

    奉上100多个按字母顺序排列的开源自然语言处理文本数据集列表(原始未结构化的文本数据),快去按图索骥下载数据自己研究吧! 数据集 Apache软件基金会公开邮件档案:截止到2011年7月11日全部公开 ...

  6. Java多线程系列 基础篇02 线程的创建和运行

    1.线程创建的方式常用有两种 1. 继承 Thread 类创建线程 2. 实现 Runnable 接口创建线程 2.Thread 和 Runnable的区别 Thread和Runnable的相同点:都 ...

  7. java多线程系列笔记 目录

    基础篇 Java多线程系列 基础篇01 线程的状态 Java多线程系列 基础篇02 线程的创建和运行 Java多线程系列 基础篇03 线程的优先级和守护线程 Java多线程系列 基础篇04 线程中断 ...

  8. LightOJ - 1284 Lights inside 3D Grid —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1284 1284 - Lights inside 3D Grid    PDF (English) Statistic ...

  9. 解决ini-parser解析ini文件中文乱码问题

    rickyah/ini-parser 是一个.net 平台解析ini文件的库,当ini文件中含有中文字符时会乱码. 解决:将文件通过Editplus 等文本编辑工具保存为 utf-8 + bom 格式 ...

  10. 使用C++模拟C#的委托机制

    1. [代码][C/C++]代码 //Event.h  #ifndef _EVENT_H_#define _EVENT_H_class EmptyObject {};template<typen ...