結果

問題 No.460 裏表ちわーわ
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-12-15 20:27:15
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 14 ms / 2,000 ms
コード長 2,213 bytes
コンパイル時間 1,304 ms
コンパイル使用メモリ 151,696 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 23:34:48
合計ジャッジ時間 3,203 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import core.bitop;
import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.typecons;
void log(A...)(A arg) { stderr.writeln(arg); }
int size(T)(in T s) { return cast(int)s.length; }


void main() {
    int M, N; readf("%s %s\n", &M, &N);
    auto A = new int[][M];
    foreach (i; 0 .. M) A[i] = readln.chomp.split(" ").map!(to!int).array;

    auto X = new int[M];
    foreach (i; 0 .. M) {
        foreach (j; 0 .. N) {
            if (A[i][j]) X[i] |= 1 << j;
        }
    }

    int flip(int bit) {
        int r = 0;
        for (int i = 0; i < N; i++) {
            if (! (bit & (1 << i))) continue;
            for (int dx = -1; dx <= 1; dx++) {
                int nx = i + dx;
                if (nx < 0 || nx >= N) continue;
                if (r & (1 << nx)) r &= ~(1 << nx);
                else r |= 1 << nx;
            }
        }
        return r;
    }

    int[Tuple!(int, int, int)] memo;
    int f(int y, int up, int cur) {
        // いまy行目, y-1行目の状態がup, y行目の状態がcur.
        auto key = tuple(y, up, cur);
        if (key in memo) return memo[key];
        int ans = int.max / 4;
        if (y == M - 1) {
            for (int t = 0; t < (1 << N); t++) {
                auto s = flip(t);
                if ( (up ^ s) || (cur ^ s) ) continue;
                ans = min(ans, popcnt(t));
            }
        } else {
            for (int t = 0; t < (1 << N); t++) {
                auto s = flip(t);
                if (up ^ s) continue;
                ans = min(ans, popcnt(t) + f(y + 1, cur ^ s, X[y + 1] ^ s));
            }
        }
        return memo[key] = ans;
    }

    auto ans = int.max / 4;
    if (M == 1) {
        for (int t = 0; t < (1 << N); t++) {
            if (X[0] ^ flip(t)) continue;
            ans = min(ans, popcnt(t));
        }
    } else {
        for (int t = 0; t < (1 << N); t++) {
            ans = min(ans, popcnt(t) + f(1, flip(t) ^ X[0], flip(t) ^ X[1]));
        }
    }
    if (ans >= int.max / 4) {
        writeln("Impossible");
    } else {
        writeln(ans);
    }
}
0