关闭

HDU 2732 Leapin' Lizards (拆点棋盘建图+最大流)

389人阅读 评论(1) 收藏 举报
分类:


Leapin' Lizards

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


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.

题意思路转自:http://blog.csdn.net/u013480600/article/details/38964749

题意:

       给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最大跳跃距离是d,如果一只蜥蜴能跳出网格边缘,那么它就安全了.且每个网格有一个最大跳出次数x,即最多有x只蜥蜴从这个网格跳出,这个网格就再也不能有蜥蜴进来了.问你最少有多少只蜥蜴跳不出网格.

分析:

       本题类似POJ3498:

http://blog.csdn.net/u013480600/article/details/38900315

       建图:

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

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

       原题中提到:任意时刻每个柱子上最多只有1只蜥蜴在上面,那么我们上面的解法会不会与这个要求冲突呢?

       不会的,假设有k只蜥蜴能出去,那么一定存在一个符合上面要求的解,使得这k只蜥蜴按顺序出去,在任意时刻每个柱子上最多只有1只蜥蜴.(假设在某个时刻,蜥蜴j想出去,它跳到了柱子h上,但是柱子h上已经有蜥蜴了,那么这样就违反了上面的要求. 其实我们可以这么想,我们为什么不让柱子h上的蜥蜴先按照蜥蜴j以前的逃跑路线出去,然后再让蜥蜴j到柱子h上去替代之前的蜥蜴,那么这样既不违反规则,也得到了解)


这题从各方面暗示你: 我是网络流, 一个蜥蜴跳了一个柱子,这个柱子就下降一块, 这就相当于水流在水管里流动,占有一部分体积一样。。然后汇点连入所有要求的蜥蜴, 然后权值是1,代表只有一个蜥蜴,代表这块水管的水流容量是1, 这种题都要拆点, 权值就是这块方块的权值, 代表这段水管的容量, 然后可以到达目的的东西, 就跟汇点连在一起, 权值赋值为INF,让容量无限大,因为相连或者出去, 没有约束,所以应该容量没有约束...


注意 因为汇点是0,所以前向星的时候,一定要把k赋值成0,唉...说多了都是泪...

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
using namespace std;
const int maxn = 1000;
const int INF = 0x3f3f3f3f;
int head[maxn], cur[maxn], d[maxn], n, m, s, t, k, sum;
char maze[25][25], val[25][25];
struct node
{
    int v, w, next;
}edge[maxn*maxn];
void addEdge(int u, int v, int w)
{
    edge[k].v = v;
    edge[k].w = w;
    edge[k].next = head[u];
    head[u] = k++;
    edge[k].v = u;
    edge[k].w = 0;
    edge[k].next = head[v];
    head[v] = k++;

}
int bfs()
{
    memset(d, 0, sizeof(d));
    d[s] = 1;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u = q.front();
        if(u == t) return 1;
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int to = edge[i].v, w = edge[i].w;
            if(w && d[to] == 0)
            {
                d[to] = d[u] + 1;
                if(to == t) return 1;
                q.push(to);
            }
        }
    }
    return 0;
}
int dfs(int u, int maxflow)
{
    if(u == t) return maxflow;
    int ret = 0;
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int to = edge[i].v, w = edge[i].w;
        if(w && d[to] == d[u]+1)
        {
            int f = dfs(to, min(maxflow-ret, w));
            edge[i].w -= f;
            edge[i^1].w += f;
            ret += f;
            if(ret == maxflow) return ret;
        }
    }
    return ret;
}
int Dinic()
{
    int ans = 0;
    while(bfs() == 1)
    {
//        memcpy(cur, head, sizeof(head));
        ans += dfs(s, INF);
    }
    return ans;
}
int main()
{
    int T, ca = 1;
    scanf("%d", &T);
    while(T--)
    {
        k = 0; sum = 0;
        memset(head, -1, sizeof(head));
        int step;
        scanf("%d%d", &n, &step);
        for(int i = 1; i <= n; i++)
            scanf(" %s", val[i]);
        for(int i = 1; i <= n; i++)
            scanf(" %s", maze[i]);
        m = strlen(val[1]);
        s = 0; t = n*m*2+1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                int in = (i-1)*m+j+1, out = (i-1)*m+j+1+n*m;
                if(maze[i][j] == 'L')
                {
                    addEdge(s, in, 1);
                    sum++;
                }
                if(val[i][j] == '0') continue;
                addEdge(in, out, val[i][j]-'0');
                if(i+step > n || i-step < 1 || j+step >= m || j-step < 0)
                    addEdge(out, t, INF);
                else
                {
                    for(int x = 1; x <= n; x++)
                        for(int y = 0; y < m; y++)
                            {
                                if(val[x][y] == '0') continue;
                                if(x == i && y == j) continue;
                                if(abs(x-i) + abs(y-j) <= step)
                                    addEdge(out, (x-1)*m+y+1, INF);
                            }
                }
            }
        }
        int ans = Dinic();
