結果

問題 No.1323 うしらずSwap
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-20 00:12:13
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 4,112 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 127,192 KB
実行使用メモリ 115,996 KB
最終ジャッジ日時 2024-06-22 10:27:32
合計ジャッジ時間 58,380 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1 ms
6,944 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 1 ms
6,940 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1,219 ms
48,196 KB
testcase_17 AC 1,362 ms
49,304 KB
testcase_18 AC 1,229 ms
49,428 KB
testcase_19 AC 1,393 ms
48,508 KB
testcase_20 AC 1,231 ms
48,192 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1,368 ms
48,340 KB
testcase_27 AC 1,611 ms
69,648 KB
testcase_28 AC 1,338 ms
65,284 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1,928 ms
65,156 KB
testcase_34 AC 2,270 ms
108,368 KB
testcase_35 AC 2,343 ms
115,040 KB
testcase_36 AC 2,362 ms
108,608 KB
testcase_37 AC 2,338 ms
65,256 KB
testcase_38 AC 2,549 ms
65,192 KB
testcase_39 AC 2,497 ms
65,008 KB
testcase_40 AC 2,650 ms
85,456 KB
testcase_41 AC 2,576 ms
65,240 KB
testcase_42 AC 2,443 ms
67,208 KB
testcase_43 AC 568 ms
65,252 KB
testcase_44 AC 744 ms
65,272 KB
testcase_45 AC 912 ms
65,408 KB
testcase_46 AC 711 ms
48,384 KB
testcase_47 AC 690 ms
65,340 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
testcase_54 WA -
testcase_55 WA -
testcase_56 WA -
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
権限があれば一括ダウンロードができます

ソースコード

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];

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 string[H];
      foreach (x; 0 .. H) {
        A[x] = readToken();
      }
      
      alias Pt = Tuple!(int, "x", int, "y");
      DList!Pt que;
      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 ~= Pt(SX, SY);
      for (; !que.empty; ) {
        const x = que.front.x;
        const y = que.front.y;
        que.removeFront;
        foreach (dir; 0 .. 4) {
          const xx = x + DX[dir];
          const yy = y + DY[dir];
          if (A[xx][yy] == '.') {
            if (dist[xx][yy] == -1) {
              dist[xx][yy] = dist[x][y] + 1;
              prev[xx][yy] = Pt(x, y);
              que ~= Pt(xx, yy);
            }
          }
        }
      }
      auto distT = new int[][](H, W);
      foreach (x; 0 .. H) foreach (y; 0 .. W) {
        distT[x][y] = -1;
      }
      distT[TX][TY] = 0;
      que ~= Pt(TX, TY);
      for (; !que.empty; ) {
        const x = que.front.x;
        const y = que.front.y;
        que.removeFront;
        foreach (dir; 0 .. 4) {
          const xx = x + DX[dir];
          const yy = y + DY[dir];
          if (A[xx][yy] == '.') {
            if (distT[xx][yy] == -1) {
              distT[xx][yy] = distT[x][y] + 1;
              que ~= Pt(xx, yy);
            }
          }
        }
      }
      
      int ans;
      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;
        }
        bool good;
        foreach (x; 1 .. H - 1) foreach (y; 1 .. W - 1) {
          if (A[x][y] == '.' && !on[x][y]) {
            if (dist[TX][TY] == dist[x][y] + distT[x][y]) {
              good = true;
            }
          }
        }
        if (good) {
          ans = 2 * dist[TX][TY];
        } else {
          bool ok;
          foreach (x; 1 .. H - 1) foreach (y; 1 .. W - 1) {
            if (on[x][y] && !(x == SX && y == SY) && !(x == TX && y == TY)) {
              foreach (dir; 0 .. 4) {
                const xx = x + DX[dir];
                const yy = y + DY[dir];
                if (A[xx][yy] == '.' && !on[xx][yy]) {
                  ok = true;
                }
              }
            }
          }
          if (ok) {
            ans = 2 * dist[TX][TY] + 2;
          } else {
            ans = -1;
          }
        }
      } else {
        ans = -1;
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0