hdoj 2732 Leapin' Lizards 【拆点网路流】 【题目数据坑。。。】

原创 2015年07月06日 21:51:31


Leapin' Lizards

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


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.
 
 
一共做了3个小时,其中调试一个多小时,最后发现题目数据D可以等于4,不是思路错误。。。
 
 
题意:迷宫着火了,在迷宫里有一个N * M地图,地图上每个坐标对应着一个柱子,每个柱子都有对应的高度,也有一些柱子上停留着蜥蜴。当蜥蜴从一个柱子跳走时,该柱子高度减一,若高度为0说明蜥蜴不能跳到该柱子上面,若蜥蜴跳出这个N*M地图,说明逃脱成功。先输入一个N * M矩阵,表示每个柱子的高度,再输入一个N * M矩阵表示该柱子上是否有蜥蜴,若为L表明柱子上有一个蜥蜴反之说明该柱子是空的。现在给出蜥蜴能够跳跃的最大距离,问最少有多少蜥蜴不能逃脱。
 
提醒:输入数据只给出了N,M需要另外求。

 
全靠自己AC的题目,没看任何题解。
就是错了4次后,看讨论区说D可能为4,加上D = 4需要建的边就过了。这次看讨论区不能算我借助外力吧, 题目说好的1 <= D <= 3呢,无语。
害我模拟1个多小时,原来是题目    D的数据有4的情况。。。

 

 


思路:

先求出每行的柱子数目M。

基础拆点:把每根高度不为0的柱子(i, j) 拆分成 j + i * M 和 400 + j + i * M两点权值为柱子高度。

构建源点汇点:建立超级源点source = 900,超级汇点sink = 1000(题目最多建点数目不会过800)。

基础建边:若坐标(i, j)处有蜥蜴,则构建一条由source 指向 j + i * M 的边且权值为1。 

重头戏:对于高度不为0的柱子 (i, j),枚举其D范围内的坐标 (x, y),若坐标 (x, y) 越界说明从该柱子可以直接跳出去,构建一条由400 + j + i * M 指向 sink 的边且权值为INF;若没有越界且(x, y)坐标处柱子高度大于0,则构建一条由400 + j + i * M 指向 y + x * M的边且权值为INF。



AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 1000+10//最多800个点 
#define MAXM 40000
#define INF 100000+10
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
}edge[MAXM];
int N, M, D;
int head[MAXN], cur[MAXN], top;
int dist[MAXN];
bool vis[MAXN];
char Map[21][21];
char val[21][21];//记录每个位置最多可以跳几次
int source = 900, sink = 1000;//超级汇点 
int sum;//蜥蜴总数目 
bool judge(int x, int y)//判断是否越界 
{
    return x >= 0 && x < N && y >= 0 && y < M; 
}
void init()
{
    top = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[top] = E1;
    head[u] = top++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[top] = E2;
    head[v] = top++;
}
void getMap()
{
    int a, b, c;
    int x, y;
    sum = 0;
    int move1[4][2] = {0,1, 0,-1, 1,0, -1,0};//D为1 可以跳4个点 
    int move2[8][2] = {0,2, 0,-2, 2,0, -2,0, 1,1, 1,-1, -1,1, -1,-1};//D为2 可以跳着8个点加上D为1的4个点  下同 
    int move3[16][2] = {0,3, 0,-3, 3,0, -3,0, 1,2, 1,-2, -1,2, -1,-2, 2,1, 2,-1, -2,1, -2,-1, 2,2, 2-2, -2,2, -2,-2};//D为3
    int move4[20][2] = {0,4, 0,-4, 4,0, -4,0, 1,3, 1,-3, -1,3, -1,-3, 2,3, 2,-3, -2,3, -2,-3, 3,1, 3,-1, -3,1, -3,-1, 3,2, 3,-2, -3,2, -3,-2};
    for(int i = 0; i < N; i++)
    {
        scanf("%s", val[i]);
        M = strlen(val[i]);
        for(int j = 0; j < M; j++)
        { 
            if(val[i][j] == '0') continue; 
            a = j + i * M;
            b = 400 + a; 
            addEdge(a, b, val[i][j]-'0');//拆点建边 
        }
    }
    for(int i = 0; i < N; i++)
    {
        scanf("%s", Map[i]);
        for(int j = 0; j < M; j++)
        {
            if(Map[i][j] == 'L')//有蜥蜴 
            {
                sum++; 
                a = j + i * M;
                addEdge(source, a, 1);//从源点引一条 容量为一的边 
            }
        }
    }
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            if(val[i][j] > '0')
            {
                a = j + i * M;
                b = 400 + a;
                for(int k = 0; k < 4; k++)
                {
                    x = i + move1[k][0];
                    y = j + move1[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')//当前位置必须 可跳跃至少一次 
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else//越界说明可以直接跳出去  
                    addEdge(b, sink, INF);//连接汇点 
                }
                if(D == 1) 
                continue;
                for(int k = 0; k < 8; k++)
                {
                    x = i + move2[k][0];
                    y = j + move2[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else
                    addEdge(b, sink, INF);//连接汇点 
                }
                if(D == 2)
                continue; 
                for(int k = 0; k < 16; k++)
                {
                    x = i + move3[k][0];
                    y = j + move3[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else
                    addEdge(b, sink, INF);//连接汇点 
                }
                if(D == 3)
                continue;
                for(int k = 0; k < 20;  k++)
                {
                    x = i + move4[k][0];
                    y = j + move4[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else
                    addEdge(b, sink, INF);//连接汇点 
                } 
            } 
        }
    }
} 
bool BFS(int start, int end)//寻找是否存在增广路 
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    Q.push(start);
    dist[start] = 0;
    vis[start] = true;
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap - E.flow > 0)
            {
                dist[E.to] = dist[u] + 1;
                vis[E.to] = true;
                if(E.to == end)
                return true;
                Q.push(E.to); 
            }
        }
    } 
    return false;
}
int DFS(int x, int a, int end)//增广路 
{
    if(x == end || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), end)) > 0)
        {
            E.flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0)
            break;
        }
    }
    return flow;
}
int Maxflow(int start, int end)
{
    int flow = 0;
    while(BFS(start, end))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(start, INF, end);
    }
    return flow;
}
int main()
{
    int t;
    int k = 1;
    int ans;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &N, &D);
        init();
        getMap();
        ans = sum - Maxflow(source, sink);
        printf("Case #%d: ", k++);
        if(ans == 0)
        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); 
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Leapin' Lizards (hdu 2732 最大流)