//        cout << ans << endl;
        if(sum-ans == 0)
            printf("Case #%d: no lizard was left behind.\n", ca++);
        else if(sum-ans == 1)
            printf("Case #%d: 1 lizard was left behind.\n", ca++, sum-ans);
        else
            printf("Case #%d: %d lizards were left behind.\n", ca++, sum-ans);
    }
    return 0;
}


0
0
查看评论

hdu2732 网络流经典建图

http://acm.hdu.edu.cn/showproblem.php?pid=2732 题目大意: 给了n和d,表示有n行的图,(列数没给),对于每一个人都可以最长跳d的距离,对于每一个柱子有一个限定条件,只能从这里跳出去多少次。给了两张图,一张表示柱子的限定条件,一张给了现在那些柱子上有...
  • liujc_
  • liujc_
  • 2016-04-18 13:50
  • 531

HDU 2732 Leapin' Lizards(最大流)

HDU 2732 Leapin' Lizards(最大流) http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:        给你一个网格,网格上的一些位置上有一只蜥蜴,所有蜥蜴的最...
  • u013480600
  • u013480600
  • 2014-08-31 22:21
  • 1088

【HDU】 2732 Leapin' Lizards

Leapin’ Lizards题目链接 Leapin’Lizards 题目大意 给你两个图,一个用0,1,2,3表示,一个用 L 或 . 表示。其中用L表示的图中,有L的位置表示有蜥蜴,没有L的位置表示没有蜥蜴。用数字表示的图中,数字表示当前位置柱子的高度,每次一个蜥蜴可以从一个柱子跳到距离d以...
  • S_Black
  • S_Black
  • 2016-05-06 16:20
  • 586

hdu2732 最大流~

Leapin' Lizards Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 20...
  • JustSteps
  • JustSteps
  • 2013-04-19 19:43
  • 1658

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

该题是一道比较简单拆点+最大流的题目,因为每个柱子都有一定的寿命,很容易将其对应成流量,那么处理结点容量的一般方法当然是拆点法 。该题反而对边的容量没有要求,为保险起见可以设成无穷大。   该题的思路很好想,建议独立编写代码 。 推荐题目: 点击打开链接   ...
  • weizhuwyzc000
  • weizhuwyzc000
  • 2015-08-30 17:13
  • 886

HDU2732 最大流..

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 o...
  • NineFailure
  • NineFailure
  • 2017-04-03 22:47
  • 128

hdu 2732 最大流

/*     最大流  拆点   简单图论  SAP     题意:有一些蜥蜴在一个迷宫里面,求这些蜥蜴还有多少是无论如何都逃不出来的。蜥蜴最远能够跳跃距离D,若某一时刻蜥蜴能跳到迷宫外围,则算>蜥蜴逃出来了。每...
  • keroro520
  • keroro520
  • 2013-07-31 16:08
  • 550

Leapin' Lizards (hdu 2732 最大流)

题意:给一个n行的图(不知道有多少列),每个点的数字表示能承受的最大跳跃次数,接下来又是n行,‘L’表示蜥蜴所在地方,给出蜥蜴能跳跃的最大距离d,每次蜥蜴跳离的那根柱子的承受力会减一,若为零了该点的柱子会塌,现在问最少有多少蜥蜴跳不出去。 思路:首先将有柱子的点拆成两个,权为承受力,增加超级源点和汇...
  • u014422052
  • u014422052
  • 2015-04-18 21:50
  • 674

HDU-2732 (Leapin' Lizards) 网络流

HDU-2732 (Leapin' Lizards) 网络流
  • ZSGG_ACM
  • ZSGG_ACM
  • 2015-08-20 00:09
  • 673

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

题意: n*m的矩阵(格子),每个格子上可能有一根柱子,长度0-3(0表示没有),每根柱子上可能会有一只蜥蜴,每只蜥蜴最多跳距离d远;每跳一下柱子的高度就会减1。问最少几只蜥蜴跳不出去。 这题难在建图,想象一下,柱子高度减1用什么办法表示的好。 发现也正是符合网络流的特质的,柱子高度即容量,如果...
  • AngOn823
  • AngOn823
  • 2016-10-02 01:35
  • 177
    个人资料
    • 访问:222649次
    • 积分:7200
    • 等级:
    • 排名:第3722名
    • 原创:470篇
    • 转载:94篇
    • 译文:0篇
    • 评论:56条
    博客专栏
    最新评论