Leapin' Lizards

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.
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <queue>
#include<set>
#include<algorithm>
#include<map>


#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scahf("%c",&a);
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 1e3 + 10, M = 1e5 + 10;
namespace DINIC {
    int head[N], ver[M << 1], edge[M << 1], Next[M << 1], d[N];
    int s, t, tot = 1, maxflow;
    queue<int> q;

    inline void init() {
        tot = 1, maxflow = 0;
        ms(head);
        while (!q.empty())q.pop();
    }

    inline void add(int x, int y, int z) {
        ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;
        ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot;
    }

    bool bfs() {
        ms(d);
        while (!q.empty())q.pop();
        q.push(s);
        d[s] = 1;
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            reps(x)
                if (edge[i] && !d[ver[i]]) {
                    q.push(ver[i]);
                    d[ver[i]] = d[x] + 1;
                    if (ver[i] == t)return true;
                }
        }
        return false;
    }

    int dinic(int x, int flow) {
        if (x == t)return flow;
        int rest = flow, k;
        for (int i = head[x]; i && rest; i = Next[i])
            if (edge[i] && d[ver[i]] == d[x] + 1) {
                k = dinic(ver[i], min(rest, edge[i]));
                if (!k)d[ver[i]] = 0;
                edge[i] -= k, edge[i ^ 1] += k, rest -= k;
            }
        return flow - rest;
    }

    int solve() {
        int flow = 0;
        while (bfs())while (flow = dinic(s, INF))maxflow += flow;
        return maxflow;
    }
}
char a[25][25], b[25][25];
int n, m, d, T, ans;
vi dx, dy;

inline void init() {
    n = qr(), d = qr();
    dx.clear(), dy.clear(), DINIC::init(), ans = 0;
    repi(i, -d, d)repi(j, -d, d)
            if (abs(i) + abs(j) <= d && abs(i) + abs(j) > 0) {
                dx.pb(i), dy.pb(j);
            }
    repi(i, 1, n)ss(a[i] + 1);
    repi(i, 1, n)ss(b[i] + 1);
    m = strlen(b[1] + 1);
    DINIC::s = 0, DINIC::t = n * m * 2 + 1;
}

inline bool check(int x, int y) {
    return x >= 1 && x <= n && y >= 1 && y <= m;
}

inline int num(int x, int y, int z) {
    return m * (x - 1) + y + (z == 1 ? 0 : n * m);
}

inline void build() {
    repi(i, 1, n)repi(j, 1, m) {
            if (b[i][j] == 'L')DINIC::add(DINIC::s, num(i, j, 1), 1), ans++;
            if (a[i][j] != '0') {
                DINIC::add(num(i, j, 1), num(i, j, 2), a[i][j] - '0');
                repi(k, 0, dx.size() - 1) {
                    int nx = i + dx[k], ny = j + dy[k];
                    if (check(nx, ny)) {
                        if (a[nx][ny] != '0')
                            DINIC::add(num(i, j, 2), num(nx, ny, 1), a[i][j] - '0');
                    } else DINIC::add(num(i, j, 2), DINIC::t, a[i][j] - '0');
                }
            }
        }
}

int main() {
    T = qr();
    int cse = 0;
    while (T--) {
        init();
        build();
        ans -= DINIC::solve();
        if (ans) {
            if (ans == 1)printf("Case #%d: 1 lizard was left behind.\n", ++cse);
            else printf("Case #%d: %d lizards were left behind.\n", ++cse, ans);
        } else printf("Case #%d: no lizard was left behind.\n", ++cse);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章