天天看點

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