最大流-HDU-2732-Leapin' Lizards

本文探讨了深度学习技术在音视频处理、AR特效、AI音视频处理等领域的应用,包括图像处理、音视频编解码、自然语言处理、区块链等方面的技术创新与实践。

Leapin’ Lizards
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2121 Accepted Submission(s): 884

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*m的网格区域里有很多的柱子和蜥蜴,蜥蜴只能在柱子间跳跃,并且蜥蜴要跳出这个网格区域,蜥蜴有一个统一的最大跳跃长度d,同时每个柱子有一个最大能承受的跳跃次数,现在已知每个柱子能够承受的跳跃次数和蜥蜴初始所在的位置,求最终最少会剩多少只蜥蜴在图中跳不出去。


题解:
题目所求即是最多能够有多少只蜥蜴跳出这个区域,那么用网络流建图即可。
将每个柱子拆为入点和出点,上界为柱子的耐久度(即能够承受的蜥蜴跳跃次数),从源点连上界为1的边到每个初始状态有蜥蜴的柱子,然后将在跳跃范围内的所有柱子都互相连上无上界的边,最后再将与边界相距不超过跳跃范围的柱子都与汇点相连,跑一次最大流即可。
需要注意的是输出,有点小坑,剩余0的时候要用“no”代替数字0,剩余0和1的时候应该是“was”而不是“were”,同时“lizard”不能加s,好一道英语题。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <utility>
#define LL long long int
using namespace std;

int T;
int N,M,MaxDis;
int moved[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int num[25][25];
bool zhu[25][25];
const int MAXN = 100010;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
    int to,next,cap,flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init()
{
    tol = 0;
    memset(head,-1,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to = v;
    edge[tol].cap = w;
    edge[tol].next = head[u];
    edge[tol].flow = 0;
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = rw;
    edge[tol].next = head[v];
    edge[tol].flow = 0;
    head[v]=tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u = start;
    pre[u] = -1;
    gap[0] = N;
    int ans = 0;
    while(dep[start] < N)
    {
        if(u == end)
        {
            int Min = INF;
            for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])
                if(Min > edge[i].cap - edge[i].flow)
                    Min = edge[i].cap - edge[i].flow;
            for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])
            {
                edge[i].flow += Min;
                edge[i^1].flow -= Min;
            }
            u = start;
            ans += Min;
            continue;
        }
        bool flag = false;
        int v;
        for(int i = cur[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
            {
                flag = true;
                cur[u] = pre[v] = i;
                break;
            }
        }
        if(flag)
        {
            u = v;
            continue;
        }
        int Min = N;
        for(int i = head[u]; i != -1; i = edge[i].next)
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]])return ans;
        dep[u] = Min+1;
        gap[dep[u]]++;
        if(u != start) u = edge[pre[u]^1].to;
    }
    return ans;
}
inline bool check(int x,int y)
{
    if(x<=0 || x>N || y<=0 || y>M)
        return 0;
    return 1;
}
inline double dist(int x1,int y1,int x2,int y2) {return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));}
inline int GetNum(int x,int y)  {return (x-1)*M+y;}
int main(void)
{
    string str;
    int total=0,now=0;
    ios::sync_with_stdio(false);
    cin >> T;
    while(T--)
    {
        total=0;
        now++;
        cin >> N >> MaxDis;
        cin >> str;
        init();
        M=str.length();
        for(int i=1;i<=M;i++)
        {
            num[1][i]=str[i-1]-'0';
            if(num[1][i]>0)
                    zhu[1][i]=1;
                else
                    zhu[1][i]=0;
        }
        for(int i=2;i<=N;i++)
        {
            cin >> str;
            for(int j=1;j<=M;j++)
            {
                num[i][j]=str[j-1]-'0';
                if(num[i][j]>0)
                    zhu[i][j]=1;
                else
                    zhu[i][j]=0;
            }
        }
        for(int i=1;i<=N;i++)
        {
            cin >> str;
            for(int j=1;j<=M;j++)
            {
                int tmp=GetNum(i,j);
                addedge(tmp,tmp+N*M,num[i][j]);
                if(str[j-1]=='L')
                    addedge(0,tmp,1),total++;
            }
        }
        for(int x1=1;x1<=N;x1++)
        {
            for(int y1=1;y1<=M;y1++)
            {
                if(!zhu[x1][y1])
                    continue;
                int tmp1=GetNum(x1,y1);
                if(x1<=MaxDis || N-x1<MaxDis || y1<=MaxDis || M-y1<MaxDis)
                    addedge(tmp1+N*M,2*N*M+1,INF);
                for(int x2=1;x2<=N;x2++)
                {
                    for(int y2=1;y2<=M;y2++)
                    {
                        if(!zhu[x2][y2])
                            continue;
                        int tmp2=GetNum(x2,y2);
                        if(tmp1!=tmp2 && dist(x1,y1,x2,y2)<=(double)MaxDis)
                            addedge(tmp1+N*M,tmp2,INF);
                    }
                }
            }
        }
        int out=total-sap(0,N*M*2+1,N*M*2+2);
        cout << "Case #" << now << ": ";
        if(out)
            cout << out;
        else
            cout << "no";
        if(out<=1)
            cout << " lizard was left behind." << endl;
        else
            cout << " lizards were left behind." << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值