点击打开题目链接 

题意:给定一个n×m的0,1矩阵,做多可以对矩阵做k次变换,每次变换只可以将矩阵的某一个元素由0变成1,或从1变成0。

求最小的变换次数使得得到的矩阵满足:每一个连通块都是一个“实心”矩形。

要想清楚的就是最终的矩阵一定是这样的: 

010...

1 0 1 ...

0 1 0 ... 这样的0,1交替的矩阵,这里0,1分别表示一个矩形。

如果 n > k, 那么肯定又一行是没有改变的(为什么?想一想),那么只需要枚举这没有改变的一行,其他行要么和他完全相同,要么完全不同(为什么?想一想)。并且任何两行之间没有任何的其他依赖关系,所以只需要取变换次数最少的一种。

如果 n< k,  那么可以枚举第一列最后的情况,也就是2^ n,这里n< k, 而且k<10。 然后和上面一样就可以了。

附上代码:

 /*************************************************************************
> File Name: 425B.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年04月30日 星期三 21时44分14秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define MAX_N (100 + 5)
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, m, k;
int a[MAX_N][MAX_N]; int
main(void) {
scanf("%d %d %d", &n, &m, &k);
for (int i = ; i < n; i++)
for (int j = ; j < m; j++)
scanf("%d", &a[i][j]);
int ans = ;
if (k < n) {
for (int i = ; i < n; i++) {
int tmp = ;
for (int j = ; j < n; j++) if (i != j) {
int cnt1 = , cnt2 = ;
for (int t = ; t < m; t++) {
if (a[j][t] == a[i][t]) {
cnt1++;
} else {
cnt2++;
}
}
tmp += min(cnt1, cnt2);
}
if (tmp < ans) {
ans = tmp;
}
}
} else {
for (int i = ; i < (<<n); i++) {
int tmp = , b[MAX_N];
for (int j = ; j < n; j++) {
if (i & (<<j)) {
if (a[j][] == ) {
b[j] = ;
tmp++;
} else {
b[j] = ;
}
} else {
if (a[j][] == ) {
b[j] = ;
tmp++;
} else {
b[j] = ;
}
}
}
for (int j = ; j < m; j++) {
int cnt1 = , cnt2 = ;
for (int t = ; t < n; t++) {
if (a[t][j] == b[t]) {
cnt1++;
} else {
cnt2++;
}
}
tmp += min(cnt1, cnt2);
}
if (tmp < ans) {
ans = tmp;
}
}
}
if (ans > k) {
puts("-1");
} else {
printf("%d\n", ans);
} return ;
}

最新文章

  1. 优秀网站看前端 —— 小米Note介绍页面
  2. .Net开发笔记(十四) 基于“泵”的UDP通信(接上篇)
  3. 我的LESS编译方案
  4. MergeRecord_C++中map的使用
  5. 查看别人的css
  6. pupper基线加固
  7. Oracle客户端安装及配置
  8. opencv 操作本地摄像头实现录像
  9. 模拟美萍加密狗--Rockey2虚拟狗(五)
  10. 第三章 视图和URL配置
  11. Activity组件的生命周期
  12. CF615D Multipliers [数学]
  13. [Open Source] RabbitMQ 高可用集群方案
  14. xml概述(1)
  15. 翻译:SET NAMES
  16. [Kubernetes]编排其实很简单
  17. 洛谷P4593 [TJOI2018]教科书般的亵渎(拉格朗日插值)
  18. 从列表和实例来了解python迭代器
  19. am335x 配置 GPIO 为可输入也可输出
  20. JDBC排序数据实例

热门文章

  1. 百钱白鸡(for循环的练习)
  2. mysql基本笔记之一
  3. leetcode 847. Shortest Path Visiting All Nodes 无向连通图遍历最短路径
  4. Windows 的 80 端口被 System 进程占用解决方案
  5. ionic4环境搭建
  6. 安装py3ditles中遇到的问题
  7. table使用display:block时会多出一条边框
  8. Weekly 10 小结
  9. mybatis深入理解(四)-----MyBatis的架构设计以及实例分析
  10. 使用Jedis操作Redis-使用Java语言在客户端操作---对Sorted-Sets的操作