Template compressed state 2: Number selected from

This is the second stencil questions, then if the Word template in fact and questions on a topic

This question is actually a binary thinking will be reflected more obvious, because if we once put the number we need to be converted into binary, and then one by one to determine, as this would really, really amazing, so I think I speak unlikely to pass, put the bar code directly

So a direct look at the code it

. 1 #include <cstdio>
 2 #include <CString>
 . 3 #include <the cstdlib>
 . 4 #include <algorithm>
 . 5 #include <the cmath>
 . 6 #include <the iostream>
 . 7  the using  namespace STD;
 . 8  int F [ 16 ] [ . 1 < < 15 ]; / * state of compression, the binary * / 
. 9  int a [ 16 ] [ 16 ], V [ . 1 << 15 ], VN, bin [ 16 ], n-;
 10  / * bin number recording power of 2 * / 
11  intmain ()
 12 is  {
 13 is      bin [ . 1 ] = . 1 ; / * pretreatment, th 2 0 * / 
14      for ( int I = 2 ; I <= 15 ; I ++) bin [I] = bin [I- . 1 ] << 1 ; / * pretreated number is a power of 2 * / 
15      Scanf ( " % D " , & n-);
 16      for ( int I = 1 ; I <= n-; I ++) for ( int J = . 1 ; J <= n-; J ++) Scanf ( " % D " , &A [I] [J]);
 . 17      int Maxx = ( . 1 << n-) - . 1 ; / * 2-n-^. 1, positioning calculation * / 
18 is      VN = 0 ; / * each line represents how many topics satisfies conditions the number * / 
19      memset (f, 0 , sizeof (f));
 20      for ( int the X-= 0 ; the X-<= maxx; the X-++) / * including the inability to use, I have only so many, we can split into binary, * / 
21      / * such as (1 for selected) 
 22 is      00.001011 billion left one
 23 is      00.01011 billion above & again, there is no duplicate is determined
 24      if there are duplicate, then 1 is returned, not be 0 
 25     00.00001 billion
 26      so we have to prove this is not simultaneously selected
 27      Also, for example
 28      00.001001 billion left a
 29      00.01001 billion re-& above, is to determine there is no repeat of
 30      results 00000000000 & post
 31      and thus the description of these two is not intersected , so that we can explain both of them are not simultaneously selected 
 32      * / 
33 is      {
 34 is          IF (((X << . 1 ) & X) == 0 ) / * Analyzing two adjacent there are simultaneously selected * / 
35          {
 36              for ( int I = . 1 ; I <= n; I ++) / * each row has n * / 
37 [              {
 38 is                  IF(bin [I] & x) f [ . 1 ] [x] + = A [ . 1 ] [I];
 39                  / * is selected, we put this number value add f this recursive inside
 40                  that is x this state is arranged inside the sum of the number of * / 
41 is              }
 42 is              V [VN ++] = X; / * this is a legitimate case, so we added, again following can for * / 
43 is          }
 44 is      }
 45      for ( int = I 2 ; I <= n-; I ++) / * the first line has finished processing, so that starting from the second row * / 
46 is      {
 47          for ( int P = . 1 ; P <= VN; P ++) / * enumeration per a legal case * /
48          {
 49              int TT = 0 ;
 50              for ( int J = . 1 ; J <= n-; J ++) IF (bin [J] & V [P]) TT + = A [I] [J];
 51 is              / * 
52 is              put the total Canadian tt into which, if we chose this state and this state can meet the cases of:
 53              1. the two are not simultaneously selected adjacent
 54              2. the other eight adjacent directions can not select
 55              tt added to the list on the inside 
 56 is              * / 
57 is              for ( int Q = . 1 ; Q <= VN; Q ++) / * then again for, for our previous row is what kind of arrangement,
 58              we are now the second row, so here is the first line * / 
59             {
 60                  IF ((V [P] & V [Q]) == 0 && (V [Q] << . 1 & V [P]) == 0 && (V [Q] >> . 1 & V [P]) == 0 )
 61                  / * the number 1. the above condition is satisfied
 62                    2,3. and that two adjacent numbers of the above is not checked off * / 
63 is                      F [I] [V [P]] = max ( F [I] [V [P]], TT + F [I- . 1 ] [V [Q]]);
 64                      / * update this value, the original is f [i-1] [v [q]] plus tt (values are now selected this line number)
 65                      again and I had the f [i] [v [p ]] which relatively large * / 
66              }
 67          }
 68      }
 69      int ANS =0;
70     for(int i=1;i<=vn;i++) ans=max(ans,f[n][v[i]]);
71     printf("%d\n",ans);
72     return 0;
73 }

 

Guess you like

Origin www.cnblogs.com/Tristanjiang/p/11458449.html