結果

問題 No.402 最も海から遠い場所
ユーザー 👑 hos.lyric
提出日時 2019-01-30 07:40:03
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 1,561 ms / 3,000 ms
コード長 3,836 bytes
コンパイル時間 1,946 ms
コンパイル使用メモリ 156,000 KB
実行使用メモリ 162,932 KB
最終ジャッジ日時 2024-06-13 03:54:05
合計ジャッジ時間 8,892 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

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) {
}
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0