zoukankan      html  css  js  c++  java
  • HDU 4012 Paint on a Wall(状压+bfs)

    Paint on a Wall

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
    Total Submission(s): 830    Accepted Submission(s): 325


    Problem Description
    Annie wants to paint her wall to an expected pattern. The wall can be represented as a 2*n grid and each grid will be painted only one color. Annie has a brush which could paint a rectangular area of the wall at a single step. The paint could be overlap and the newly painted color will replace the older one.
      For a given pattern of the wall, your task is to help Annie find out the minimum possible number of painting steps. You can assume that the wall remains unpainted until Annie paint some colors on it.
     
    Input
    There are multiple test cases in the input. The first line contains the number of test cases.
      For each test case, the first line contains the only integer n indicating the length of the wall. (1 <= n <= 8)
      Two lines follow, denoting the expected pattern of the wall. The color is represented by a single capital letter.
      See the sample input for further details.
     
    Output
    For each test case, output only one integer denoting the minimum number of steps.
     
    Sample Input
    3
    3
    ABA
    CBC
     
    3
    BAA
    CCB
     
    3
    BBB
    BAB
     
    Sample Output
    Case #1: 3
    Case #2: 3
    Case #3: 2
     
    n <= 8 。。很经典的搜索。。
    状压 + bfs , 用二进制表示是否到达目标颜色。
    可以发现一个性质就是 没发现一个未达到目标颜色的点可以直接以它作为顶点向左,向右构造新的状态
    写得比较烂, 跑了将近 3 s
     
    #include <bits/stdc++.h>
    using namespace std ;
    typedef pair<int,int> pii ;
    #define X first
    #define Y second
    const int N = 1<<16;
    int n ;
    string s , s2 ;
    struct node {
        int w , st ;
        node(){}
        node( int w , int st ):w(w),st(st){}
        bool operator < ( const node &a ) const {
            return w > a.w ;
        }
    };
    
    void update( int &st , int i , int j , bool tag ) {
        if( ( st & ( 1<<j ) ) && s[j] != s[i] )  st ^= (1<<j) ;
        if( !( st & ( 1<<j ) ) && s[j] == s[i] ) st |= (1<<j) ;
        if( !tag ) return ;
        if( ( st & ( 1<<((j+n)%(2*n)) ) ) && s[(j+n)%(2*n)] != s[i] ) st ^= (1<<((j+n)%(2*n)) );
        if( !( st & ( 1<<(j+n)%(2*n)) ) && s[(j+n)%(2*n)] == s[i] ) st |= (1<<((j+n)%(2*n)) ) ;
    //    cout << j << ' ' << (j+n)%(2*n) << endl ;
    }
    
    void show( int st ) {
        for( int i = 0 ; i < n ; ++i ) if( st&(1<<i) ) cout << '1' ; else cout << '0' ; cout << endl ;
        for( int i = n ; i < 2*n ; ++i ) if( st&(1<<i) ) cout << '1' ; else cout << '0' ; cout << endl ;
    }
    
    int dis[N] , all ;
    void bfs( ) {
        priority_queue<node>que;
        que.push( node(0,0) ) ;
        memset( dis , 0x3f , sizeof dis );
        dis[0] = 0 ;
    //    cout << all << endl ;
        while( !que.empty() ) {
            int uw = que.top().w , ust = que.top().st ;
            que.pop();
            if( dis[ust] < uw ) continue ;
    //        cout << uw << endl ; show( ust ); cout << endl ;
            if( ust == all ) return ;
            int vw = uw + 1 , vst , vst2 ;
    
            for( int i = 0 ; i < n ; ++i ) if( !( ust&(1<<i)) ) {
                vst = ust , vst2 = ust ;
                for( int j = i ; j < n ; ++j ) {         // single line
                    update( vst , i , j , false );
                    update( vst2 , i , j , true );
                    if( vw < dis[vst] ) {
    //                     cout << i << ' ' << vst << endl ;
    //                     show( vst ); cout << endl ;
                         dis[vst] = vw ;
                         que.push( node( vw , vst ) ) ;
                    }
                    if( vw < dis[vst2] ) {
    //                    show( vst2 ); cout << endl ;
                        dis[vst2] = vw ;
                        que.push( node( vw , vst2 ));
                    }
                }
                vst = ust , vst2 = ust ;
                for( int j = i ; j >= 0 ; --j ) {
                    update( vst , i , j , false );
                    update( vst2 , i , j , true );
                    if( vw < dis[vst] ) {
    //                    show( vst ); cout << endl ;
                         dis[vst] = vw ;
                         que.push( node( vw , vst ) ) ;
                    }
                    if( vw < dis[vst2] ) {
    //                    show( vst2 ); cout << endl ;
                        dis[vst2] = vw ;
                        que.push( node( vw , vst2 ));
                    }
                }
            }
    
            for( int i = n ; i < 2 * n ; ++i ) if( !( ust&(1<<i)) ) {
                vst = ust , vst2 = ust ;
                for( int j = i ; j < 2 * n ; ++j ) {         // single line
                    update( vst , i , j , false );
                    update( vst2 , i , j , true );
                    if( vw < dis[vst] ) {
    //                        show( vst ); cout << endl ;
                         dis[vst] = vw ;
                         que.push( node( vw , vst ) ) ;
                    }
                    if( vw < dis[vst2] ) {
    //                    show( vst2 ); cout << endl ;
                        dis[vst2] = vw ;
                        que.push( node( vw , vst2 ));
                    }
                }
                vst = ust , vst2 = ust ;
                for( int j = i ; j >= n ; --j ) {
                    update( vst , i , j , false );
                    update( vst2 , i , j , true );
                    if( vw < dis[vst] ) {
    //                        show( vst ); cout << endl ;
                         dis[vst] = vw ;
                         que.push( node( vw , vst ) ) ;
                    }
                    if( vw < dis[vst2] ) {
    //                    show( vst2 ); cout << endl ;
                        dis[vst2] = vw ;
                        que.push( node( vw , vst2 ));
                    }
                }
            }
        }
    }
    
    int Run() {
        int _ , cas = 1 ; cin >> _ ;
        while( _-- ) {
            cout << "Case #" << cas++ <<": " ;
            cin >> n >> s >> s2 ;
            s += s2 ;
            all = (1<<(n*2))-1;
            bfs();
    //        cout << dis[45] << endl ;
            cout << dis[all] << endl ;
        }
        return 0 ;
    }
    int main() {
    //    freopen("in.txt","r",stdin);
        ios::sync_with_stdio(0);
        return Run();
    }
    View Code
  • 相关阅读:
    asp.net Post Get提交数据转Model实例
    ETL构建数据仓库五步法
    什么是数据仓库-数据仓库的基本概念
    简单的说下什么是数据仓库
    简单理解Socket
    Net中的反射使用入门
    JS---BOM
    jQuery Ajax 全解析
    Ajax与JSON的一些总结
    ASP.NET中验证控件的使用
  • 原文地址:https://www.cnblogs.com/hlmark/p/4353852.html
Copyright © 2011-2022 走看看