Editorial for VM 15 Bài 01 - Đảo bit


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của happyboy99x

#include <bits/stdc++.h>
using namespace std;

const int N = 100;
int a[N][N];

int main() {
    ios::sync_with_stdio(false);
    int m; cin >> m;
    int n; cin >> n;
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> a[i][j];
        }
    }
    vector<pair<int, int>> result;
    for (int i = 0; i + 3 <= m; ++i) {
        for (int j = 0; j + 3 <= n; ++j) if (a[i][j] == 1) {
            for (int x = 0; x < 3; ++x) {
                for (int y = 0; y < 3; ++y) {
                    a[i + x][j + y] ^= 1;
                }
            }
            result.emplace_back(i, j);
        }
    }
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) if (a[i][j] != 0) {
            cout << -1 << '\n';
            return 0;
        }
    }
    cout << result.size() << '\n';
    for (auto x : result) {
        cout << x.first + 1 << ' ' << x.second + 1 << '\n';
    }
    return 0;
}

Code mẫu của ladpro98

#include <bits/stdc++.h>

const int N = 1111;

using namespace std;

int m, n;
int a[N][N];

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
#ifdef _LAD_
    freopen("VMDAOBIT.txt", "r", stdin);
#endif
    cin >> m >> n;
    for (int i = 1; i <= m; ++i) for (int j = 1; j <= n; ++j) cin >> a[i][j];
    vector<pair<int, int> > ans;
    for (int i = 1; i <= m - 2; ++i) for (int j = 1; j <= n - 2; ++j) if (a[i][j]) {
        ans.push_back(make_pair(i, j));
        for (int x = i; x < i + 3; ++x) for (int y = j; y < j + 3; ++y)
            a[x][y] ^= 1;
    }
    for (int i = 1; i <= m; ++i) for (int j = 1; j <= n; ++j)
    if (a[i][j]) {
        cout << -1 << endl;
        return 0;
    }
    cout << ans.size() << endl;
    for (auto it: ans) cout << it.first << ' ' << it.second << '\n';
    return 0;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.