zoukankan      html  css  js  c++  java
  • HDU 2209 翻纸牌游戏 状态BFS

    翻纸牌游戏

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    Problem Description
    有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,每当你翻一张纸牌(由正翻到反,或者有反翻到正)时,他左右两张纸牌(最左边和最右边的纸牌,只会影响附近一张)也必须跟着翻动,现在给你一个乱的状态,问你能否把他们整理好,使得每张纸牌都正面朝上,如果可以,最少需要多少次操作。
     
    Input
    有多个case,每个case输入一行01符号串(长度不超过20),1表示反面朝上,0表示正面朝上。
     
    Output
    对于每组case,如果可以翻,输出最少需要翻动的次数,否则输出NO。
     
    Sample Input
    01 011
     
    Sample Output
    NO 1
     

          0^1 = 1 

          1^1 = 0

         [][][][][][][]

    ^   1100000

    ^   0111000

    ^   0011100

    ^   0001110

    ^   0000111

    ^   0000011

    #include <iostream>
    #include <string>
    #include <string.h>
    #include <map>
    #include <stdio.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <math.h>
    #include <set>
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std ;
    typedef long long LL ;
    bool     visited[1<<20] ;
    class App{
      private :
          string   st ;
          int      st_num ;
          int      Len ;
          struct  Node{
              int  X ;
              int  step ;
              friend bool operator < (const Node A ,const Node B){
                   return A.step > B.step  ;
              }
              Node(){}
              Node(int x ,int s):X(x),step(s){}
          };
      public :
          App() ;
          App(string ) ;
          int bfs() ;
    };
    
    App::App(){
    
    }
    
    App::App(string s):st(s){
         memset(visited,0,sizeof(visited)) ;
         st_num = 0 ;
         Len = st.length() ;
         for(int i = 0 ; i < Len ; i++)
            st_num = (st_num<<1) + (st[i]-'0') ;
         visited[st_num] = 1 ;
    }
    
    int App::bfs(){
        priority_queue<Node> que ;
        que.push(Node(st_num,0)) ;
        while(!que.empty()){
            Node now = que.top() ;
            int x = now.X  ,nx ;
            int s = now.step ;
            if(x == 0)
              return s ;
            que.pop() ;
            nx = x^3 ;
            if(!visited[nx]){
                visited[nx] = 1 ;
                que.push(Node(nx,s+1)) ;
            }
            for(int i = 0 ; i <= Len -3 ; i++){
                nx = (7<<i)^x ;
                if(!visited[nx]){
                   visited[nx] = 1 ;
                   que.push(Node(nx,s+1)) ;
                }
            }
            nx = (3<<(Len-2))^x ;
            if(!visited[nx]){
                visited[nx] = 1 ;
                que.push(Node(nx,s+1)) ;
            }
        }
        return -1 ;
    }
    
    int main(){
       string s ;
       while(cin>>s){
          App app(s) ;
          int step = app.bfs() ;
          if(step == -1)
             puts("NO") ;
          else
             printf("%d
    ",step) ;
       }
       return 0 ;
    }
  • 相关阅读:
    騎士宣言
    [洛谷P1631] 序列合并
    [HNOI2006]公路修建问题
    [洛谷2068] 统计和
    [洛谷P1168] 中位数
    【模板】可持久化数组(可持久化线段树/平衡树)
    【模板】可持久化线段树 1(主席树)
    [JSOI2008]最大数maxnumber
    NOI导刊2010提高(06) 黑匣子
    [洛谷1533] 可怜的狗狗
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3461613.html
Copyright © 2011-2022 走看看