結果

問題 No.402 最も海から遠い場所
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-30 07:40:03
言語 D
(dmd 2.105.2)
結果
AC  
実行時間 1,243 ms / 3,000 ms
コード長 3,836 bytes
コンパイル時間 2,082 ms
コンパイル使用メモリ 151,808 KB
実行使用メモリ 163,188 KB
最終ジャッジ日時 2023-09-03 23:21:40
合計ジャッジ時間 9,110 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,504 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 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 7 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 29 ms
7,412 KB
testcase_16 AC 43 ms
11,424 KB
testcase_17 AC 498 ms
93,392 KB
testcase_18 AC 1,243 ms
163,184 KB
testcase_19 AC 783 ms
163,184 KB
testcase_20 AC 1,068 ms
163,120 KB
testcase_21 AC 778 ms
163,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int H, W;
string[] S;

void main() {
  try {
    for (; ; ) {
      H = readInt() + 2;
      W = readInt() + 2;
      S = new string[H];
      S[0] = S[H - 1] = iota(W).map!(_ => '.').to!string;
      foreach (x; 1 .. H - 1) {
        S[x] = '.' ~ readToken() ~ '.';
      }
      
      /*
        d d d
         e e
        d d d
         e e
        d d d
      */
      auto q = new int[H * W + (H - 1) * (W - 1)];
      int qHead, qTail;
      auto d = new int[][](H, W);
      auto e = new int[][](H - 1, W - 1);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        d[x][y] = -1;
      }
      foreach (x; 0 .. H - 1) foreach (y; 0 .. W - 1) {
        e[x][y] = -1;
      }
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        if (S[x][y] == '.') {
          d[x][y] = 0;
          q[qTail++] = x * W + y;
        }
      }
      for (; qHead < qTail; ) {
        const s = q[qHead] / (H * W);
        const x = q[qHead] % (H * W) / W;
        const y = q[qHead] % W;
        ++qHead;
        if (s == 0) {
          const cc = d[x][y] + 1;
          if (x < H - 1 && y < W - 1) {
            if (e[x][y] == -1) {
              e[x][y] = cc;
              q[qTail++] = H * W + x * W + y;
            }
          }
          if (x > 0 && y < W - 1) {
            if (e[x - 1][y] == -1) {
              e[x - 1][y] = cc;
              q[qTail++] = H * W + (x - 1) * W + y;
            }
          }
          if (x > 0 && y > 0) {
            if (e[x - 1][y - 1] == -1) {
              e[x - 1][y - 1] = cc;
              q[qTail++] = H * W + (x - 1) * W + (y - 1);
            }
          }
          if (x < H - 1 && y > 0) {
            if (e[x][y - 1] == -1) {
              e[x][y - 1] = cc;
              q[qTail++] = H * W + x * W + (y - 1);
            }
          }
        } else {
          const cc = e[x][y] + 1;
          if (d[x + 1][y + 1] == -1) {
            d[x + 1][y + 1] = cc;
            q[qTail++] = (x + 1) * W + (y + 1);
          }
          if (d[x][y + 1] == -1) {
            d[x][y + 1] = cc;
            q[qTail++] = x * W + (y + 1);
          }
          if (d[x][y] == -1) {
            d[x][y] = cc;
            q[qTail++] = x * W + y;
          }
          if (d[x + 1][y] == -1) {
            d[x + 1][y] = cc;
            q[qTail++] = (x + 1) * W + y;
          }
        }
      }
      debug {
        if (H <= 10) {
          foreach (x; 0 .. H) {
            writeln(d[x]);
          }
          foreach (x; 0 .. H - 1) {
            writeln(e[x]);
          }
        }
      }
      int ans;
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        chmax(ans, d[x][y]);
      }
      ans /= 2;
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0