[CodeVs1050] Checkerboard staining 2 (state compression DP)

Source: Internet
Author: User

The main topic: there is a 5*n (≤100) board, some of the checkerboard has been dyed black, for the minimum number of lattice staining, all the black can be connected together.

This problem card I 1h, wrote 2.6k code, Qingming homework a lump has not done ah ... Before always thought that this is the plug DP, the results of today's look found not >_<, although still a pressure DP.

Because there are only 5 columns, so there are at most 3 black Unicom blocks per line, that is, black, white, black, white, black, the other case is less than 3 unicom block, so we can put the Unicom block label. 0 means white, 1 means 1th unicom blocks, 2 and 3 are the same, so we can use 4 to represent the state of each row. The next line of black if the black connection with the previous line, its Unicom block number is the previous line with its connected black Unicom block number. For each state, enumerate the next layer to black which white, then transfer, the last layer of all black for the same unicom block to update the answer. So this problem is done, the idea is very simple, but it is really a bit of trouble to write ... But it must have been me too weak to write so long = ...

The code is as follows:

typenode=Array[0..6] ofLongint;varF:Array[0.. -,0.. -] ofLongint; A:Array[0.. -] ofLongint; H:Array[0..102400,1..2] ofLongint;  S,t:node;  Ch:char; N,i,j,ans:longint;functionlowbit (x:longint): Longint;begin  ifx=0  ThenExit0); Exit (Lowbit (x-(X and-X) +1);End;procedureChangevara:node;sum1,sum2:longint);varI:longint;begin   fori:=1  to 5  Do  ifA[i]=sum1 Then  beginA[i]:=sum2; if(a[i-1]<>0) and(a[i-1]<Ten) ThenChange (a,a[i-1],sum2); if(a[i+1]<>0) and(a[i+1]<Ten) ThenChange (a,a[i+1],sum2); End;End;procedureWorkvara:node);varI,sum:longint;beginsum:=Ten;  fori:=1  to 5  Do  if(a[i]<>0) and(a[i]<Ten) Then  beginInc (SUM);  Change (a,a[i],sum); End;  fori:=1  to 5  Do  ifA[i]>0  ThenDec (a[i),Ten);End;procedureBFS;varI,j,k,yy,front,rear:longint; Flag:boolean;beginh[1][1]:=0; h[1][2]:=0; Front:=0; rear:=1;  whileFront<rear Do  beginInc (front); YY:=h[front][2];  fori:=1  to 5  Do    beginS[i]:=h[front][2] and 3; h[front][2]:=h[front][2]>>2; End; h[front][2]:=yy; ifh[front][1]=n Then    beginflag:=true;  fori:=1  to 5  Do      ifS[i]>1  Thenflag:=false; ifFlag Then      ifans>f[h[front][1],h[front][2]] Thenans:=f[h[front][1]][h[front][2]];    Continue End;  fori:=0  to(1<<5)-1  Do    ifI anda[h[front][1]+1]=0  Then    begin       forj:=1  to 5  DoT[j]:= (((a[h[front][1]+1]+i) >> (J-1)) and 1) * (j+3); K:=0;  forj:=1  to 5  Do      if(s[j]>0) and(t[j]>0) Then      beginT[j]:=S[j]; K:=kor(1<<S[j]); End; Flag:=true;  forj:=1  to 5  Do      if(s[j]>0) and(k and(1&LT;&LT;S[J]) =0) Thenflag:=false; ifFlag=false Thencontinue;      Work (t); K:=0;  forj:=5 Downto 1  DoK:=k<<2+T[j]; iff[h[front][1]+1,k]>1000000000  Then      beginInc (rear); h[rear][1]:=h[front][1]+1; h[rear][2]:=K; End; iff[h[front][1]+1][k]>f[h[front][1]][h[front][2]]+lowbit (i) Thenf[h[front][1]+1][k]:=f[h[front][1]][h[front][2]]+lowbit (i); End; End;End;beginREADLN (n);  fori:=1  toN Do  begin     forj:=1  to 5  Do    beginread (CH); ifCh='1'  ThenA[i]:=a[i]or 1<< (J-1); End;  READLN; End;  whileN>0  Do  begin    ifA[n]>0  ThenBreak ;  Dec (n); End; ifn=0  Then  beginWriteln (0);  Halt End; Fillchar (F,sizeof (f), the); t[0]:=0; t[6]:=0; Ans:=Maxlongint; f[0,0]:=0;  BFs Writeln (ans);End.
View Code

It seems that my code is very short in the Pascal party ... And my code is still in the machine room god Ben Spit Groove begin to hit the next line, such as the length of the code in the last column

[CodeVs1050] Checkerboard staining 2 (state compression DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.