Give a matrix of N * n, 1, and change the smallest number of zeros to 1, so that the sum of the upper, lower, and left elements of each element is an even number.
Idea: enumerate the first row, and the remaining n-1 rows are recursive based on the previous row.
# Include <iostream> using namespace STD; int map [20] [20], TT [20] [20], mi, N, K; int qiuhe (int ii, int JJ) // calculate the sum of (II, JJ) Up and down {int S = 0; If (II-1> = 0) S + = TT [II-1] [JJ]; if (jj-1> = 0) S + = TT [II] [jj-1]; If (JJ + 1 <n) S + = TT [II] [JJ + 1]; return s;} int work (int kk) // The number of successful changes returned.-1 {int I, j; for (I = 1; I <N; I ++) // recursion from the first line for (j = 0; j <n; j ++) {int S = qiuhe (I-1, J ); if (S % 2 = 0) // if he is an even, 0 {If (Map [I] [J] = 1) Return-1 is required; else TT [I] [J] = 0;} else // He 1 {If (Map [I] [J] = 1) TT [I] [J] = 1; else {TT [I] [J] = 1; kk ++ ;}}for (j = 0; j <n; j ++) // determine whether the last row meets the condition if (qiuhe (n-1, J) % 2 = 1) Return-1; return KK;} void out () {int I, J; cout <"***********" <Endl; for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) {If (J! = N-1) cout <TT [I] [J] <""; else cout <TT [I] [J] <Endl ;} cout <"************" <Endl;} void Meiju (int ii) // enumerate the n elements of the 1st rows {If (II = N) {// cout <"K:" <k <Endl; int S = work (k); // If (S> = 0) {cout <"s:" <S <Endl; out ();} if (S> = 0 & mi> S) MI = s;} else {If (Map [0] [II] = 0) {TT [0] [II] = 1; k ++; Meiju (II + 1); TT [0] [II] = 0; k --; meiju (II + 1);} else Meiju (II + 1);} int main () {int T, I, j, Case = 1; CIN> T; while (t --) {mi = 999999; k = 0; CIN> N; for (I = 0; I <n; I ++) for (j = 0; j <n; j ++) CIN> map [I] [J], TT [I] [J] = map [I] [J]; Meiju (0); cout <"case" <Case ++ <":"; if (Mi! = 999999) cout <mi <Endl; else cout <-1 <Endl;} return 0 ;}