結果

問題 No.348 カゴメカゴメ
ユーザー te-shte-sh
提出日時 2017-07-10 17:24:51
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 596 ms / 2,000 ms
コード長 4,799 bytes
コンパイル時間 1,121 ms
コンパイル使用メモリ 139,120 KB
実行使用メモリ 31,848 KB
最終ジャッジ日時 2024-06-12 20:50:03
合計ジャッジ時間 9,773 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 406 ms
25,208 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 596 ms
21,816 KB
testcase_03 AC 485 ms
14,664 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 11 ms
6,944 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 325 ms
14,560 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 441 ms
17,976 KB
testcase_24 AC 437 ms
28,692 KB
testcase_25 AC 445 ms
31,848 KB
testcase_26 AC 440 ms
28,672 KB
testcase_27 AC 439 ms
31,764 KB
testcase_28 AC 441 ms
31,712 KB
testcase_29 AC 426 ms
14,804 KB
testcase_30 AC 426 ms
14,976 KB
testcase_31 AC 424 ms
18,020 KB
testcase_32 AC 433 ms
28,676 KB
testcase_33 AC 37 ms
8,564 KB
testcase_34 AC 35 ms
6,944 KB
testcase_35 AC 36 ms
7,896 KB
testcase_36 AC 36 ms
7,172 KB
testcase_37 AC 35 ms
7,760 KB
testcase_38 AC 35 ms
7,776 KB
testcase_39 AC 35 ms
6,944 KB
testcase_40 AC 35 ms
7,704 KB
testcase_41 AC 36 ms
7,572 KB
testcase_42 AC 36 ms
7,984 KB
testcase_43 AC 6 ms
6,940 KB
testcase_44 AC 6 ms
6,944 KB
testcase_45 AC 6 ms
6,940 KB
testcase_46 AC 7 ms
6,940 KB
testcase_47 AC 6 ms
6,944 KB
testcase_48 AC 6 ms
6,944 KB
testcase_49 AC 7 ms
6,944 KB
testcase_50 AC 6 ms
6,940 KB
testcase_51 AC 6 ms
6,944 KB
testcase_52 AC 6 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.container; // SList, DList, BinaryHeap

alias Point!int point;
const emptyCell = -2, tbdCell = -1;

void main()
{
  auto rd = readln.split.to!(int[]), r = rd[0], c = rd[1];

  auto g = Grid!(int, int)(r+2, c+2);
  foreach (i; 0..r) {
    auto s = readln.chomp;
    foreach (j; 0..c)
      g[i+1][j+1] = s[j].predSwitch('.', emptyCell, 'x', tbdCell);
  }
  foreach (i; 0..r+2) {
    g[i][0] = emptyCell;
    g[i][$-1] = emptyCell;
  }
  g[0][] = emptyCell;
  g[$-1][] = emptyCell;

  auto ringNo = 0;
  int[] rings = [];

  foreach (i; 0..r+2)
    foreach (j; 0..c+2) {
      auto p = point(j, i);
      if (g[p] == emptyCell || g[p] != tbdCell) continue;

      struct Qitem { point pp, cp; int len; }
      auto q = DList!Qitem(Qitem(point(-1, -1), p, 1));
      g[p] = ringNo;
      while (!q.empty) {
        auto qi = q.front; q.removeFront;
        foreach (np; g.sibPoints8(qi.cp).filter!(p => p != qi.pp)) {
          if (np == p) rings ~= qi.len;
          if (g[np] == tbdCell) {
            g[np] = ringNo;
            q.insertBack(Qitem(qi.cp, np, qi.len+1));
            break;
          }
        }
      }

      ++ringNo;
    }

  auto nr = rings.length;
  auto visited = Grid!(bool, int)(r+2, c+2);
  auto marked = new bool[](nr), tree = new int[][](nr+1);

  auto findMarked(point p)
  {
    foreach (np; g.sibPoints4(p))
      if (marked[g[np]])
        return g[np];
    assert(0);
  }

  foreach (i; 0..r+2)
    foreach (j; 0..c+2) {
      auto p = point(j, i);
      if (g[p] != emptyCell || visited[p]) continue;
      auto outer = p == point(0, 0) ? nr : findMarked(p);

      auto q = DList!point(p);
      visited[p] = true;
      while (!q.empty) {
        auto cp = q.front; q.removeFront();
        foreach (np; g.sibPoints4(cp)) {
          if (g[np] == emptyCell) {
            if (!visited[np]) {
              visited[np] = true;
              q.insertBack(np);
            }
          } else {
            auto ring = g[np];
            if (!marked[ring]) {
              tree[outer] ~= ring;
              marked[ring] = true;
            }
          }
        }
      }
    }

  auto dpa = new int[](nr), dpb = new int[](nr);
  auto t = SList!int();

  foreach (i; tree[nr]) {
    auto s = SList!int(i);
    while (!s.empty) {
      auto j = s.front; s.removeFront();
      t.insertFront(j);
      foreach (k; tree[j]) s.insertFront(k);
    }
  }

  while (!t.empty) {
    auto i = t.front; t.removeFront();
    dpa[i] = rings[i] + tree[i].map!(j => dpb[j]).sum;
    dpb[i] = tree[i].map!(j => max(dpa[j], dpb[j])).sum;
  }

  writeln(tree[nr].map!(i => max(dpa[i], dpb[i])).sum);
}

