当前位置:网站首页>翻转(DAY 97)
翻转(DAY 97)
2022-08-02 04:22:00 【张学恒】
1:题目
给定一个 M×N 的 01 矩阵。
你需要选择其中一些元素,并对选择的元素进行翻转操作。
翻转操作是指将所选元素以及与其上下左右相邻的元素(如果有)进行翻转(0 变 1,1 变 0)。
我们希望最终矩阵变为一个全 0 矩阵,并且选择进行翻转操作的元素数量尽可能少。
输出最佳翻转方案。
输入格式
第一行包含整数 M,N。
接下来 M 行,每行包含 N 个整数,每个整数为 0 或 1。
输出格式
共 M 行,每行包含 N 个整数,其中第 i 行第 j 列的整数,表示第 i 行第 j 列元素的翻转次数。
如果翻转操作次数最少的操作方案不唯一,则输出字典序最小的方案。
如果不存在合理方案,则输出 IMPOSSIBLE。
数据范围
1≤M,N≤15
输入样例:
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
输出样例:
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
2:代码实现
#include <bits/stdc++.h>
using namespace std;
#define debug(x...) cerr << (#x) <<" -> ", err(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define endl '\n'
// #define int long long
void err() {
cerr << '\n';
}
template<class T, class... Ts>
void err(const T &A, const Ts &... other) {
cerr << (A) << ' ';
err(other...);
}
using PII = pair<int, int>;
using ll = long long;
const int N = 2e6 + 10, INF = 0x3f3f3f3f;
const int mod = 998244353;
int dx[5] = {
-1, 0, 1, 0, 0}, dy[5] = {
0, 1, 0, -1, 0};
void solve() {
int n, m, res = INF;
cin >> n >> m;
vector<vector<int>> g(n, vector<int>(m)), b(n, vector<int>(m));
vector<vector<int>> ans(n, vector<int>(m, 0));
for (auto &x : g)
for (auto &y : x)
cin >> y;
auto change = [&](int x, int y) {
for (int i = 0; i < 5; i ++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || (nx >= n) || ny < 0 || (ny >= m)) continue;
b[nx][ny] ^= 1;
}
};
for (int i = 0; i < (1 << m); ++ i) {
// 枚举第一行的所有方案
b = g;
int cnt = 0;
vector<vector<int>> o(n, vector<int>(m, 0));
for (int j = 0; j < m; ++ j)
if (i >> j & 1)
change(0, j), o[0][j] = 1, cnt ++;
for (int j = 0; j < n - 1; ++ j)
for (int k = 0; k < m; ++ k)
if (b[j][k])
change(j + 1, k), o[j + 1][k] = 1, cnt ++;
bool flag = true;
for (int j = 0; j < m; ++ j)
if (b[n - 1][j]) flag = false;
if (flag and cnt < res) res = cnt, ans = o;
}
if (res == INF) {
cout << "IMPOSSIBLE" << endl;
return;
}
for (auto x : ans) {
for (auto y : x) {
cout << y << ' ';
}
cout << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int ___ = !(0 ^ 0);
// cin >> ___;
for (; ___ -- ;) solve();
return 0;
}
边栏推荐
- 论文速读:Homography Loss for Monocular 3D Object Detection
- 力扣练习——39 正方形数组的数目
- CODESYS指针型变量编程应用(配方)
- 无主复制系统(1)-节点故障时写DB
- 学内核之五:问题一,关于上下文切换
- Arduino框架下STM32F1/F4系列HID模式程序烧录教程
- STM32 OLED显示屏--SPI通信知识汇总
- 2022-08-01:以下go语言代码输出什么?A:panic;B:5;C:6;D:编译错误。 package main import ( “fmt“ ) func main() {
- MySQL存储函数详解
- Live | 7.30 ApacheCon Asia 2022 IOT/IIOT topic, IoTDB PMC Qiao Jialin as the producer
猜你喜欢

CaDDN code debugging

安装部署 Kubernetes 仪表板(Dashboard)

洛谷P2437蜜蜂路线

多主复制下处理写冲突(4)-多主复制拓扑

数学建模学习(76):多目标线性规划模型(理想法、线性加权法、最大最小法),模型敏感性分析

Luogu P2437 Bee Route

The line chart with square PyQt5_pyqtgraph mouse

被大厂强制毕业,两个月空窗期死背八股文,幸好上岸,不然房贷都还不上了

DOM系列之 click 延时解决方案

Learn about the sequential storage structure of binary tree - heap
随机推荐
Excel如何解密工作表保护
多主复制下处理写冲突(4)-多主复制拓扑
OpenPCDet environment configuration of 3 d object detection and demo test
internship:数据库表和建立的实体类及对应的枚举类之间的联系示例
alibaba数据同步组件canal的实践整理
日本痴汉打赏女主播1.5亿,结果。。。
HyperLynx中层叠设计实例
Scala basics [common method supplement, pattern matching]
复制延迟案例(4)-一致前缀读
区间和 离散化
【数字IC手撕代码】Verilog固定优先级仲裁器|题目|原理|设计|仿真
How to save a section of pages in a PDF as a new PDF file
MySQL存储函数详解
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
捷信将ESG理念注入企业DNA致力于提供“负责任的消费金融服务”
如果有些字段不想进行序列化怎么办?
C语言:结构体总结
其他重要协议(DNS,ICMP,NAT,交换机)
LeetCode 23: 合并K个升序链表
P1012 [NOIP1998 提高组] 拼数