結果

問題 No.460 裏表ちわーわ
ユーザー te-shte-sh
提出日時 2017-12-07 14:09:03
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,705 bytes
コンパイル時間 804 ms
コンパイル使用メモリ 98,680 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 17:17:34
合計ジャッジ時間 4,536 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.math;      // math functions

void main()
{
  auto rd = readln.split.to!(int[]), m = rd[0], n = rd[1];
  auto a = new bool[][](m, n);
  foreach (i; 0..m)
    a[i] = readln.split.map!(aij => aij == "1").array;

  auto inf = 10^^3;

  auto w(bool[][] a)
  {
    foreach (i; 0..m) {
      foreach (j; 0..n)
        write(a[i][j] ? "1" : "0");
      writeln;
    }
  }

  auto reverse(ref bool[][] a, int i, int j)
  {
    foreach (i2; max(0, i-1)..min(m, i+2))
      foreach (j2; max(0, j-1)..min(n, j+2))
        a[i2][j2] = !a[i2][j2];
  }

  auto calc(bool[][] a, ref bool[][] b)
  {
    auto c = a.map!(ai => ai.dup).array;

    foreach (i; 0..m)
      if (b[i][0]) reverse(c, i, 0);
    foreach (j; 1..n)
      if (b[0][j]) reverse(c, 0, j);

    foreach (i; 1..m)
      foreach (j; 1..n) {
        b[i][j] = c[i-1][j-1];
        if (b[i][j]) reverse(c, i, j);
      }

    foreach (i; 0..m)
      if (c[i][n-1]) return inf;
    foreach (j; 0..n-1)
      if (c[m-1][j]) return inf;

    return b.map!(bi => bi.count(true)).sum.to!int;
  }

  auto ans = inf;

  foreach (i; 0..1<<(m+n-1)) {
    auto b = new bool[][](m, n);
    foreach (j; 0..m)
      b[j][0] = i.bitTest(j);
    foreach (j; 1..n)
      b[0][j] = i.bitTest(j+m-1);
    ans = min(ans, calc(a, b));
  }

  if (ans == inf) writeln("Impossible");
  else            writeln(ans);
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
  pure T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }
}
0