struct Point(T)
{
  T x, y;
  pure auto opBinary(string op: "+")(Point!T rhs) const { return Point!T(x + rhs.x, y + rhs.y); }
  pure auto opBinary(string op: "-")(Point!T rhs) const { return Point!T(x - rhs.x, y - rhs.y); }
  pure auto opBinary(string op: "*")(Point!T rhs) const { return x * rhs.x + y * rhs.y; }
  pure auto opBinary(string op: "*")(T a) const { return Point!T(x * a, y * a); }
  pure auto opBinary(string op: "/")(T a) const { return Point!T(x / a, y / a); }
  pure auto hypot2() const { return x ^^ 2 + y ^^ 2; }
}

struct Grid(T, U)
{
  import std.algorithm, std.conv, std.range, std.traits, std.typecons;

  const sibs4 = [Point!U(-1, 0), Point!U(0, -1), Point!U(1, 0), Point!U(0, 1)];
  const sibs8 = [Point!U(-1, 0), Point!U(-1, -1), Point!U(0, -1), Point!U(1, -1),
                 Point!U(1, 0), Point!U(1, 1), Point!U(0, 1), Point!U(-1, 1)];

  T[][] m;
  const size_t rows, cols;

  mixin Proxy!m;

  this(size_t r, size_t c) { rows = r; cols = c; m = new T[][](rows, cols); }
  this(T[][] s) { rows = s.length; cols = s[0].length; m = s; }

  pure auto dup() const { return Grid(m.map!(r => r.dup).array); }
  ref pure auto opIndex(Point!U p) { return m[p.y][p.x]; }
  ref pure auto opIndex(size_t y) { return m[y]; }
  ref pure auto opIndex(size_t y, size_t x) const { return m[y][x]; }
  static if (isAssignable!T) {
    auto opIndexAssign(T v, Point!U p) { return m[p.y][p.x] = v; }
    auto opIndexAssign(T v, size_t y, size_t x) { return m[y][x] = v; }
    auto opIndexOpAssign(string op, V)(V v, Point!U p) { return mixin("m[p.y][p.x] " ~ op ~ "= v"); }
    auto opIndexOpAssign(string op, V)(V v, size_t y, size_t x) { return mixin("m[y][x] " ~ op ~ "= v"); }
  }
  pure auto validPoint(Point!U p) { return p.x >= 0 && p.x < cols && p.y >= 0 && p.y < rows; }
  pure auto points() const { return rows.to!U.iota.map!(y => cols.to!U.iota.map!(x => Point!U(x, y))).joiner; }
  pure auto sibPoints4(Point!U p) { return sibs4.map!(s => p + s).filter!(p => validPoint(p)); }
  pure auto sibPoints8(Point!U p) { return sibs8.map!(s => p + s).filter!(p => validPoint(p)); }
}
0