Leapin' Lizards

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2732

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.

题意:

给出一个n*m的矩阵,输入两次,第一次表示上面的柱子,如果有数值,就代表这根柱子最多只能被几只蜥蜴上来过;第二次如果有L,这代表这个位置有只蜥蜴。

会给出蜥蜴的最大跳跃距离d,当然可以跳多次,只能跳到柱子上。问最多有多少只蜥蜴可以跳出这个矩阵。

题解:

考虑最大流,可以这样建图:

首先将每个柱子拆点,边权为柱子的最大容量。

然后预处理能跳出矩阵的格子(有柱子),让汇点与这些格子的出度点连边,边权为INF;然后遍历这个图,对能互相跳的柱子连边,一个柱子的出度点连续另一个柱子的入读点,边权为INF。

最后让源点与每个蜥蜴连边,权值为1。

最后跑个最大流这题基本就完了~最后要注意下输出...

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define INF 99999999
#define t 1000
using namespace std; const int N = ;
int T,Case,tot,D,n,m;
int head[],d[],cur[];
int map[N][N]; struct Edge{
int v,next,c;
}e[];
void adde(int u,int v,int c){
e[tot].v=v;e[tot].c=c;e[tot].next=head[u];head[u]=tot++;
e[tot].v=u;e[tot].c=;e[tot].next=head[v];head[v]=tot++;
}
int dis(int x1,int y1,int x2,int y2){
return abs(x1-x2)+abs(y1-y2);
}
bool bfs(int S,int T){
memset(d,,sizeof(d));d[S]=;
queue <int > q;q.push(S);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(!d[v] && e[i].c>){
d[v]=d[u]+;
q.push(v);
}
}
}
return d[T]!=;
}
int dfs(int s,int a){
int flow=,f;
if(s==t || a==) return a;
for(int &i=cur[s];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]!=d[s]+) continue ;
f=dfs(v,min(a,e[i].c));
if(f){
e[i].c-=f;
e[i^].c+=f;
flow+=f;
a-=f;
if(a==) break;
}
}
if(!flow) d[s]=-;
return flow;
}
int Dinic(){
int max_flow=;
while(bfs(,t)){
for(int i=;i<=t;i++) cur[i]=head[i];
max_flow+=dfs(,INF);
}
return max_flow;
}
int main(){
scanf("%d",&T);
while(T--){
Case++;
tot=;memset(head,-,sizeof(head));
scanf("%d%d",&n,&D);
char s[N];
for(int i=;i<=n;i++){
scanf("%s",s);
m = strlen(s);
for(int j=;j<m;j++){
if(s[j]==) continue ;
map[i][j+]=s[j]-'';
if(i<=D || n-i+<=D || j+<=D || m-j<=D){
int u = (i-)*m+j+;
adde(u+,t,map[i][j+]);
}
}
}
int sum = ;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(map[i][j]) adde((i-)*m+j,(i-)*m+j+,map[i][j]);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(map[i][j]){
for(int _i=max(,i-);_i<=min(n,i+);_i++){
for(int _j=max(,j-);_j<=min(m,j+);_j++){
if(!map[_i][_j] || (i==_i && j==_j)) continue ;
if(dis(i,j,_i,_j)<=D)
adde((i-)*m+j+,(_i-)*m+_j,INF);
}
}
}
}
}
for(int i=;i<=n;i++){
scanf("%s",s);
for(int j=;j<=m;j++)
if(s[j-]=='L') adde(,(i-)*m+j,),sum++;
}
int cnt=Dinic();
int left = sum-cnt;
if(!left) printf("Case #%d: no lizard was left behind.\n",Case);
else if(left==) printf("Case #%d: 1 lizard was left behind.\n",Case);
else printf("Case #%d: %d lizards were left behind.\n",Case,left);
}
return ;
}

HDU2732:Leapin' Lizards(最大流)的更多相关文章

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

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

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

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

  3. HDU2732 Leapin' Lizards 最大流

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

  4. hdu2732 Leapin' Lizards (网络流dinic)

    D - Leapin' Lizards Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  5. HDU2732 Leapin' Lizards

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

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

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

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

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

  8. hdu 2732 Leapin' Lizards (最大流 拆点建图)

    Problem Description Your platoon of wandering lizards has entered a strange room in the labyrinth yo ...

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

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

随机推荐

  1. [转载]三小时学会Kubernetes:容器编排详细指南

    原翻译by梁晓勇 原英文:Learn Kubernetes in Under 3 Hours: A Detailed Guide to Orchestrating Containers 我很奇怪,为什 ...

  2. 理解Canvas像素边界

    大家看下面的例子 var context = document.getElementById('canvas').getContext('2d'); context.lineWidth = 1; co ...

  3. kudu是什么

    Apache Kudu Overview 建议配合[Apache Kudo]审阅本文(http://kudu.apache.org/overview.html) 数据模式 Kudo是一个列式存储的用于 ...

  4. 分分钟搞定redis

    随着科技不断的发展,使用到的技术也是更新换代,大家都知道当一个程序用户量上来之后,必然是要做数据缓存的,那么如何去实现的呢,在之前我们一直使用memcache去做数据缓存,现在众所周知主流的缓存技术已 ...

  5. BI领军者之一Tableau试用浅谈

    下图是最新的Gartner BI Magic Quadrant,其中领军者之一的Tableau表现的异常突出,执行力象限上直接甩开其它产品一条街,前瞻性象限上略微超越了MSBI,怀着无比的好奇心,特意 ...

  6. window平台下使用python虚拟环境

    第一步:安装virtualenv模块 安装virtualenv模块,使用pip install C:\Users\wangjun>pip install virtualenv 第二步:创建虚拟环 ...

  7. tensorflow nmt基本配置(tf-1.4)

    随着tensorflow的不断更新,直接按照nmt的教程搭建nmt环境会报错的...因此,需要一些不太好的办法来避免更多的问题出现.tensorflow看来在ubuntu和debian中运行是没有问题 ...

  8. 《Deep Learning》第二章 线性代数 笔记

    第二章 线性代数 2.1 名词 标量(scalar).向量(vector).矩阵(matrix).张量(tensor) 2.2 矩阵和向量相乘 1. 正常矩阵乘法: 2. 向量点积: 3. Hadam ...

  9. week12第二轮迭代任务分配forZ.XML

    Z.XML第二轮迭代任务初步分配新鲜出炉,请关注! 以上便是任务分配列表,队员们会按照进度每天更改任务进度 当然,根据敏捷开发的方法,我们将在开发过程中根据情况迅速调整任务分配,以适应当时问题. Z- ...

  10. 将Excel表中的数据导入MySQL数据库

    原文地址: http://fanjiajia.cn/2018/09/26/%E5%B0%86Excel%E8%A1%A8%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E5% ...