zoukankan      html  css  js  c++  java
  • HDU2732 Leapin' Lizards

    Leapin' Lizards

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


    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.
     
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    const int INF = 1e9 ;
    const int N = 2010;
    const int M = 1000010;
    
    bool vis[N];
    int eh[N],et[M],nxt[M],ef[M],ec[M],etot;
    int s , t , n , m;
    int d[N],cur[N];
    char mp1[N][N] , mp2[N][N];
    int colum;
    
    struct node
    {
        int s,e,p;
    
    }ma[M];
    
    
    void init(){
        memset( eh, -1 , sizeof eh) ;
        etot =0 ;
    }
    
    void addedge( int u , int v , int c ){
        et[etot] = v ; nxt[etot] = eh[u]; ef[etot] = 0; ec[etot] = c ; eh[u] = etot++;
        et[etot] = u ; nxt[etot] = eh[v]; ef[etot] = 0; ec[etot] = 0 ; eh[v] = etot++;
    }
    
    bool bfs ()
    {
        memset( vis , 0 , sizeof vis);
        queue< int >que;
        que.push(s) ;
        d[s] = 0;
        vis[s] =1 ;
        while( !que.empty() ) {
            int u = que.front(); que.pop();
            for( int i = eh[ u ] ; ~i ; i = nxt[i] ){
                int v = et[i] , c = ec[i] , f = ef[i];
                if( !vis[v] && c > f){
                    vis[v] = 1 ;
                    d[v] = d[u] + 1;
                    que.push(v);
                }
            }
        }
        return vis[t];
    }
    
    int dfs (int x ,int a)
    {
        if ( x == t || a == 0 ){
            return a ;
        }
        int flow = 0 , F;
        for( int &i = cur[x] ; ~i ; i = nxt[i] ){
            int v = et[i] , c = ec[i] , &f = ef[i];
            if( d[x] + 1 == d[v] && ( F = dfs (v, min( a, c - f))) > 0) {
                f += F;
                ef[ i ^ 1 ] -= F;
                flow += F;
                a -= F;
                if( a == 0 )break;
            }
        }
        return flow;
    }
    
    int MF(){
        int flow = 0 ;
        while( bfs() ){
            memcpy( cur , eh , sizeof eh );
            flow += dfs(s , INF);
        }
        return flow;
    }
    bool check(int x ,int  y){
        if( x - m < 0 || x + m >= n )return 1 ;
        if( y - m < 0 || y + m >= colum )return 1 ;
        return 0 ;
    }
    
    void run()
    {
        init();
        s = N - 1, t = N - 2 ;
        scanf("%d%d",&n,&m);
        int cnt = 0 ;
    
        for(int i = 0 ; i < n ;++i ){scanf("%s",mp1[i]);}
        for(int i = 0 ; i < n ;++i ){scanf("%s",mp2[i]);}
        colum  =  strlen(mp1[0]);
        for(int i = 0 ; i < n ;++i ){
            for( int  j = 0 ; j < colum ; ++j )
                mp1[i][j] -= '0';
        }
    
        for(int i = 0 ; i < n ;++i ){
            for( int  j = 0 ; j < colum ; ++j ){
                if( mp1[i][j] > 0  ){
    
                    addedge( i * colum + j , i * colum + j + colum * n , mp1[i][j]  );
    
                    if( check( i, j ) ){
                        addedge( i*colum +j + colum * n , t,INF );
                    }
    
    
                    for(int ii = i - m ; ii <= i+m ;++ii ){
                        for(int jj = j - m ; jj <= j+m ;++jj ){
                            if( abs(i-ii) + abs(j-jj) <= m && mp1[i][j] > 0 ){
                                addedge(  ii * colum + jj + colum * n ,i * colum + j ,  INF );
                                }
                            }
                        }
                }
    
                if(mp2[i][j]=='L'){
                    cnt ++ ;
                    addedge(s, i*colum+j ,1 );
                }
            }
        }
    
        int ans = MF();
        ans = cnt - ans ;
    
        if(!ans )
            cout <<"no lizard was left behind."<<endl;
        else if(ans == 1)
            cout <<"1 lizard was left behind."<<endl;
        else
            cout<<ans<<" lizards were left behind."<<endl;
    
    
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
    
        int _ , cas = 1 ;
        scanf("%d",&_);
        while(_--){
            printf("Case #%d: ",cas++);
            run();
        }
        return 0;
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    博客园第一篇随笔css3动画(奔跑的小杨)
    Python输出菱形
    Android开发经验总结
    Android中Activity共享变量的另一方法:Application context
    system()与execv()函数使用详解
    Sublime Text2 编译和运行C/C++程序(windows)
    Android View.post(Runnable )
    Android图像处理之Bitmap类
    android中dip、dp、px、sp和屏幕密度
    System.setProperty and System.getProperty
  • 原文地址:https://www.cnblogs.com/hlmark/p/3966401.html
Copyright © 2011-2022 走看看