結果

問題 No.157 2つの空洞
ユーザー te-shte-sh
提出日時 2018-07-12 13:50:36
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,357 bytes
コンパイル時間 667 ms
コンパイル使用メモリ 94,712 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 20:41:00
合計ジャッジ時間 1,955 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}
void readC(T...)(size_t n,ref T t){foreach(ref v;t)v=new typeof(v)(n);foreach(i;0..n){auto r=rdsp;foreach(ref v;t)pick(r,v[i]);}}

void main()
{
  int w, h; readV(w, h);
  string[] c; readC(h, c);

  auto b = new bool[][](h, w);
  auto findSpace()
  {
    foreach (i; 0..h)
      foreach (j; 0..w)
        if (c[i][j] == '.' && !b[i][j]) {
          auto r = [P(j, i)];
          b[i][j] = true;
          auto q = DList!P(P(j, i));
          while (!q.empty) {
            auto u = q.front; q.removeFront();
            foreach (d; [[-1,0], [1,0], [0,-1], [0,1]]) {
              auto v = P(u.x+d[0], u.y+d[1]);
              if (c[v.y][v.x] == '.' && !b[v.y][v.x]) {
                r ~= v;
                b[v.y][v.x] = true;
                q.insertBack(v);
              }
            }
          }
          return r;
        }
    assert(0);
  }

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

  writeln(r-1);
}

struct P { int x, y; }
auto dist(P p1, P p2) { return (p1.x-p2.x).abs+(p1.y-p2.y).abs; }
0