题意:给一个n行的图(不知道有多少列),每个点的数字表示能承受的最大跳跃次数,接下来又是n行,‘L’表示蜥蜴所在地方,给出蜥蜴能跳跃的最大距离d,每次蜥蜴跳离的那根柱子的承受力会减一,若为零了该点的柱...
  • u014422052
  • u014422052
  • 2015年04月18日 21:50
  • 674

HDU 2732 Leapin' Lizards(最大流)

HDU 2732 Leapin' Lizards(最大流) http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:        给你一个网格,网格上的一...
  • u013480600
  • u013480600
  • 2014年08月31日 22:21
  • 1090

【HDU】 2732 Leapin' Lizards

Leapin’ Lizards题目链接 Leapin’Lizards 题目大意 给你两个图,一个用0,1,2,3表示,一个用 L 或 . 表示。其中用L表示的图中,有L的位置表示有蜥蜴,没有L的位...
  • S_Black
  • S_Black
  • 2016年05月06日 16:20
  • 587

HDU-2732 (Leapin' Lizards) 网络流

HDU-2732 (Leapin' Lizards) 网络流
  • ZSGG_ACM
  • ZSGG_ACM
  • 2015年08月20日 00:09
  • 679

[HDU 2732]Leapin' Lizards[拆点][SAP]

题目链接:[HDU 2732]Leapin' Lizards[拆点][SAP] 题意分析: 一张图,n行,m代表每行的长度,d代表蜥蜴每次能跳跃的最大距离。给出两张图,第一张图只含有0~3,代表蜥蜴能...
  • CatGlory
  • CatGlory
  • 2016年03月13日 00:28
  • 206

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

题意: n*m的矩阵(格子),每个格子上可能有一根柱子,长度0-3(0表示没有),每根柱子上可能会有一只蜥蜴,每只蜥蜴最多跳距离d远;每跳一下柱子的高度就会减1。问最少几只蜥蜴跳不出去。 这题难在建...
  • AngOn823
  • AngOn823
  • 2016年10月02日 01:35
  • 178

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

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)...
  • Ever_glow
  • Ever_glow
  • 2017年08月20日 18:16
  • 444

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

题意: 给出一个地图,地图里面有一些青蛙,每个位置有限定承受的次数,如果青蛙跳跃到某个位置,那么这个位置的承受力就减一,如果承受力是0就无法到达这个位置。现在问最少有多少青蛙无法跳出地图。 题解:...
  • My_ACM_Dream
  • My_ACM_Dream
  • 2015年03月30日 19:58
  • 296

hdu 2732 Leapin' Lizards 拆点最大流 isap

题意:n*m的矩阵有一些蜥蜴,每只蜥蜴最远能跳d(0~3)个单位,而每次起跳蜥蜴的站台会少一格血,最开始的时候蜥蜴保证站在有站台(站台初始血量为0~3)的地方。问最多有多少只蜥蜴能跳出矩阵。 建超源超...
  • u014204835
  • u014204835
  • 2015年05月26日 17:27
  • 280

hdu 2732 Leapin' Lizards(拆点dinic)

Leapin' Lizards Problem Description Your platoon of wandering lizards has entered a strange room i...
  • a709743744
  • a709743744
  • 2016年02月28日 23:25
  • 150
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdoj 2732 Leapin' Lizards 【拆点网路流】 【题目数据坑。。。】
举报原因:
原因补充:

(最多只允许输入30个字)