zoukankan      html  css  js  c++  java
  • UESTC_Can You Help God Wu CDOJ 582

    There is a boy named God Wu in UESTC ACM team. One day he is asked to finish a task. The task is that he has to paint a wall as the given pattern. The wall can be represented as a 2×n grid and each grid will be painted only one color. You know God Wu is a God, so he has a brush which could paint a rectangular area of the wall at a single step. As we all know, the paint could be overlap and the newly painted color will replace the older one.

    God Wu is so lazy that he always want to finish something in the least steps. At first, the wall was not painted until God Wu paint some colors on it. For a given pattern of the wall, God Wu has to find out the minimum possible number of painting steps to make the wall the same as the given pattern.

    Input

    In the input file, the first line contains the number of test cases.

    For each test case, the first contains only one integer n(1n8) indicating the length of the wall.

    Then follows two lines, denoting the expected pattern of the wall. Every grid is painted by a color which is represented by a single capital letter. You can see the Input Sample for more details.

    Output

    For each test case, output only one integer denoting the minimum number of steps.

    Sample input and output

    Sample InputSample Output
    3
    3
    ABA
    CBC
    3
    BAA
    CCB
    3
    BBB
    BAB
    Case #1: 3
    Case #2: 3
    Case #3: 2

    Source

    UESTC Training for Search Algorithm
     
    解题报告
    暴力枚举涂法,注意二进制的优先级。。TLE
    改进:
    我们设计一个A*算法,g(x) = 目前的涂色次数 , h(x) = 当前的不对颜色种类.
    很容易得到 f(x) = g(x) + h(x)是满足单调性的
    证明:
     设在某步时g(n) = x,h(n) = y;
     在下一步时必然有 g(n') = x + 1 , 且h(n') = y or y-1, 因为你每次只能消除一种颜色,甚至于等于没消
     故 c(n,n') + h(n') - h(n) = 1 + (y or y-1) -y -> min = 0 >=0
     满足相容性,得证
     
      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 #include <algorithm>
      5 #include <queue>
      6 using namespace std;
      7 
      8 char g[18];
      9 int  len,all;
     10 bool vis[65537];
     11 int  target;
     12 int  caculate[17] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536};
     13 typedef struct status
     14 {
     15 int st,step,h;
     16 friend bool operator < (status a,status b)
     17 {
     18  if (a.step + a.h < b.step + b.h)
     19   return false;
     20  if (a.step + a.h == b.step + b.h && a.step  < b.step)
     21   return false;
     22  return true;
     23 }
     24      
     25 };
     26 
     27 priority_queue<status>q;
     28 
     29 int A(status &x)
     30 {
     31  bool ll[26];
     32  int result = 0;
     33  memset(ll,false,sizeof(ll));
     34  for(int i = 0; i < all;++i)
     35   if ( (!(x.st & (1 << i))) && !ll[g[i]-'A'])
     36    {
     37        result ++;
     38        ll[g[i]-'A'] = true;
     39    }
     40 return result;
     41 }
     42 
     43 
     44 int bfs()
     45 {
     46 status start;
     47 start.step = 0,start.st = 0;
     48 start.h = A(start);
     49 q.push(start);
     50 vis[0] = true;
     51 while(!q.empty())
     52 {
     53  status ss = q.top();q.pop();
     54  if (ss.st == target) return ss.step;
     55  for(int i = 0 ; i <len;++i)
     56   for(int j = 1;j <= len-i;++j)
     57    {
     58         char id;
     59         for(int h = 0;h< j;++h)
     60          {
     61         id = g[i+h];
     62         int t = ss.st;
     63         for(int v = 0 ; v < j ;++ v)
     64          if(g[i+v] != id)
     65           t &= ~(1 << (i+v));
     66          else
     67           t |=  (1 << (i+v));
     68         if (!vis[t])
     69          {
     70              status ns;
     71              ns.step = ss.step + 1;
     72              ns.st = t;
     73              ns.h = A(ns);
     74              q.push(ns);
     75              vis[t] = true;
     76          }
     77       }
     78       
     79      for(int h = 0;h< j;++h)
     80          {
     81         id = g[i+h+len];
     82         int t = ss.st;
     83         for(int v = 0 ; v < j ;++ v)
     84          if(g[i+v+len] != id)
     85           t &= ~(1 << (i+v+len));
     86          else
     87           t |=  (1 << (i+v+len));
     88         if (!vis[t])
     89          {
     90              status ns;
     91              ns.step = ss.step + 1;
     92              ns.h = A(ns);
     93              ns.st = t;
     94              q.push(ns);
     95              vis[t] = true;
     96          }
     97       }
     98       
     99       
    100      
    101      for(int h = 0 ; h < 2*j;++ h)
    102       {
    103           if (h >= j)
    104            id = g[h-j+i+len];
    105         else
    106          id = g[i+h];
    107         int t = ss.st;
    108         
    109         for(int v = 0 ; v < j ; ++ v)
    110          {
    111              if (g[i+v] != id)
    112               t &= ~(1 << (i+v));
    113                else
    114                 t |= (1 << (i+v));
    115                
    116                if (g[i+v+len] != id)
    117                 t &= ~(1 << (i+v+len));
    118             else
    119              t |= (1 << (i+v+len));
    120              
    121          }
    122          
    123          if (!vis[t])
    124           {
    125               vis[t] = true;
    126               status ns;
    127               ns.h = A(ns);
    128               ns.step  = ss.step + 1;
    129               ns.st = t;
    130               q.push(ns);
    131           }
    132         } 
    133       
    134    }
    135  
    136 }
    137 return -1;    
    138 }
    139 
    140 
    141 int main(int argc, char * argv[])
    142 {
    143  int T,T2=1;
    144  memset(g,0,sizeof(g));
    145  scanf("%d",&T);
    146  while (T--)
    147   {
    148       while(!q.empty())
    149        q.pop();
    150       scanf("%d",&len);
    151       scanf("%s%s",g,&g[len]);
    152       memset(vis,false,sizeof(vis));
    153       target = caculate[len*2]-1;
    154       all = len*2;
    155       cout << "Case #" << T2++ << ": " << bfs() << endl;
    156   }
    157  return 0;    
    158 }
    No Pain , No Gain.
  • 相关阅读:
    HDU 2112 HDU Today
    HDU 1869 六度分离
    HDU 3790 最短路径问题
    HDU2066 一个人的旅行
    HDU1596 find the safest road(最短路)
    HDU 1254 推箱子(双重bfs)
    HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
    HDU 1045 Fire Net
    数据结构之单链表头插法,尾插法
    Java--会移动、反弹的球
  • 原文地址:https://www.cnblogs.com/Xiper/p/4455122.html
Copyright © 2011-2022 走看看