結果

問題 No.1323 うしらずSwap
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-20 00:32:52
言語 D
(dmd 2.106.1)
結果
RE  
実行時間 -
コード長 5,176 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 111,152 KB
実行使用メモリ 72,080 KB
最終ジャッジ日時 2023-09-04 11:41:22
合計ジャッジ時間 10,252 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 RE -
testcase_02 AC 2 ms
4,384 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 2 ms
4,384 KB
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 AC 1 ms
4,380 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,388 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 117 ms
70,632 KB
testcase_17 AC 286 ms
68,808 KB
testcase_18 AC 119 ms
69,896 KB
testcase_19 AC 286 ms
70,416 KB
testcase_20 AC 117 ms
70,600 KB
testcase_21 AC 116 ms
70,864 KB
testcase_22 AC 279 ms
69,576 KB
testcase_23 AC 117 ms
70,928 KB
testcase_24 AC 264 ms
70,100 KB
testcase_25 AC 117 ms
69,180 KB
testcase_26 RE -
testcase_27 RE -
testcase_28 AC 141 ms
70,160 KB
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 AC 280 ms
70,112 KB
testcase_34 AC 258 ms
68,656 KB
testcase_35 AC 256 ms
70,952 KB
testcase_36 AC 254 ms
69,328 KB
testcase_37 AC 248 ms
70,156 KB
testcase_38 AC 247 ms
70,432 KB
testcase_39 AC 254 ms
70,152 KB
testcase_40 AC 253 ms
70,440 KB
testcase_41 AC 256 ms
70,160 KB
testcase_42 AC 244 ms
69,560 KB
testcase_43 AC 85 ms
70,588 KB
testcase_44 AC 123 ms
69,900 KB
testcase_45 AC 113 ms
71,324 KB
testcase_46 AC 129 ms
70,160 KB
testcase_47 AC 93 ms
69,352 KB
testcase_48 RE -
testcase_49 RE -
testcase_50 RE -
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
testcase_54 RE -
testcase_55 RE -
testcase_56 RE -
testcase_57 RE -
testcase_58 RE -
testcase_59 AC 4 ms
4,380 KB
testcase_60 AC 1 ms
4,380 KB
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];

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); }
      }
      
      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("distS = ", distS);
        writeln("distT = ", distT);
      }
      
      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;
        }
        ans = INF;
        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]);
            }
          }
        }
        if (ans >= INF) {
          ans = -1;
        }
      } else {
        ans = -1;
      }
if(ans==-1){import core.stdc.stdlib;exit(1);}
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0