zoukankan      html  css  js  c++  java
  • Ural 1519 Formula 1( 插头dp )

    1519. Formula 1

    Time limit: 1.0 second
    Memory limit: 64 MB

    Background

    Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic games of 20**, it is well-known, that the city will conduct one of the Formula 1 events. Surely, for such an important thing a new race circuit should be built as well as hotels, restaurants, international airport - everything for Formula 1 fans, who will flood the city soon. But when all the hotels and a half of the restaurants were built, it appeared, that at the site for the future circuit a lot of gophers lived in their holes. Since we like animals very much, ecologists will never allow to build the race circuit over the holes. So now the mayor is sitting sadly in his office and looking at the map of the circuit with all the holes plotted on it.

    Problem

    Who will be smart enough to draw a plan of the circuit and keep the city from inevitable disgrace? Of course, only true professionals - battle-hardened programmers from the first team of local technical university!.. But our heroes were not looking for easy life and set much more difficult problem: "Certainly, our mayor will be glad, if we find how many ways of building the circuit are there!" - they said.
    It should be said, that the circuit in Vologda is going to be rather simple. It will be a rectangle N*M cells in size with a single circuit segment built through each cell. Each segment should be parallel to one of rectangle's sides, so only right-angled bends may be on the circuit. At the picture below two samples are given for N = M = 4 (gray squares mean gopher holes, and the bold black line means the race circuit). There are no other ways to build the circuit here.
    Problem illustration

    Input

    The first line contains the integer numbers N and M (2 ≤ N, M ≤ 12). Each of the next N lines contains M characters, which are the corresponding cells of the rectangle. Character "." (full stop) means a cell, where a segment of the race circuit should be built, and character "*" (asterisk) - a cell, where a gopher hole is located.

    Output

    You should output the desired number of ways. It is guaranteed, that it does not exceed 263-1.

    Samples

    inputoutput
    4 4
    **..
    ....
    ....
    ....
    
    2
    
    4 4
    ....
    ....
    ....
    ....
    
    6
    

    插头dp的一条入门。

    http://wenku.baidu.com/view/4fe4ac659b6648d7c1c74633.html

    #include <bits/stdc++.h>
    using namespace std ;
    const int N = 15 ;
    const int M = 30007 ;
    const int MAXN = 1000010;
    int n , m ;
    int maze[N][N] ;
    int code[N] ;
    int ch[N] ;
    int ex , ey ;
    
    struct HASHMAP {
        int head[M] , next[MAXN] , tot  ;
        long long st[MAXN] , f[MAXN] ;
        void init() {
            memset( head , -1 , sizeof head ) ;
            tot = 0 ;
        }
        void push( long long state , long long ans ) {
            int u = state % M ;
            for( int i = head[u] ; ~i ; i = next[i] ) {
                if( st[i] == state ) {
                    f[i] += ans ; return ;
                }
            }
            st[tot] = state ;
            f[tot] = ans ;
            next[tot] = head[u] ;
            head[u] = tot++ ;
        }
    } mp[2] ;
    
    void decode ( int* code , int m , long long st ) {
        for( int i = m ; i >= 0 ; --i ) {
            code[i] = st&7 ;
            st >>= 3 ;
        }
    }
    
    long long encode( int *code , int m ) {
        int cnt = 1 ;
        long long st = 0 ;
        memset( ch , -1 , sizeof ch) ;
        ch[0] = 0 ;
        for( int i = 0 ; i <= m ; ++i ) {
            if( ch[code[i]] == -1 ) ch[ code[i] ] = cnt++ ;
            code[i] = ch[ code[i] ] ;
            st <<= 3 ;
            st |= code[i] ;
        }
        return st ;
    }
    
    void shift( int *code , int m ) {
        for( int i = m ; i > 0 ; --i ) {
            code[i] = code[i-1] ;
        } code[0] = 0 ;
    }
    
    void dpblank( int i , int j , int cur ) {
        int left , up ;
        for( int k = 0 ; k < mp[cur].tot ; ++k ) {
            decode( code , m , mp[cur].st[k] );
            left = code[j-1] ;
            up = code[j] ;
            if( left && up ) {
                if( left == up ) {
                    if( i == ex && j == ey ) {
                        code[j-1] = code[j] = 0 ;
                        if( j == m ) shift(code,m);
                        mp[cur^1].push( encode(code,m) , mp[cur].f[k] );
                    }
                }else {
                    code[j-1] = code[j] = 0 ;
                    for( int t = 0 ; t <= m ; ++t ) {
                        if( code[t] == up )
                            code[t] = left ;
                    }
                    if( j == m ) shift( code,m );
                    mp[cur^1].push(encode(code,m),mp[cur].f[k]) ;
                }
            }
            else if( ( left && ( !up ) ) || ( up && (!left ) ) ) {
                int  t ;
                if( left ) t = left ;
                else t = up ;
                if( maze[i][j+1] ) {
                    code[j-1] = 0 ;
                    code[j] = t ;
                    mp[cur^1].push( encode(code,m) , mp[cur].f[k] ) ;
                }
                if( maze[i+1][j] ) {
                    code[j-1] = t ;
                    code[j] = 0 ;
                    if( j == m ) shift( code , m );
                    mp[cur^1].push(encode(code,m),mp[cur].f[k]);
    
                }
            }
            else {
                if( maze[i][j+1] && maze[i+1][j] ) {
                    code[j-1] = code[j] = 13 ;
                    mp[cur^1].push( encode(code,m),mp[cur].f[k]);
                }
            }
        }
    }
    void dpblock( int i , int j , int cur ) {
        for( int k = 0 ; k < mp[cur].tot ; ++k ) {
            decode( code , m , mp[cur].st[k] );
            code[j-1] = code[j] = 0 ;
            if( j == m ) shift( code , m );
            mp[cur^1].push( encode(code,m) , mp[cur].f[k] );
        }
    }
    
    void Solve() {
        int v = 0 ;
        mp[v].init();
        mp[v].push(0,1);
        for( int i = 1 ; i <= n ; ++i ) {
            for( int j = 1 ; j <= m ; ++j ) {
                mp[v^1].init() ;
                if( maze[i][j] ) dpblank( i , j , v ) ;
                else dpblock( i , j , v );
                v ^= 1 ;
            }
        }
        long long ans = 0 ;
        for( int i = 0 ; i < mp[v].tot ; ++i )
            ans += mp[v].f[i];
        printf("%lld
    ",ans);
    }
    
    char s[20];
    
    int main () {
        while( ~scanf("%d%d",&n,&m) ) {
            ex = 0 ;
            memset( maze , 0 , sizeof maze ) ;
            for( int i = 1 ; i <= n ; ++i ) {
                scanf("%s",s);
                for( int j = 0 ; j < m ; ++j ) {
                    if( s[j] == '.' ) {
                        ex = i , ey = j + 1 ;
                        maze[i][j+1] = 1 ;
                    }
                }
            }
            if( !ex ) { puts("0"); continue ; }
            else Solve();
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    21班考试总结
    性别
    2019.08.20上课笔记2
    2019.08.20上课笔记3
    2019.08.20上课笔记1
    单词2
    数据类和运算符
    2019.08.14单词1
    2019.08.14上课笔记1
    request.get... getHeader 能取得的信息 参数
  • 原文地址:https://www.cnblogs.com/hlmark/p/4371248.html
Copyright © 2011-2022 走看看