結果

問題 No.460 裏表ちわーわ
ユーザー nebukuro09nebukuro09
提出日時 2016-12-15 22:46:18
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,688 bytes
コンパイル時間 1,168 ms
コンパイル使用メモリ 109,388 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-02 23:34:58
合計ジャッジ時間 2,305 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;
import std.bigint;
import core.bitop;


void main() {
  auto s = readln.split.map!(to!int);
  auto H = s[0];
  auto W = s[1];
  auto A = H.iota.map!(_ => readln.split.map!(to!int).array).array;
  auto B = new int[](H+1);
  foreach (i; 0..H) {
    int b = 0;
    foreach (j; 0..W)
      if (A[i][j] == 1)
        b |= (1 << j);
    B[i] = b;
  }

  auto p = new int[](1<<W);
  foreach (i; 0..(1<<W))
    p[i] = 2^^30-1;
  foreach (on; 0..(1<<W)) {
    foreach (push; 0..(1<<W)) {
      auto tmp = on;
      foreach (i; 0..W)
        if (push & (1 << i))
          tmp ^= (((0b111 << i) >> 1) & ((1 << W) - 1));
      if (tmp == 0 && p[on].popcnt > push.popcnt)
        p[on] = push;
    }
  }

  int ans = int.max;
  foreach (top; 0..(1<<W)) {
    auto tmp_b = B.dup;
    int tmp_ans = top.popcnt;

    foreach (i; 0..W) {
      if (top & (1 << i)) {
        tmp_b[0] ^= (((0b111 << i) >> 1) & ((1 << W) -1));
        tmp_b[1] ^= (((0b111 << i) >> 1) & ((1 << W) -1));
      }
    }

    foreach (i; 1..H) {
      if (p[tmp_b[i-1]] == 2^^30-1) {
        tmp_ans = int.max;
        break;
      }
      tmp_ans += p[tmp_b[i-1]].popcnt;
      foreach (j; 0..W)
        if (p[tmp_b[i-1]] & (1 << j)) {
          tmp_b[i] ^= (((0b111 << j) >> 1) & ((1 << W) -1));
          tmp_b[i+1] ^= (((0b111 << j) >> 1) & ((1 << W) -1));
        }
    }
    if (tmp_b[H-1] != 0) tmp_ans = int.max;
    ans = min(ans, tmp_ans);
  }

  (ans == int.max ? "Impossible" : ans.to!string).writeln;
}
0