結果
問題 | No.1323 うしらずSwap |
ユーザー | 👑 hos.lyric |
提出日時 | 2020-12-20 01:15:22 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 610 ms / 3,000 ms |
コード長 | 3,589 bytes |
コンパイル時間 | 1,236 ms |
コンパイル使用メモリ | 132,212 KB |
実行使用メモリ | 81,580 KB |
最終ジャッジ日時 | 2024-06-22 10:30:41 |
合計ジャッジ時間 | 17,090 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,940 KB |
testcase_05 | AC | 1 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 2 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,940 KB |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | AC | 1 ms
6,944 KB |
testcase_11 | AC | 1 ms
6,940 KB |
testcase_12 | AC | 1 ms
6,940 KB |
testcase_13 | AC | 1 ms
6,948 KB |
testcase_14 | AC | 1 ms
6,944 KB |
testcase_15 | AC | 1 ms
6,940 KB |
testcase_16 | AC | 244 ms
79,876 KB |
testcase_17 | AC | 500 ms
81,396 KB |
testcase_18 | AC | 242 ms
79,876 KB |
testcase_19 | AC | 501 ms
80,380 KB |
testcase_20 | AC | 241 ms
80,228 KB |
testcase_21 | AC | 241 ms
79,948 KB |
testcase_22 | AC | 488 ms
79,992 KB |
testcase_23 | AC | 240 ms
80,432 KB |
testcase_24 | AC | 483 ms
80,116 KB |
testcase_25 | AC | 240 ms
79,220 KB |
testcase_26 | AC | 213 ms
80,504 KB |
testcase_27 | AC | 228 ms
79,584 KB |
testcase_28 | AC | 295 ms
80,444 KB |
testcase_29 | AC | 203 ms
80,556 KB |
testcase_30 | AC | 411 ms
79,288 KB |
testcase_31 | AC | 203 ms
79,148 KB |
testcase_32 | AC | 418 ms
80,860 KB |
testcase_33 | AC | 610 ms
81,104 KB |
testcase_34 | AC | 607 ms
79,332 KB |
testcase_35 | AC | 603 ms
80,172 KB |
testcase_36 | AC | 590 ms
79,584 KB |
testcase_37 | AC | 603 ms
79,212 KB |
testcase_38 | AC | 596 ms
78,884 KB |
testcase_39 | AC | 607 ms
81,580 KB |
testcase_40 | AC | 598 ms
79,508 KB |
testcase_41 | AC | 603 ms
80,044 KB |
testcase_42 | AC | 596 ms
79,464 KB |
testcase_43 | AC | 162 ms
79,288 KB |
testcase_44 | AC | 231 ms
79,996 KB |
testcase_45 | AC | 221 ms
79,576 KB |
testcase_46 | AC | 230 ms
81,576 KB |
testcase_47 | AC | 179 ms
80,400 KB |
testcase_48 | AC | 142 ms
80,344 KB |
testcase_49 | AC | 202 ms
79,640 KB |
testcase_50 | AC | 129 ms
79,256 KB |
testcase_51 | AC | 80 ms
80,220 KB |
testcase_52 | AC | 101 ms
79,616 KB |
testcase_53 | AC | 197 ms
79,784 KB |
testcase_54 | AC | 102 ms
79,300 KB |
testcase_55 | AC | 194 ms
79,132 KB |
testcase_56 | AC | 88 ms
79,752 KB |
testcase_57 | AC | 221 ms
79,672 KB |
testcase_58 | AC | 2 ms
6,944 KB |
testcase_59 | AC | 4 ms
6,940 KB |
testcase_60 | AC | 2 ms
6,940 KB |
testcase_61 | AC | 1 ms
6,940 KB |
ソースコード
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 INF = 10^^9 / 2; int H, W, SX, SY, TX, TY; string[] A; alias Pt = Tuple!(int, "x", int, "y"); Pt[] que; Tuple!(int[][], Pt[][]) bfs(bool needPrev)(int sx, int sy, int ox, int oy) { int qb, qe; auto dist = new int[][](H, W); Pt[][] prev; static if (needPrev) prev = new Pt[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { dist[x][y] = INF; } dist[sx][sy] = 0; que[qe++] = Pt(sx, sy); for (; qb != qe; ) { const x = que[qb].x; const y = que[qb].y; ++qb; void update(int xx, int yy) { if (A[xx][yy] == '.' && !(xx == ox && yy == oy)) { if (chmin(dist[xx][yy], dist[x][y] + 1)) { static if (needPrev) prev[xx][yy] = Pt(x, y); que[qe++] = Pt(xx, yy); } } } update(x + 1, y); update(x, y + 1); update(x - 1, y); update(x, y - 1); } return tuple(dist, prev); } void main() { try { for (; ; ) { H = readInt(); W = readInt(); SX = readInt() - 1; SY = readInt() - 1; TX = readInt() - 1; TY = readInt() - 1; A = new string[H]; foreach (x; 0 .. H) { A[x] = readToken(); } que = new Pt[H * W]; const res = bfs!true(SX, SY, -1, -1); const dS = res[0]; const prev = res[1]; const dT = bfs!false(TX, TY, -1, -1)[0]; const dST = bfs!false(SX, SY, TX, TY)[0]; const dTS = bfs!false(TX, TY, SX, SY)[0]; int ans = INF; if (dS[TX][TY] < INF) { 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]) { chmin(ans, dS[TX][TY] + dST[x][y] + dTS[x][y]); } } foreach (x; 1 .. H - 1) foreach (y; 1 .. W - 1) { if (A[x][y] == '.') { int cnt; if (A[x + 1][y] == '.') ++cnt; if (A[x][y + 1] == '.') ++cnt; if (A[x - 1][y] == '.') ++cnt; if (A[x][y - 1] == '.') ++cnt; if (cnt >= 3) { chmin(ans, 2 * (dS[x][y] + dT[x][y] + 2)); } } } } writeln((ans >= INF) ? -1 : ans); } } catch (EOFException e) { } }