wenbao与常见策略(大白书)
枚举
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=26&problem=2459&mosmsg=Submission+received+with+ID+18787313
01矩阵,使上下左右和为偶数
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 #define ll long long 5 #define REP(i, x, n) for(int i = x; i < n; i++) 6 const int maxn = 17; 7 const int INF = 1<<30; 8 int n, a[maxn][maxn], b[maxn][maxn]; 9 int go(int x){ 10 memset(b, 0, sizeof(b)); 11 REP(i, 0, n){ 12 if(x & (1<<i)) b[0][i] = 1; 13 else if(a[0][i] == 1) return INF; 14 } 15 REP(i, 1, n) REP(j, 0, n){ 16 int sum = 0; 17 if(i > 1) sum += b[i-2][j]; 18 if(j > 0) sum += b[i-1][j-1]; 19 if(j < n-1) sum += b[i-1][j+1]; 20 b[i][j] = sum%2; 21 if(b[i][j] == 0 && a[i][j] == 1) return INF; 22 } 23 int num = 0; 24 REP(i, 0, n) REP(j, 0, n){ 25 if(a[i][j] != b[i][j]) num ++; 26 } 27 return num; 28 } 29 int main(){ 30 int t; 31 scanf("%d", &t); 32 REP(i, 1, t+1){ 33 int sum = INF; 34 scanf("%d", &n); 35 REP(j, 0, n) REP(k, 0, n) scanf("%d", &a[j][k]); 36 int x = 1 << n; 37 REP(j, 0, x) sum = min(sum, go(j)); 38 printf("Case %d: %d\n", i, sum == INF ? -1 : sum); 39 } 40 return 0; 41 }
只有不断学习才能进步!