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;
}

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

相关文章推荐

hdu 2732 / poj 2711 Leapin' Lizards //拆点最大流

 Leapin' LizardsTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1166 Accepted: 447Descript...
  • hqd_acm
  • hqd_acm
  • 2011年04月29日 19:47
  • 1452

hdu 2732 Leapin' Lizards 网络流 拆点构图

题意:一个矩阵里有n*m个方格,有些方格中有l长的柱子,没相邻的两个柱子间的间距是1m,有些柱子上面有蜥蜴,现在你的目的是要使这些蜥蜴尽可能多的跳出到方格以为,假设蜥蜴每次能跳d远,但是它每离开一根柱...

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

该题是一道比较简单拆点+最大流的题目,因为每个柱子都有一定的寿命,很容易将其对应成流量,那么处理结点容量的一般方法当然是拆点法 。该题反而对边的容量没有要求,为保险起见可以设成无穷大。   该题的思路...

hdu 2732 Leapin' Lizards 【图论-网络流-最大流】

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

HDU 2732 Leapin' Lizards (最大流)

题目链接 题目大意:有一些蜥蜴在一些迷宫里,迷宫的行数为n列数没给,每个蜥蜴每次可以跳跃的最大距离为d,现在给出两个矩阵,第一个描述的是每个位置最多可以被走几次,第二个描述的是哪里有蜥蜴,问最少有几...
  • HHH_go_
  • HHH_go_
  • 2016年12月14日 17:21
  • 270

Leapin' Lizards (hdu 2732 最大流)

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

HDU-2732 (Leapin' Lizards) 网络流

HDU-2732 (Leapin' Lizards) 网络流

hdu 2732 Leapin' Lizards【Edmond-Karp最大流+Bfs建图】

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

HDU2732 Leapin' Lizards(最大流)

建图比较恶心的网络流模型。 有一个n*m(m没有直接给出)的网状迷宫图,还有一堆蜥蜴要从这个迷宫逃跑。蜥蜴站在一个柱子上面,每次跳跃的最大值是d(1≤d≤31\leq d\leq3),而且蜥蜴起跳离...
  • cqbzwja
  • cqbzwja
  • 2016年03月13日 12:45
  • 136

hdu 2732 Leapin' Lizards(最大流)

题意:给出一个n×m的方格,每个方格有可能有柱子,柱子上有可能有蜥蜴,每个柱子有个长度,每个蜥蜴每次能跳的最远的长度为d,每个柱子蜥蜴从这个柱子跳出去会长度会下降。下降到0蜥蜴就不能跳到上面。问最后有...
  • qian99
  • qian99
  • 2014年03月05日 19:40
  • 538
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdoj 2732 Leapin' Lizards 【拆点网路流】 【题目数据坑。。。】
举报原因:
原因补充:

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