当前位置:网站首页>翻转(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;
}
边栏推荐
猜你喜欢
Anatomy of Unreal Playback System (Part 1)
批量--09---批量读文件入表
多主复制的适用场景(1)-多IDC
ScholarOne Manuscripts submits journal LaTeX file and cannot convert PDF successfully!
Batch normalization (BN) based on deep learning
关于地图GIS开发事项的一次实践整理(上)
【STM32】 ADC模数转换
How to decrypt worksheet protection in Excel
Line generation 005
论文速读:Homography Loss for Monocular 3D Object Detection
随机推荐
Use the advanced timer of GD32F207 to generate hidden bugs in PWM waves
Arduino框架下 ESP32看门狗使用示例
被大厂强制毕业,两个月空窗期死背八股文,幸好上岸,不然房贷都还不上了
轮询和长轮询的区别
【每日一题】1374. 生成每种字符都是奇数个的字符串
论文速读:Homography Loss for Monocular 3D Object Detection
投资组合分析:portfolio_analysis.Tangenvy_portfolio(切点组合)
Scientific research notes (5) SLAC WiFi Fingerprint+ Step counter fusion positioning
多主复制的适用场景(1)-多IDC
MySQL read-write separation mysql-proxy deployment
AFMG SysTune1.3.7使用图解
批量--10---根据set数拆分文件
CaDDN code debugging
Deep blue college - handwritten VIO operations - the first chapter
力扣练习——41 对称二叉树
ADSP21489工程中LDF文件配置详解
违约金过高”的认定依据
PyQt5_pyqtgraph鼠标在折线图上画方形
什么是接触电流怎么测?
MySQL存储函数详解