結果

問題 No.1323 うしらずSwap
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-20 00:55:51
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,427 ms / 3,000 ms
コード長 6,339 bytes
コンパイル時間 2,830 ms
コンパイル使用メモリ 111,540 KB
実行使用メモリ 84,996 KB
最終ジャッジ日時 2023-09-04 11:43:25
合計ジャッジ時間 35,523 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,500 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 722 ms
83,904 KB
testcase_17 AC 886 ms
83,600 KB
testcase_18 AC 723 ms
84,996 KB
testcase_19 AC 921 ms
83,888 KB
testcase_20 AC 727 ms
84,684 KB
testcase_21 AC 723 ms
83,620 KB
testcase_22 AC 914 ms
83,964 KB
testcase_23 AC 719 ms
84,188 KB
testcase_24 AC 896 ms
84,008 KB
testcase_25 AC 716 ms
83,740 KB
testcase_26 AC 704 ms
84,464 KB
testcase_27 AC 714 ms
83,648 KB
testcase_28 AC 754 ms
83,740 KB
testcase_29 AC 702 ms
83,736 KB
testcase_30 AC 893 ms
83,944 KB
testcase_31 AC 699 ms
83,644 KB
testcase_32 AC 906 ms
83,936 KB
testcase_33 AC 1,205 ms
83,436 KB
testcase_34 AC 1,251 ms
84,660 KB
testcase_35 AC 1,298 ms
83,700 KB
testcase_36 AC 1,313 ms
83,920 KB
testcase_37 AC 1,323 ms
83,688 KB
testcase_38 AC 1,384 ms
83,408 KB
testcase_39 AC 1,408 ms
84,392 KB
testcase_40 AC 1,402 ms
83,716 KB
testcase_41 AC 1,427 ms
83,892 KB
testcase_42 AC 1,398 ms
83,680 KB
testcase_43 AC 335 ms
83,932 KB
testcase_44 AC 433 ms
83,744 KB
testcase_45 AC 532 ms
83,932 KB
testcase_46 AC 441 ms
83,996 KB
testcase_47 AC 403 ms
83,944 KB
testcase_48 AC 324 ms
83,964 KB
testcase_49 AC 421 ms
83,992 KB
testcase_50 AC 279 ms
83,604 KB
testcase_51 AC 71 ms
83,900 KB
testcase_52 AC 179 ms
83,932 KB
testcase_53 AC 413 ms
83,952 KB
testcase_54 AC 179 ms
83,992 KB
testcase_55 AC 421 ms
84,504 KB
testcase_56 AC 126 ms
83,688 KB
testcase_57 AC 534 ms
84,420 KB
testcase_58 AC 4 ms
4,380 KB
testcase_59 AC 5 ms
4,380 KB
testcase_60 AC 4 ms
4,380 KB
testcase_61 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, 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)); }


enum DX = [+1, 0, -1, 0];
enum DY = [0, +1, 0, -1];

enum INF = 10^^9;

