結果
問題 | No.367 ナイトの転身 |
ユーザー | te-sh |
提出日時 | 2017-07-18 17:25:32 |
言語 | D (dmd 2.106.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,995 bytes |
コンパイル時間 | 955 ms |
コンパイル使用メモリ | 119,956 KB |
実行使用メモリ | 10,592 KB |
最終ジャッジ日時 | 2024-06-12 21:00:20 |
合計ジャッジ時間 | 4,725 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | RE | - |
testcase_04 | RE | - |
testcase_05 | AC | 1 ms
6,944 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,940 KB |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | AC | 235 ms
10,484 KB |
testcase_11 | AC | 391 ms
10,552 KB |
testcase_12 | WA | - |
testcase_13 | AC | 32 ms
6,944 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | AC | 89 ms
10,572 KB |
testcase_17 | AC | 15 ms
6,940 KB |
testcase_18 | AC | 19 ms
6,940 KB |
testcase_19 | RE | - |
testcase_20 | AC | 3 ms
6,940 KB |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | AC | 2 ms
6,944 KB |
testcase_26 | RE | - |
ソースコード
import std.algorithm, std.conv, std.range, std.stdio, std.string; import std.container; // SList, DList, BinaryHeap alias Point!int point; alias Grid!(bool, int) grid; void main() { auto rd = readln.split.to!(size_t[]), h = rd[0], w = rd[1]; auto red = grid(h, w), visit = new grid[](2); foreach (i; 0..2) visit[i] = grid(h, w); point s, g; foreach (i; 0..h) foreach (j, c; readln.chomp) { switch (c) { case 'S': s = point(j.to!int, i.to!int); break; case 'G': g = point(j.to!int, i.to!int); break; case 'R': red[j][i] = true; break; default: break; } } struct Qitem { point p; bool knight; int len; } auto q = DList!Qitem(Qitem(s, true, 0)); visit[true][s] = true; while (!q.empty) { auto qi = q.front; q.removeFront(); foreach (np; qi.knight ? red.sibPointsKnight(qi.p).array : red.sibPointsBishop(qi.p).array) { if (np == g) { writeln(qi.len + 1); return; } auto knight = qi.knight ^ red[np]; if (!visit[knight][np]) { visit[knight][np] = true; q.insertBack(Qitem(np, knight, qi.len + 1)); } } } writeln(-1); } struct Point(T) { T x, y; pure auto opBinary(string op: "+")(Point!T rhs) const { return Point!T(x + rhs.x, y + rhs.y); } pure auto opBinary(string op: "-")(Point!T rhs) const { return Point!T(x - rhs.x, y - rhs.y); } } struct Grid(T, U) { import std.algorithm, std.conv, std.range, std.traits, std.typecons; const sibsKnight = [Point!U(-1, -2), Point!U(-2, -1), Point!U(-1, 2), Point!U(-2, 1), Point!U(1, 2), Point!U(2, 1), Point!U(1, -2), Point!U(2, -1)]; const sibsBishop = [Point!U(-1, -1), Point!U(-1, 1), Point!U(1, 1), Point!U(1, -1)]; T[][] m; const size_t rows, cols; mixin Proxy!m; this(size_t r, size_t c) { rows = r; cols = c; m = new T[][](rows, cols); } this(T[][] s) { rows = s.length; cols = s[0].length; m = s; } pure auto dup() const { return Grid(m.map!(r => r.dup).array); } ref pure auto opIndex(Point!U p) { return m[p.y][p.x]; } ref pure auto opIndex(size_t y) { return m[y]; } ref pure auto opIndex(size_t y, size_t x) const { return m[y][x]; } static if (isAssignable!T) { auto opIndexAssign(T v, Point!U p) { return m[p.y][p.x] = v; } auto opIndexAssign(T v, size_t y, size_t x) { return m[y][x] = v; } auto opIndexOpAssign(string op, V)(V v, Point!U p) { return mixin("m[p.y][p.x] " ~ op ~ "= v"); } auto opIndexOpAssign(string op, V)(V v, size_t y, size_t x) { return mixin("m[y][x] " ~ op ~ "= v"); } } pure auto validPoint(Point!U p) { return p.x >= 0 && p.x < cols && p.y >= 0 && p.y < rows; } pure auto points() const { return rows.to!U.iota.map!(y => cols.to!U.iota.map!(x => Point!U(x, y))).joiner; } pure auto sibPointsKnight(Point!U p) { return sibsKnight.map!(s => p + s).filter!(p => validPoint(p)); } pure auto sibPointsBishop(Point!U p) { return sibsBishop.map!(s => p + s).filter!(p => validPoint(p)); } }