[Lesson] algorithm coins array problem

Gold array problem

[Title] Italy

Matrix 01 is given, does the original 01 through two operations such matrix transform matrix to a target 01

Operation 1: line change --01 Flip

Operation 2: Columns transformation - two exchange

 

[Inspection]

Simulation title

 

【answer】

In order to simulate,

The first step: we must find a column as the first column, row by zero or more times transformed into the first column of the matrix as a target.

Step two: through an exchange column, with the objective to obtain the same matrix.

Note greedy exchanged can not reach the minimum number of steps

 


 

 

 

  . 1 #include <bits / STDC ++ H.>
   2  the using  namespace STD;
   . 3   
  . 4  const  int N = 2E2 + 10 ;
   . 5  const  int INF = 0x3f3f3f3f ;
   . 6   
  . 7  //   original array source destination array Aim operating arrays 
  . 8  int the Src [N] [ N], of Aim [N] [N], tmp [N] [N];
   . 9  int n-, m, CNT, Ans;
 10   
. 11  // current column matching judgment operation whether a column in the array and the target array 
12 is  BOOL SameCol ( int a C1, int C2) {
 13 is   
14      for( Int I = . 1 ; I <= n-; I ++ ) {
 15          IF (! Tmp [I] [a C1] = of Aim [I] [C2])
 16              return  to false ;
 . 17      }
 18 is      return  to true ;
 . 19  }
 20 is   
21 is  @ operation 1: operating an array of a row gold flip 
22 is  void Filp ( int row) {
 23 is      for ( int J = 1 ; J <= m; J ++ ) {
 24          tmp [row] [J] ^ = 1 ;
 25      }
 26     ++ CNT ;
 27  }
 28   
29  // Operation 2: Operation of a two switching array 
30  void SwapCol ( int a C1, int C2) {
 31 is   
32      IF (a C1 == C2) return ; // if the same two no processing 
33 is   
34 is      for ( int I = . 1 ; I <= n-; I ++ ) {
 35          the swap (tmp [I] [a C1], tmp [I] [C2]);
 36      }
 37 [      CNT ++ ;
 38 is  }
 39   
40  void the Input () {
 41  
 42     scanf("%d%d",&n,&m);
 43  
 44     for( int i = 1 ; i <= n ; i++ ){
 45         for( int j = 1 ; j <= m ; j++ ){
 46             scanf("%d",&Src[i][j] );
 47         }
 48     }
 49  
 50     for( int i = 1 ; i <= n ; i++ ){
 51         for( intJ = . 1 ; J <= m; J ++ ) {
 52 is              Scanf ( " % D " , & [I] of Aim [J]);
 53 is          }
 54 is      }
 55   
56 is  }
 57 is   
58  void the Solve () {
 59   
60      Ans = INF;
 61 is   
62 is      // select a row as the first row 
63 is      for ( int CoI = . 1 ; CoI <= m; CoI ++ ) {
 64   
65          // before operating the process of updating should cnt, tmp [] [] initialized 
66          CNT = 0 ;
67          the memcpy (tmp, the Src, the sizeof the Src);
 68   
69          // the corresponding row exchange with the first column 
70          SwapCol (CoI, . 1 );
 71 is   
72          // if the current location does not conform, use the "coin flipping operation 1-" such compliance. 
73 is          for ( int I = . 1 ; I <= n-; I ++ ) {
 74              IF (tmp [I] [ . 1 ] of Aim = [I] [! . 1 ])
 75                  Filp (I);
 76          }
 77   
78          // can not use the "coin flipping operation 1-" after fixed because the first column,
 79          // if using a "coin flip 1- operation", the first column will destroy this
80   
81          // *** *** remaining tasks: through the exchange of two, are in accordance with such remaining Aim 
82   
83          BOOL Successful_Col;
 84          // outer loop enumeration Aim array column position 
85          for ( int K = 2 ; K <= m; K ++ ) {
 86   
87              Successful_Col = to false ;
 88   
89              // determine whether the current row has been matched 
90              iF (SameCol (K, K)) {
 91 is                  Successful_Col = to true ;
 92                  Continue ;
 93              }
 94   
95              //Tmp enumerated positions within the cycle of the array columns, the first k columns have been matched, should [k + 1, m] Select 
96   
97              for ( int J = k + . 1 ; J <= m; J ++ ) {
 98                  IF (SameCol ( ! J, K) && SameCol (J, J)) {
 99                      Successful_Col = to true ;
 100                      SwapCol (J, K);
 101                      BREAK ;
 102                  }
 103              }
 104   
105              IF (Successful_Col)! BREAK ;
 106          }
 107   
108          IF (Successful_Col && Ans> cnt ){
109             //printf("Col : %d , %d\n",Col,cnt);
110             Ans = cnt ;
111         }
112     }
113     if( Ans != inf )
114         printf("%d\n",Ans);
115     else{
116         printf("-1\n");
117     }
118 }
119 int main(){
120  
121     Input();
122     Solve();
123  
124     return 0;
125 }
126 /*
127 3 6
128  
129 0 0 0 0 0 1
130 0 0 1 1 1 1
131 0 1 0 0 0 0
132  
133 0 0 0 0 0 1
134 0 1 1 1 0 0
135 0 0 0 0 1 0
136  
137 */
View Code

 

Guess you like

Origin www.cnblogs.com/Osea/p/11515051.html