void main() {
  try {
    for (; ; ) {
      const H = readInt();
      const W = readInt();
      const SX = readInt() - 1;
      const SY = readInt() - 1;
      const TX = readInt() - 1;
      const TY = readInt() - 1;
      auto A = new char[][H];
      foreach (x; 0 .. H) {
        A[x] = readToken().dup;
      }
      
      alias Pt = Tuple!(int, "x", int, "y");
      auto que = new Pt[H * W];
      int qb, qe;
      auto dist = new int[][](H, W);
      auto prev = new Pt[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        dist[x][y] = -1;
      }
      dist[SX][SY] = 0;
      que[qe++] = Pt(SX, SY);
      for (; qb != qe; ) {
        const x = que[qb].x;
        const y = que[qb].y;
        ++qb;
        if (A[x + 1][y] == '.' && dist[x + 1][y] == -1) { dist[x + 1][y] = dist[x][y] + 1; prev[x + 1][y] = Pt(x, y); que[qe++] = Pt(x + 1, y); }
        if (A[x][y + 1] == '.' && dist[x][y + 1] == -1) { dist[x][y + 1] = dist[x][y] + 1; prev[x][y + 1] = Pt(x, y); que[qe++] = Pt(x, y + 1); }
        if (A[x - 1][y] == '.' && dist[x - 1][y] == -1) { dist[x - 1][y] = dist[x][y] + 1; prev[x - 1][y] = Pt(x, y); que[qe++] = Pt(x - 1, y); }
        if (A[x][y - 1] == '.' && dist[x][y - 1] == -1) { dist[x][y - 1] = dist[x][y] + 1; prev[x][y - 1] = Pt(x, y); que[qe++] = Pt(x, y - 1); }
      }
      
      qb = qe = 0;
      auto dist1 = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        dist1[x][y] = -1;
      }
      dist1[TX][TY] = 0;
      que[qe++] = Pt(TX, TY);
      for (; qb != qe; ) {
        const x = que[qb].x;
        const y = que[qb].y;
        ++qb;
        if (A[x + 1][y] == '.' && dist1[x + 1][y] == -1) { dist1[x + 1][y] = dist1[x][y] + 1; que[qe++] = Pt(x + 1, y); }
        if (A[x][y + 1] == '.' && dist1[x][y + 1] == -1) { dist1[x][y + 1] = dist1[x][y] + 1; que[qe++] = Pt(x, y + 1); }
        if (A[x - 1][y] == '.' && dist1[x - 1][y] == -1) { dist1[x - 1][y] = dist1[x][y] + 1; que[qe++] = Pt(x - 1, y); }
        if (A[x][y - 1] == '.' && dist1[x][y - 1] == -1) { dist1[x][y - 1] = dist1[x][y] + 1; que[qe++] = Pt(x, y - 1); }
      }
      
      A[SX][SY] = '!';
      qb = qe = 0;
      auto distT = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        distT[x][y] = -1;
      }
      distT[TX][TY] = 0;
      que[qe++] = Pt(TX, TY);
      for (; qb != qe; ) {
        const x = que[qb].x;
        const y = que[qb].y;
        ++qb;
        if (A[x + 1][y] == '.' && distT[x + 1][y] == -1) { distT[x + 1][y] = distT[x][y] + 1; que[qe++] = Pt(x + 1, y); }
        if (A[x][y + 1] == '.' && distT[x][y + 1] == -1) { distT[x][y + 1] = distT[x][y] + 1; que[qe++] = Pt(x, y + 1); }
        if (A[x - 1][y] == '.' && distT[x - 1][y] == -1) { distT[x - 1][y] = distT[x][y] + 1; que[qe++] = Pt(x - 1, y); }
        if (A[x][y - 1] == '.' && distT[x][y - 1] == -1) { distT[x][y - 1] = distT[x][y] + 1; que[qe++] = Pt(x, y - 1); }
      }
      A[SX][SY] = '.';
      
      A[TX][TY] = '!';
      qb = qe = 0;
      auto distS = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        distS[x][y] = -1;
      }
      distS[SX][SY] = 0;
      que[qe++] = Pt(SX, SY);
      for (; qb != qe; ) {
        const x = que[qb].x;
        const y = que[qb].y;
        ++qb;
        if (A[x + 1][y] == '.' && distS[x + 1][y] == -1) { distS[x + 1][y] = distS[x][y] + 1; que[qe++] = Pt(x + 1, y); }
        if (A[x][y + 1] == '.' && distS[x][y + 1] == -1) { distS[x][y + 1] = distS[x][y] + 1; que[qe++] = Pt(x, y + 1); }
        if (A[x - 1][y] == '.' && distS[x - 1][y] == -1) { distS[x - 1][y] = distS[x][y] + 1; que[qe++] = Pt(x - 1, y); }
        if (A[x][y - 1] == '.' && distS[x][y - 1] == -1) { distS[x][y - 1] = distS[x][y] + 1; que[qe++] = Pt(x, y - 1); }
      }
      A[TX][TY] = '.';
      
      debug {
        writeln("dist = ", dist);
        writeln("dist1 = ", dist1);
        writeln("distS = ", distS);
        writeln("distT = ", distT);
      }
      
      int ans = INF;
      if (dist[TX][TY] != -1) {
        auto on = new bool[][](H, W);
        for (int x = TX, y = TY; ; ) {
          on[x][y] = true;
          if (x == SX && y == SY) {
            break;
          }
          const p = prev[x][y];
          x = p.x;
          y = p.y;
        }
        foreach (x; 1 .. H - 1) foreach (y; 1 .. W - 1) {
          if (A[x][y] == '.' && !on[x][y]) {
            if (distS[x][y] != -1 && distT[x][y] != -1) {
              chmin(ans, dist[TX][TY] + distS[x][y] + distT[x][y]);
            }
          }
        }
        foreach (x; 1 .. H - 1) foreach (y; 1 .. W - 1) {
          if (A[x][y] == '.') {
            int cnt;
            foreach (dir; 0 .. 4) {
              if (A[x + DX[dir]][y + DY[dir]] == '.') {
                ++cnt;
              }
            }
            if (cnt >= 3) {
              if (dist[x][y] != -1 && dist1[x][y] != -1) {
                chmin(ans, 2 * (dist[x][y] + dist1[x][y] + 2));
              }
            }
          }
        }
      }
      writeln((ans >= INF) ? -1 : ans);
    }
  } catch (EOFException e) {
  }
}
0