結果
問題 | No.367 ナイトの転身 |
ユーザー | te-sh |
提出日時 | 2017-07-18 17:26:36 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 417 ms / 2,000 ms |
コード長 | 2,995 bytes |
コンパイル時間 | 1,250 ms |
コンパイル使用メモリ | 118,912 KB |
実行使用メモリ | 10,696 KB |
最終ジャッジ日時 | 2024-06-12 21:00:23 |
合計ジャッジ時間 | 2,778 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,944 KB |
testcase_02 | AC | 1 ms
6,940 KB |
testcase_03 | AC | 1 ms
6,940 KB |
testcase_04 | AC | 1 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 1 ms
6,940 KB |
testcase_07 | AC | 1 ms
6,944 KB |
testcase_08 | AC | 1 ms
6,944 KB |
testcase_09 | AC | 1 ms
6,944 KB |
testcase_10 | AC | 241 ms
10,696 KB |
testcase_11 | AC | 417 ms
10,508 KB |
testcase_12 | AC | 24 ms
6,944 KB |
testcase_13 | AC | 34 ms
6,940 KB |
testcase_14 | AC | 101 ms
10,484 KB |
testcase_15 | AC | 4 ms
6,940 KB |
testcase_16 | AC | 98 ms
10,488 KB |
testcase_17 | AC | 16 ms
6,940 KB |
testcase_18 | AC | 21 ms
6,940 KB |
testcase_19 | AC | 19 ms
6,940 KB |
testcase_20 | AC | 4 ms
6,940 KB |
testcase_21 | AC | 21 ms
6,944 KB |
testcase_22 | AC | 1 ms
6,944 KB |
testcase_23 | AC | 1 ms
6,944 KB |
testcase_24 | AC | 3 ms
6,940 KB |
testcase_25 | AC | 3 ms
6,944 KB |
testcase_26 | AC | 2 ms
6,940 KB |
ソースコード
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[i][j] = 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)); } }