Leapin' Lizards(拆点+最大流建模)

这是一道关于蜥蜴的题,光是题意就读了许久,这道题可以说是考察的很全面了,个人认为题目很好,可以多敲几遍练练手。

难点:1.读题。

题意大概是这样的,有一只蜥蜴,在一个房子里,输入第一个数T说明有T组。然后输入n和d。n表示行数(没有列数)然后n行。0,1,2,3分别表示对应位置上的柱子最多能承受几次蜥蜴的跳跃。然后再n行。L表示柱子上有蜥蜴。“.”表示没有。(着火了)所以只能在柱子上跳,八个方向都可以(只要距离小于d)问最后有几只蜥蜴跳不出去。

题目真的是,太难了,我菜到读不懂题,看了好久。。

2.建边。建边比较绕人的就在于360*找可以相连的边的过程。

建边要全面,很容易漏掉一些细节。

方法:就是蜥蜴的初始位置与source相连,flow=1;

         然后可以直接跳出去的点(与上下左右最小的距离<=d)与sink(t)相连,flow=1。

         然后点与点之间,互相能到达的相连。flow=inf. 

         然后每个点一定要拆点!!因为每个点是有一定流量的(就是限制的跳的个数)

         然后就是代码了。代码有很多简化的细节,一定要注意简洁。

题目:

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<cmath>
#include<algorithm>
using namespace std;
int n,np,nc,m;
int cnt;
char x;
const int maxE = 200000;
const int maxN = 800000;
const int maxQ = 200000;
const int MAXN=505;
const int MAXM=5105;
const int oo = 1e+9;
const int inf=0x3f3f3f3f;

struct Edge {
   int v;//弧尾
   int c;//容量
   int n;//指向下一条从同一个弧头出发的弧
} edge[maxE];//边组


int adj[maxN], cntE;//前向星的表头
int Q[maxQ], head, tail;//队列
int d[maxN], cur[maxN], pre[maxN], num[maxN];
int source, sink, nv;//sourse:源点,sink:汇点,nv:编号修改的上限


void add(int u, int v, int c) {//添加边
    //正向边
    edge[cntE].v = v;
    edge[cntE].c = c;//正向弧的容量为c
    edge[cntE].n = adj[u];
    adj[u] = cntE++;

    //反向边
    edge[cntE].v = u;
    edge[cntE].c = 0;//反向弧的容量为0
    edge[cntE].n = adj[v];
    adj[v] = cntE++;
}

void rev_bfs () {//反向BFS标号
    memset(num,0,sizeof(num));
    memset(d,-1,sizeof(d));//没标过号则为-1

    d[sink] = 0;//汇点默认为标过号
    num[0] = 1;
    head = tail = 0;
    Q[tail++] = sink;

    while (head != tail) {
        int u = Q[head++];
        for (int i = adj[u]; ~i; i = edge[i].n) {
            int v = edge[i].v;
            if (~d[v]) continue;//已经标过号
            d[v] = d[u] + 1;//标号
            Q[tail++] = v;
            num[d[v]]++;
        }
    }
}

int ISAP() {
    //copy (cur, adj);//复制,当前弧优化
    memcpy(cur,adj,sizeof(cur));
    rev_bfs ();//只用标号一次就够了,重标号在ISAP主函数中进行就行了
    int flow = 0, u = pre[source] = source, i;

    while (d[sink] < nv) {//最长也就是一条链,其中最大的标号只会是nv - 1,如果大于等于nv了说明中间已经断层了。
        if (u == sink) {//如果已经找到了一条增广路,则沿着增广路修改流量
            int f = oo, neck;
            for (i = source; i != sink; i = edge[cur[i]].v) {
                if (f > edge[cur[i]].c){
                    f = edge[cur[i]].c;//不断更新需要减少的流量
                    neck = i;//记录回退点,目的是为了不用再回到起点重新找
                }
            }
            for (i = source; i != sink; i = edge[cur[i]].v) {//修改流量
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
            }
            flow += f;//更新
            u = neck;//回退
        }
        for (i = cur[u]; ~i; i = edge[i].n) if (d[edge[i].v] + 1 == d[u] && edge[i].c) break;
        if (~i) {//如果存在可行增广路,更新
            cur[u] = i;//修改当前弧
            pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else {//否则回退,重新找增广路
            if (0 == (--num[d[u]])) break;//GAP间隙优化,如果出现断层,可以知道一定不会再有增广路了
            int mind = nv;
            for (i = adj[u]; ~i; i = edge[i].n) {
                if (edge[i].c && mind > d[edge[i].v]) {//寻找可以增广的最小标号
                    cur[u] = i;//修改当前弧
                    mind = d[edge[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];//回退
        }
    }

    return flow;
}

void init () {//初始化
    memset(adj,-1,sizeof(adj));
    cntE = 0;
    cnt=0;
}
int main()
{
    int T,logal,d,n;
    //string str[50];
    string str2[50];
    int mp[50][50],number[50][50];
    int numm,i,j,k,p;
    cin>>T;
    logal=0;
    while (T--)
    {
        ++logal;
        init();
        int len;
        numm=0;
        memset(mp,0,sizeof(mp));
        //memset(MP,0,sizeof(MP));
        cin>>n>>d;
        source=0;
        for (i=0;i<n;i++)
        {
            cin>>str2[i];
            len=str2[i].size();
            for (j=0;j<len;j++)
            {
                mp[i][j]=str2[i][j]-'0';
                number[i][j]=++cnt;//这样计数很方便!!
            }
        }
        for (i=0;i<n;i++)
            cin>>str2[i];
        sink=2*cnt+1;
        nv=sink+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)
                        add(number[i][j]+cnt,sink,inf);
                    add(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;
                            add(number[i][j]+cnt,number[k][p],inf);
                        }
                    }
                }
                if (str2[i][j]=='L')
                {
              //      cout<<"hahha"<<endl;
                    add(source,number[i][j],1);
                    numm++;
                }
            }
        }
       // cout<<numm<<endl;
        //cout<<ISAP()<<" **"<<endl;
        int uuu=ISAP();
        int xpp=numm-uuu;
        //cout<<xpp<<"()()"<<endl;
        cout<<"Case #"<<logal<<": ";
        if(xpp==0)
            cout<<"no lizard was left behind."<<endl;
        else if(xpp==1)
            cout<<"1 lizard was left behind."<<endl;
        else if(xpp>1)
            cout<<xpp<<" "<<"lizards were left behind."<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值