天天看点

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

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

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.

题目大意:

给了n和d,表示有n行的图,(列数没给),对于每一个蜥蜴都可以最长跳d的距离,对于每一个柱子有一个限定条件,只能从这里跳出去多少次。给了两张图,一张表示柱子的限定条件,一张给了现在那些柱子上有蜥蜴。问有几只蜥蜴不能跳到安全的地方。

解题思路:拆点、最大流

把每个存在柱子的地点一分为二,即p->p’ 他们的容量即为柱子个数

把蜥蜴能够跳到的地点进行连接

然后把蜥蜴所在的地点与源点相连

最后把蜥蜴能跳到安全地方的地点与汇点相连

AC代码:

# include <iostream>
# include <cstring>
# include <string>
# include <queue>
# include <cmath>

using namespace std;

# define MAXN 1005
# define MAXM 1000005
# define INF 1 << 29

struct EDGE
{
    int to;
    int w;
    int next;
}edge[MAXM];

struct POS
{
    int x;
    int y;
    int w;
}p[MAXN], l[MAXN];

int head[MAXN];
int tot;
int pcount;
int lcount;
int dis[MAXN];
char pnum[][];
char lnum[][];

int min(int a, int b)
{
    return a > b ? b : a;
}

void Init()
{
    tot = ;
    memset(head, -, sizeof(head));
}

void Addedge(int u, int v, int w)
{
    edge[tot].to = v;
    edge[tot].w = w;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].to = u;
    edge[tot].w = ;
    edge[tot].next = head[v];
    head[v] = tot++;
}

int BuideGraph(int s, int t, int row, int col, int d)
{
    int i, j;
    int N = row * col;
    for (i = ; i < N; i++)
    {
        int x = i / col;
        int y = i % col;
        if (pnum[x][y] != '0')
        {
            Addedge(i + , i + N + , pnum[x][y] - '0'); //拆点
        }
    }
    for (i = ; i < N; i++)
    {
        int x = i / col + ;
        int y = i % col + ;
        if ((x <= d) || (y <= d) || (row +  - x <= d) || (col +  - y <= d))
        {
            if (pnum[x - ][y - ] != '0')
                Addedge(i +  + N, t, pnum[x - ][y - ] - '0'); //把能跳出的点与汇点相连
        }
    }

    for (i = ; i < N; i++)
    {
        for (j = i + ; j < N; j++)
        {
            int y1 = i % col;
            int x1 = i / col;
            if (pnum[x1][y1] != '0')
            {
                int x2 = j / col;
                int y2 = j % col;
                if (pnum[x2][y2] != '0')
                {
                    if (abs(y2 - y1) + abs(x2 - x1) <= d)
                    {
                        //把能相连的点进行连接
                        Addedge(i +  + N, j + , pnum[x1][y1] - '0'); 
                        Addedge(j +  + N, i + , pnum[x1][y1] - '0');
                    }
                }
            }
        }
    }
    int sum = ;
    for (i = ; i < N; i++)
    {
        int y = i%col;
        int x = i / col;
        if (lnum[x][y] == 'L')
        {
            //把蜥蜴所在的点与源点相连
            Addedge(, i + , );
            sum++;
        }
    }
    return sum;
}

bool Bfs(int s, int t)
{
    memset(dis, , sizeof(dis));
    queue<int> que;
    dis[s] = ;
    que.push(s);
    while (!que.empty())
    {
        int u = que.front(); que.pop();
        for (int i = head[u]; i != -; i = edge[i].next)
        {
            int v = edge[i].to;
            if (dis[v] ==  && edge[i].w > )
            {
                dis[v] = dis[u] + ;
                if (v == t)
                {
                    return true;
                }
                que.push(v);
            }
        }
    }
    return false;
}

int Dfs(int u, int t, int f)
{
    if (u == t)
    {
        return f;
    }
    int cost = ;
    for (int i = head[u]; i != -; i = edge[i].next)
    {
        int v = edge[i].to;
        int w = edge[i].w;
        if (dis[v] == dis[u] +  && w > )
        {
            int d = Dfs(v, t, min(w, f - cost));
            if (d > )
            {
                edge[i].w -= d;
                edge[i ^ ].w += d;
                cost += d;
                if (cost == f)
                {
                    break;
                }
                else
                {
                    dis[v] = -;
                }
            }
        }
    }
    return cost;
}

int Dinic(int s, int t)
{
    int maxflow = ;
    while (Bfs(s, t))
    {
        maxflow += Dfs(s, t, INF);
    }
    return maxflow;
}

void Solve(int s, int t, int row, int col, int d)
{
    int sum = BuideGraph(s, t, row, col, d);
    int res = sum - Dinic(s, t);
    //printf("%d\n", res);
    if (!res) //当为零时
    {
        printf("no lizard was left behind.\n");
    }
    else if ( == res) //当为1时,lizard为单数
    {
        printf("%d lizard was left behind.\n", res);
    }
    else //其他情况时,lizards为复数
    {
        printf("%d lizards were left behind.\n", res);
    }
}

int main(void)
{
    int T;
    scanf("%d", &T);
    int Ti;
    for (Ti = ; Ti <= T; Ti ++)
    {
        Init();
        int i;
        int n, d;
        int row, col;
        scanf("%d %d", &n, &d);
        row = n;
        for (i = ; i < n; i++)
        {
            scanf("%s", pnum[i]);
        }
        for (i = ; i < n; i++)
        {
            scanf("%s", lnum[i]);
        }
        col = strlen(pnum[]);
        int s = ;
        int t =  * row * col + ;
        printf("Case #%d: ", Ti);
        Solve(s, t, row, col, d);
    }
    return ;
}