POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
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.
Http
POJ:https://vjudge.net/problem/POJ-2711
HDU:https://vjudge.net/problem/HDU-2732
BZOJ:http://www.lydsy.com/JudgeOnline/problem.php?id=1066
Source
网络流,最大流
题目大意
在一个迷宫中有若干只蜥蜴,每只蜥蜴一次可以跳跃给定距离(曼哈顿距离),每一个格子都有一个能承载的上限,最多只能有这么多只蜥蜴跳到这个上面(即以之为中转点)。一个格子在同一时间只能有一只蜥蜴。若蜥蜴能调到迷宫外则称之为逃离出去了。现在求最少几只蜥蜴不能跳出去(即使得最多的蜥蜴逃出迷宫)
解决思路
首先关于每个点的处理,因为每一格能通过的蜥蜴的数量是有限的,所以我们可以拆点,来限制通过每一点的容量。
然后,如果有一个点初始有蜥蜴,则从源点出发连一条边。若某个点可以直接跳到迷宫外面,则连一条边到汇点。
最后对于从一个点可以跳到的点,连容量为无穷大的边。
然后跑一边最大流即可求出获救的蜥蜴数量。
注意输出格式,如no和是否有复数形式。
这里使用Dinic实现最大流,可以参考这篇文章
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
//这两个分别是把二维坐标转成一维数组地址,和把一维数组地址转成坐标输出
#define pos(x,y) ((x-1)*m+y)
#define inpos(x) '['<<x<<']'<<'('<<(x/m)+(int)(!(x%m==0))<<','<<(x-1)%m+1<<')'
const int maxN=2001;
const int maxM=maxN*maxN*2;
const int inf=2147483647;
class Edge
{
public:
int u,v,flow;
};
int n,m;//即题目中的n*m矩阵
int D;//蜥蜴能跳的最长距离
int Lizards;//蜥蜴数
int cnt;
int Head[maxN];
int Next[maxM];
Edge E[maxM];
int depth[maxN];
int cur[maxN];
int Q[maxN];
char str1[maxN][maxN];//存每个格子能跳过的最多蜥蜴数
char str[maxN];
void Add_Edge(int u,int v,int flow);
bool bfs();
int dfs(int u,int flow);
int main()
{
int T;
scanf("%d",&T);
for (int ti=1;ti<=T;ti++)
{
Lizards=0;//多组数据,先初始化
cnt=-1;
memset(Head,-1,sizeof(Head));
scanf("%d%d",&n,&D);
cin>>str1[1];//先读入一个,得出m,方便处理
m=strlen(str1[1]);
for (int i=1;i<=n;i++)
{
for (int j=0;j<m;j++)
if (str1[i][j]!='0')
Add_Edge(pos(i,j+1),pos(i,j+1)+n*m,str1[i][j]-'0');//连接拆点后的两个点
if (i!=n)
cin>>str1[i+1];
}
for (int i=1;i<=n;i++)
{
cin>>str;
for (int j=0;j<m;j++)
if (str[j]=='L')
{
Lizards++;
Add_Edge(0,pos(i,j+1),1);//连接蜥蜴与源点
}
}
/*
for (int i=1;i<=n*m*2;i++)
{
for (int j=Head[i];j!=-1;j=Next[j])
if (E[j].v!=flow)
cout<<inpos(i)<<"->"<<inpos(E[j].v)<<" "<<E[j].flow<<endl;
}
//*/
for (int i=1;i<=n;i++)//连接每一个格子
for (int j=1;j<=m;j++)
if (str1[i][j-1]!='0')
{
if ((i<=D)||(j<=D)||(n-i+1<=D)||(m-j+1<=D))
{
Add_Edge(pos(i,j)+n*m,n*m*2+1,inf);//连接能跳出迷宫的格子与汇点
//continue;
}
for (int x=max(i-D,1);x<=min(i+D,n);x++)//连接能够跳到的格子
for (int y=max(j-D,1);y<=min(j+D,m);y++)
{
if ((x==i)&&(j==y))
continue;
if (str1[x][y-1]=='0')
continue;
if (abs(x-i)+abs(y-j)<=D)//主语是曼哈顿距离
Add_Edge(pos(i,j)+n*m,pos(x,y),inf);
}
}
/*
for (int i=0;i<=n*m*2;i++)
{
for (int j=Head[i];j!=-1;j=Next[j])
if (E[j].flow!=0)
cout<<inpos(i)<<"->"<<inpos(E[j].v)<<" "<<E[j].flow<<endl;
}
//*/
int Ans=0;//求解最大流
while (bfs())
{
for (int i=0;i<=n*m*2+1;i++)
cur[i]=Head[i];
while (int di=dfs(0,inf))
Ans+=di;
}
Ans=Lizards-Ans;//因为要求的是不能跳出去的蜥蜴的个数,所以要用总数减去最大流
printf("Case #%d: ",ti);
if (Ans==0)//注意输出格式,如果是0输出no,如果是1输出单数形式,如果大于1输出复数形式
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);
//printf("%d\n",Ans);
}
return 0;
}
void Add_Edge(int u,int v,int flow)
{
cnt++;
Next[cnt]=Head[u];
Head[u]=cnt;
E[cnt].u=u;
E[cnt].v=v;
E[cnt].flow=flow;
cnt++;
Next[cnt]=Head[v];
Head[v]=cnt;
E[cnt].u=v;
E[cnt].v=u;
E[cnt].flow=0;
}
bool bfs()
{
memset(depth,-1,sizeof(depth));
int h=1,t=0;
Q[1]=0;
depth[0]=1;
do
{
t++;
int u=Q[t];
for (int i=Head[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==-1)&&(E[i].flow>0))
{
depth[v]=depth[u]+1;
h++;
Q[h]=v;
}
}
}
while (t!=h);
if (depth[n*m*2+1]==-1)
return 0;
return 1;
}
int dfs(int u,int flow)
{
if (u==n*m*2+1)
return flow;
for (int i=Head[u];i!=-1;i=Next[i])
{
int v=E[i].v;
if ((depth[v]==depth[u]+1)&&(E[i].flow>0))
{
int di=dfs(v,min(flow,E[i].flow));
if (di>0)
{
E[i].flow-=di;
E[i^1].flow+=di;
return di;
}
}
}
return 0;
}
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)的更多相关文章
- poj 2711 Leapin' Lizards && BZOJ 1066: [SCOI2007]蜥蜴 最大流
题目链接:http://poj.org/problem?id=2711 题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1066 Your p ...
- 【解题报告】 Leapin' Lizards HDU 2732 网络流
[解题报告] Leapin' Lizards HDU 2732 网络流 题外话 在正式讲这个题目之前我想先说几件事 1. 如果大家要做网络流的题目,我在网上看到一个家伙,他那里列出了一堆网络流的题目, ...
- HDU 2732 Leapin' Lizards(拆点+最大流)
HDU 2732 Leapin' Lizards 题目链接 题意:有一些蜥蜴在一个迷宫里面,有一个跳跃力表示能跳到多远的柱子,然后每根柱子最多被跳一定次数,求这些蜥蜴还有多少是不管怎样都逃不出来的. ...
- hdu 2732 Leapin' Lizards(最大流)Mid-Central USA 2005
废话: 这道题不难,稍微构造一下图就可以套最大流的模板了.但是我还是花了好久才解决.一方面是最近确实非常没状态(托词,其实就是最近特别颓废,整天玩游戏看小说,没法静下心来学习),另一方面是不够细心,输 ...
- HDU 2732 Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意: 给出n行的网格,还有若干只蜥蜴,每只蜥蜴一开始就在一个格子之中,并且给出蜥蜴每次的最大跳跃长度d. ...
- Leapin' Lizards(hdu 2732)
Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- K - Leapin' Lizards - HDU 2732(最大流)
题意:在一个迷宫里面有一些蜥蜴,这个迷宫有一些柱子组成的,并且这些柱子都有一个耐久值,每当一只蜥蜴跳过耐久值就会减一,当耐久值为0的时候这个柱子就不能使用了,每个蜥蜴都有一个最大跳跃值d,现在想知道有 ...
- HDU 2732 Leapin' Lizards
网络最大流+拆点.输出有坑!!! #include<cstdio> #include<cstring> #include<string> #include<c ...
- Leapin' Lizards HDU - 2732 (恶心的建图。。)
这道题其实不难...就是建图恶心了点....emm... 题意: 多源多汇 + 拆边 青蛙跳柱子, 每根柱子都有一定的承载能力, 青蛙跳上去之后柱子的承载能力就会减一,跳到边界就能活 跳不到就over ...
随机推荐
- myeclipse、maven、tomcat、jdk技巧和坑【待完善】
公司使用前后不分离或半分离的springmvc + maven ,自己不得不研究研究myeclipse.maven.tomcat等等 开发环境搭建:坑一: Unable to process Jar ...
- Linux-C-Program:makefile
注:本文参照博客:https://blog.csdn.net/initphp/article/details/7692923 1. 概述2. 示例说明2.1 无makefile编译2.2 有makef ...
- SpringBoot日记——ElasticSearch全文检索
看到标题的那一串英文,对于新手来说一定比较陌生,而说起检索,应该都知道吧. 这个ElasticSearch目前我们的首选,他主要有可以提供快速的存储.搜索.分析海量数据的作用.他是一个分布式搜索服务, ...
- 从源码的角度看 React JS 中批量更新 State 的策略(下)
这篇文章我们继续从源码的角度学习 React JS 中的批量更新 State 的策略,供我们继续深入学习研究 React 之用. 前置文章列表 深入理解 React JS 中的 setState 从源 ...
- 我就骂你了,我tm还想打你呢
从地铁出来,一男的抽烟走在我前面,走了一路闻了一路二手烟. 进门,一个园区的,我直接骂了一句:caoni妈的 这哥们瞪着我,我也瞪着他 你骂我干什么 我闻了一路子二手烟 你可以走前面啊 我不走啊 我不 ...
- OC与JS的交互(iOS与H5混编)
大神总结WKWebView的坑:https://mp.weixin.qq.com/s/rhYKLIbXOsUJC_n6dt9UfA 在开发过程中,经常会出现需要iOS移动端与H5混编的使用场景. iO ...
- 在Java中执行Tomcat中startup.bat
问题:更改数据库时,需要重启Tomcat服务器,才能把更改后的数据加载到项目中.于是想每次更改数据库时,都调用Java方法,重启Tomcat 代码: Process process = Runtime ...
- github心得体会 王倩倩 201303014004 计科高职13-1
刚开始接触一门语言软件特别无从下手,尤其还是全英文的,真的是很头疼,注册的时候这个就弄了半天,在网上搜了一下教程然后又结合着老师上课讲的内容自己多做了几遍,也算是对github熟悉了,然后学会操作代码 ...
- 第三个spring冲刺第8天
今天,我们忙于完成精美的背景,还有难度的具体设置,如何达到最理想化,为此我们今天主要是做了开会讨论,但还没有完全确定好结论,明天就应该能做出结论,然后修改后台的难度设置了.
- JSP中properties文件的路径问题
做练习的时候,写了个properties文件,放在src/servlet/目录下,访问文件问题花了点时间折腾,最终得到解决,记下. 环境:eclipse jee oxygen,tomcat 9.0. ...