結果

問題 No.460 裏表ちわーわ
ユーザー xuzijian629xuzijian629
提出日時 2018-10-31 13:48:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 1,548 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 205,412 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-12 11:19:55
合計ジャッジ時間 4,630 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 41 ms
4,380 KB
testcase_06 AC 39 ms
4,376 KB
testcase_07 AC 38 ms
4,380 KB
testcase_08 AC 38 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 38 ms
4,380 KB
testcase_11 AC 41 ms
4,384 KB
testcase_12 AC 41 ms
4,376 KB
testcase_13 AC 41 ms
4,380 KB
testcase_14 AC 40 ms
4,380 KB
testcase_15 AC 42 ms
4,380 KB
testcase_16 AC 40 ms
4,380 KB
testcase_17 AC 40 ms
4,376 KB
testcase_18 AC 41 ms
4,384 KB
testcase_19 AC 41 ms
4,376 KB
testcase_20 AC 39 ms
4,376 KB
testcase_21 AC 39 ms
4,380 KB
testcase_22 AC 40 ms
4,380 KB
testcase_23 AC 39 ms
4,380 KB
testcase_24 AC 42 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

int m, n;

void flip(vvi& b, int x, int y) {
    for (int dx = -1; dx <= 1; dx++) {
        for (int dy = -1; dy <= 1; dy++) {
            int xx = x + dx;
            int yy = y + dy;
            if (0 <= xx && xx < m && 0 <= yy && yy < n) {
                b[xx][yy] = 1 - b[xx][yy];
            }
        }
    }
}

int main() {
    cin >> m >> n;
    vvi b(m, vi(n));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> b[i][j];
        }
    }

    int ans = 1e9;

    for (int i = 0; i < 1 << (m + n - 1); i++) {
        vvi bb(b);
        int cnt = 0;
        for (int j = 0; j < m; j++) {
            if ((i >> j) & 1) {
                cnt++;
                flip(bb, j, 0);
            }
        }
        for (int j = 0; j < n - 1; j++) {
            if ((i >> m + j) & 1) {
                cnt++;
                flip(bb, 0, 1 + j);
            }
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (bb[i - 1][j - 1]) {
                    cnt++;
                    flip(bb, i, j);
                }
            }
        }

        int ok = 1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (bb[i][j]) ok = 0;
            }
        }
        if (ok) {
            ans = min(ans, cnt);
        }
    }

    cout << (ans == 1e9 ? "Impossible" : to_string(ans)) << endl;
}
0