码迷,mamicode.com
首页 > Windows程序 > 详细

Leapin' Lizards (hdu 2732 最大流)

时间:2015-04-19 09:00:12      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:leapin lizards   hdu 2732   最大流   

Leapin‘ Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1433    Accepted Submission(s): 586


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.
 

Source
 

Recommend
zty   |   We have carefully selected several similar problems for you:  3338 3416 1569 2883 3491 

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

思路:首先将有柱子的点拆成两个,权为承受力,增加超级源点和汇点,‘L’和源点相连,权为1,能一次跳出去的柱子和汇点相连,权为INF,然后能相互到达的柱子之间连边,权为INF。这一题要注意蜥蜴能跳的不只是四个方向,360度任何方向都行,只要两个柱子之间的距离小于d。最后注意输出。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 10005
#define MAXN 50005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Edge{
    int u,v,cap,next;
}edge[MAXN];

int n,d;
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int head[maxn],level[maxn],cur[maxn];
int num,cnt,all;
char MP[25][25];
int mp[25][25];
int number[25][25];

void init()
{
    cnt=0;
    num=0;
    all=0;
    mem(head,-1);
}

void addedge(int u,int v,int w)
{
    edge[num].u=u; edge[num].v=v; edge[num].cap=w; edge[num].next=head[u]; head[u]=num++;
    edge[num].u=v; edge[num].v=u; edge[num].cap=0; edge[num].next=head[v]; head[v]=num++;
}

bool bfs(int s,int t)
{
    mem(level,-1);
    queue<int>Q;
    level[s]=0;
    Q.push(s);
    while (!Q.empty())
    {
        int u=Q.front(); Q.pop();
        for (int i=head[u];i+1;i=edge[i].next)
        {
            int v=edge[i].v;
            if (level[v]==-1&&edge[i].cap>0)
            {
                level[v]=level[u]+1;
                Q.push(v);
            }
        }
    }
    return level[t]!=-1;
}

int dfs(int u,int t,int f)
{
    if (u==t) return f;
    for (int &i=cur[u];i+1;i=edge[i].next)
    {
        int v=edge[i].v;
        if (edge[i].cap>0&&level[v]==level[u]+1)
        {
            int d=dfs(v,t,min(f,edge[i].cap));
            if (d>0)
            {
                edge[i].cap-=d;
                edge[i^1].cap+=d;
                return d;
            }
        }
    }
    return 0;
}

int dinic(int s,int t,int cnt)
{
    int flow=0;
    while (bfs(s,t))
    {
        for (int i=0;i<=cnt;i++) cur[i]=head[i];
        int f;
        while ((f=dfs(s,t,INF))>0)
            flow+=f;
    }
    return flow;
}

int main()
{
    int i,j,k,p,t,cas=0;
    sf(t);
    while (t--)
    {
        init();
        int len;
        sff(n,d);
        for (i=0;i<n;i++)
        {
            scanf("%s",MP[i]);
            len=strlen(MP[i]);
            for (j=0;j<len;j++)
            {
                mp[i][j]=MP[i][j]-'0';
                number[i][j]=++cnt;
            }
        }
        mem(MP,0);
        for (i=0;i<n;i++)
            scanf("%s",MP[i]);
        int t=2*cnt+1;
        for (i=0;i<n;i++)
        {
            for (j=0;j<len;j++)
            {
                if (mp[i][j])
                {
                    if (i<d||j<d||n-i<=d||len-j<=d)
                        addedge(number[i][j]+cnt,t,INF);
                    addedge(number[i][j],number[i][j]+cnt,mp[i][j]); //拆点
                    for (k=0;k<n;k++)
                    {
                        for (p=0;p<len;p++)
                        {
                            int dx=abs(i-k);
                            int dy=abs(j-p);
                            double mm=sqrt(dx*dx*1.0+dy*dy*1.0);
                            if (mm>d) continue;
                            addedge(number[i][j]+cnt,number[k][p],INF);
                        }
                    }
                }
                if (MP[i][j]=='L')
                {
                    addedge(0,number[i][j],1);
                    all++;
                }
            }
        }
        int s=dinic(0,t,t+1);
        int ans=all-s;
        if (!ans)
            printf("Case #%d: no lizard was left behind.\n",++cas);
        else if (ans==1)
            printf("Case #%d: 1 lizard was left behind.\n",++cas);
        else
            printf("Case #%d: %d lizards were left behind.\n",++cas,ans);
    }
    return 0;
}




Leapin' Lizards (hdu 2732 最大流)

标签:leapin lizards   hdu 2732   最大流   

原文地址:http://blog.csdn.net/u014422052/article/details/45116963

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!