• 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
    ", i, sum == INF ? -1 : sum);
    39     }
    40     return 0;
    41 }

    只有不断学习才能进步!

  • 相关阅读:
    PHP获取时间or戳?
    滤镜灰CSS
    css3 文字渐变色
    除指定区域外点击任何地方隐藏DIV
    margin-top bug 处理方案
    基于Bootstrap好用的瀑布流
    初始数据库
    协程
    粘包及解决方案
    log日志的三种方式
  • 原文地址:https://www.cnblogs.com/wenbao/p/6406617.html
Copyright © 2020-2023  润新知