結果

問題 No.157 2つの空洞
ユーザー te-shte-sh
提出日時 2020-01-25 18:08:54
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 5,616 bytes
コンパイル時間 1,422 ms
コンパイル使用メモリ 153,032 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 05:01:32
合計ジャッジ時間 2,587 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// URL: https://yukicoder.me/problems/no/157

import std.algorithm, std.array, std.container, std.math, std.range, std.typecons, std.string;

version(unittest) {} else
void main()
{
  int W, H; io.getV(W, H);
  string[] C; io.getC(H, C);

  auto c = grid(cast(char[][])C), b = Grid!bool(W, H);
  alias Point = c.Point;
  auto findSpace()
  {
    foreach (y; 0..H)
      foreach (x; 0..W) {
        auto p = Point(x, y);
        if (c[p] == '.' && !b[p]) {
          auto r = [p];
          b[p] = true;
          auto q = DList!Point(p);
          while (!q.empty) {
            auto u = q.front; q.removeFront();
            foreach (v; c.around4(u)) {
              if (c[v] == '.' && !b[v]) {
                r ~= v;
                b[v] = true;
                q.insertBack(v);
              }
            }
          }
          return r;
        }
      }
    assert(0);
  }

  auto s1 = findSpace(), s2 = findSpace(), r = 1000;
  foreach (p1; s1)
    foreach (p2; s2)
      r = min(r, distManhattan(p1, p2));

  io.put(r-1);
}

struct Point2(T)
{
  alias P = Point2!T, Op = string;
  T x, y;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-")
  { return mixin("P(x"~o~"r.x, y"~o~"r.y)"); }
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-")
  { mixin("x"~o~"=r.x; y"~o~"=r.y;"); return this; }
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/")
  { return mixin("P(x"~o~"a, y"~o~"a)"); }
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/")
  { mixin("x"~o~"=a; y"~o~"=a;"); return this; }
  pure T opBinary(Op o: "*")(P r) { return x*r.x+y*r.y; }
  pure T hypot2() { return x^^2+y^^2; }
}

pure T distManhattan(T)(Point2!T p1, Point2!T p2)
{ return (p1.x-p2.x).abs + (p1.y-p2.y).abs; }

pure T cross(T)(Point2!T p1, Point2!T p2)
{ return p1.x*p2.y - p1.y*p2.x; }

struct Point3(T)
{
  alias P = Point3!T, Op = string;
  T x, y, z;
  pure P opBinary(Op o)(P r) if (o=="+"||o=="-")
  { return mixin("P(x"~o~"r.x, y"~o~"r.y, z"~o~"r.z)"); }
  P opOpAssign(Op o)(P r) if (o=="+"||o=="-")
  { mixin("x"~o~"=r.x; y"~o~"=r.y; z"~o~"=r.z;"); return this; }
  pure P opBinary(Op o)(T a) if (o=="*"||o=="/")
  { return mixin("P(x"~o~"a, y"~o~"a, z"~o~"a)"); }
  P opOpAssign(Op o)(T a) if (o=="*"||o=="/")
  { mixin("x"~o~"=a; y"~o~"=a; z"~o~"=a;"); return this; }
  pure T opBinary(Op o: "*")(P r) { return x*r.x+y*r.y+z*r.z; }
  pure T hypot2() { return x^^2+y^^2+z^^2; }
}

pure Point3!T cross(T)(Point3!T p1, Point3!T p2)
{ return Point3!T(p1.y*p2.z - p1.z*p2.y, p1.z*p2.x - p1.x*p2.z, p1.x*p2.y - p1.y*p2.x); }

struct Grid(T)
{
  alias G = Grid!T, P = Point2!int, Point = P;
  size_t c, r;
  T[][] data;

  this(size_t c, size_t r) { this.c = c; this.r = r; data = new T[][](r, c); }
  this(T[][] data) { c = data[0].length; r = data.length; this.data = data; }

  pure P i2p(size_t i) { return P(cast(int)(i%c), cast(int)(i/c)); }
  pure size_t p2i(P p) { return p.x*c + p.y; }

  pure T opIndex(size_t x, size_t y) { return data[y][x]; }
  pure T opIndex(P p) { return data[p.y][p.x]; }
  G opIndexAssign(T v, size_t x, size_t y) { data[y][x] = v; return this; }
  G opIndexAssign(T v, P p) { data[p.y][p.x] = v; return this; }
  G opIndexOpAssign(string op)(T v, size_t x, size_t y)
  { mixin("data[y][x]"~op~"=v;"); return this; }
  G opIndexOpAssign(string op)(T v, P p)
  { mixin("data[p.y][p.x]"~op~"=v;"); return this; }
  G opIndexUnary(string op)(size_t x, size_t y) if (op=="++"||op=="--")
  { mixin(op~"data[y][x];"); return this; }
  G opIndexUnary(string op)(P p) if (op=="++"||op=="--")
  { mixin(op~"data[p.y][p.x];"); return this; }

  pure bool valid(size_t x, size_t y) { return 0 <= x && x < c && 0 <= y && y < r; }
  pure bool valid(P p) { return valid(p.x, p.y); }
  auto d4 = [P(-1, 0), P(0, -1), P(1, 0), P(0, 1)];
  pure auto around4(P p) { return d4.map!(d => d+p).filter!(np => valid(np)); }
  auto d8 = [P(-1, 0), P(-1, -1), P(0, -1), P(1, -1), P(1, 0), P(1, 1), P(0, 1), P(-1, 1)];
  pure auto around8(P p) { return d8.map!(d => d+p).filter!(np => valid(np)); }
}
Grid!T grid(T)(T[][] data) { return Grid!T(data); }

auto io = IO!()();
import std.stdio;
struct IO(string floatFormat = "%.10f", string delimiter = " ", alias IN = stdin, alias OUT = stdout)
{
  import std.conv, std.format, std.meta, std.traits;
  alias assignable = hasAssignableElements;

  auto getV(T...)(ref T v) { foreach (ref w; v) get(w); }
  auto getA(T)(size_t n, ref T v) if (assignable!T) { v = new T(n); foreach (ref w; v) get(w); }
  auto getC(T...)(size_t n, ref T v) if (allSatisfy!(assignable, T))
  {
    foreach (ref w; v) w = new typeof(w)(n);
    foreach (i; 0..n) foreach (ref w; v) get(w[i]);
  }
  auto getM(T)(size_t r, size_t c, ref T v) if (assignable!T && assignable!(ElementType!T))
  {
    v = new T(r); foreach (ref w; v) getA(c, w);
  }

  auto put(bool flush = false, T...)(T v)
  {
    foreach (i, w; v) { putA(w); if (i < v.length-1) OUT.write(delimiter); }
    OUT.writeln;
    static if (flush) OUT.flush();
  }
  auto putB(S, T)(bool c, S t, T f) { if (c) put(t); else put(f); }
  auto putRaw(T...)(T v) { OUT.write(v); OUT.writeln; }

  private
  {
    dchar[] buf;
    auto sp = (new dchar[](0)).splitter;
    void nextLine() { IN.readln(buf); sp = buf.splitter; }
    auto get(T)(ref T v) { if (sp.empty) nextLine(); v = sp.front.to!T; sp.popFront(); }

    auto putR(T)(T v)
    {
      auto w = v;
      while (!w.empty) { putA(w.front); w.popFront(); if (!w.empty) OUT.write(delimiter); }
    }
    auto putA(T)(T v)
    {
      static if (isInputRange!T && !isSomeString!T) putR(v);
      else if (isFloatingPoint!T) OUT.write(format(floatFormat, v));
      else OUT.write(v);
    }
  }
}
0