結果

問題 No.348 カゴメカゴメ
ユーザー te-shte-sh
提出日時 2017-07-10 17:22:46
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 696 ms / 2,000 ms
コード長 4,621 bytes
コンパイル時間 1,427 ms
コンパイル使用メモリ 139,788 KB
実行使用メモリ 37,644 KB
最終ジャッジ日時 2024-06-12 20:49:43
合計ジャッジ時間 12,191 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 480 ms
37,644 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 696 ms
36,560 KB
testcase_03 AC 576 ms
14,708 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 13 ms
6,940 KB
testcase_13 AC 5 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 378 ms
14,540 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 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,944 KB
testcase_23 AC 513 ms
31,760 KB
testcase_24 AC 507 ms
28,752 KB
testcase_25 AC 509 ms
28,764 KB
testcase_26 AC 505 ms
18,120 KB
testcase_27 AC 502 ms
28,708 KB
testcase_28 AC 501 ms
14,812 KB
testcase_29 AC 506 ms
28,708 KB
testcase_30 AC 499 ms
14,924 KB
testcase_31 AC 496 ms
18,020 KB
testcase_32 AC 508 ms
28,748 KB
testcase_33 AC 41 ms
8,028 KB
testcase_34 AC 42 ms
7,012 KB
testcase_35 AC 43 ms
7,168 KB
testcase_36 AC 42 ms
7,088 KB
testcase_37 AC 42 ms
8,020 KB
testcase_38 AC 42 ms
7,784 KB
testcase_39 AC 43 ms
6,988 KB
testcase_40 AC 43 ms
7,968 KB
testcase_41 AC 43 ms
7,304 KB
testcase_42 AC 42 ms
7,580 KB
testcase_43 AC 7 ms
6,940 KB
testcase_44 AC 8 ms
6,940 KB
testcase_45 AC 7 ms
6,940 KB
testcase_46 AC 7 ms
6,940 KB
testcase_47 AC 7 ms
6,940 KB
testcase_48 AC 7 ms
6,940 KB
testcase_49 AC 7 ms
6,944 KB
testcase_50 AC 7 ms
6,940 KB
testcase_51 AC 7 ms
6,940 KB
testcase_52 AC 7 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 (p; g.points) {
    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 (p; g.points) {
    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