Paint on a Wall
Title Link: http://acm.split.hdu.edu.cn/showproblem.php?pid=4012
Search + state compression
This problem just began to think that is DP (before the wall is a line of the problem, but it is in the Ashen after the transfer out of the = =), but do not think how to do, see n<=8 data volume so small, should search can be solved, and then want to go up to write ida*, the result has a code how can not find the bug, Always WA.
After the end, with BFS written over, each transfer number is (2n) ^2, the worst case is the 2n layer, so the complexity is (2n) ^ (4n), but after the state de-weight, the state number of up to 2^ (2n) species, the complexity of the [(2n) ^2]*[2^ (2n)], Because n is small, it does not time out. However, if you use the previous ida*, you will almost have the depth less than the deep of all the solution space traversal, must be timed out.
The debug took several hours, and the last discovery was
(Lower&bit[other*n+i]) >0
Was written
(Lower&bit[other*n+i]) = =1
... Don't want to say anything ...
The code is as follows:
1#include <cstdio>2#include <cstring>3#include <queue>4 #defineMet (b) memset (A,b,sizeof (a))5 #defineMKP (State,deep) Make_pair (state,deep)6 #defineX First7 #defineY Second8 #defineN 89 using namespacestd;Tentypedef pair<int,int>P; One intT,n,ans,before,after; A Charmp[2][n+1]; - BOOLstate[1<< -]; - intbit[ -]; the voidinit () { - for(intI=0; i<= -;++i) -Bit[i]= (1<<i); - } + voidBFs () { - intFinish= (1<< (2*n))-1; +Met (state,0); AQueue<p>Q; atQ.push (MKP (0,0)); -state[0]=1; - while(!Q.empty ()) { -P s=Q.front (); Q.pop (); - if(s.x==finish) { -ans=S.y; in return; - } to for(intC=0; c<n;++c) + for(intR=0;r<2;++R) - if((s.x&bit[r*n+c]) = =0){ the CharColor=Mp[r][c]; * intOther= (int)(!R); $ intUpper=s.x;Panax Notoginseng intLower=s.x; - intdeep=S.y; the for(inti=c;i<n;++i) { + if((Upper&bit[r*n+i]) >0&&mp[r][i]!=color) Aupper^=bit[r*n+i]; the Else if((upper&bit[r*n+i]) = =0&&mp[r][i]==color) +upper|=bit[r*n+i]; - if((Lower&bit[other*n+i]) >0&&mp[other][i]!=color) $lower^=bit[other*n+i]; $ Else if((lower&bit[other*n+i]) = =0&&mp[other][i]==color) -lower|=bit[other*n+i]; - if(!State[upper]) { thestate[upper]=1; -Q.push (MKP (upper,deep+1));Wuyi } the inttemp; - if(r==0) temp= (Before&upper) | (after&lower); Wu Else if(r==1) temp= (After&upper) | (before&lower); - if(!State[temp]) { Aboutstate[temp]=1; $Q.push (MKP (temp,deep+1)); - } - } - } A } + } the intMainvoid){ - init (); $scanf"%d",&T); the for(intt=1; t<=t;++t) { thescanf"%d\n",&n); theBefore=after=0; the for(intI=0;i<2*n;++i) { - if(i<n) before|= (1<<i); in ElseAfter|= (1<<i); the } the for(intI=0;i<2;++i) Aboutscanf"%s", Mp[i]); the BFS (); theprintf"Case #%d:", T); theprintf"%d\n", ans); + } -}
Paint on a Wall