結果
| 問題 | No.367 ナイトの転身 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-02-05 07:46:46 |
| 言語 | D (dmd 2.111.0) |
| 結果 |
AC
|
| 実行時間 | 530 ms / 2,000 ms |
| コード長 | 1,677 bytes |
| 記録 | |
| コンパイル時間 | 1,118 ms |
| コンパイル使用メモリ | 135,412 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-13 04:09:43 |
| 合計ジャッジ時間 | 3,610 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;
const int[][] dr = [[-2, -2, -1, -1, 1, 1, 2, 2], [-1, -1, 1, 1]];
const int[][] dc = [[-1, 1, -2, 2, -2, 2, -1, 1], [-1, 1, -1, 1]];
void main() {
auto s = readln.split.map!(to!int);
auto H = s[0];
auto W = s[1];
auto A = H.iota.map!(_ => readln.chomp).array;
int sr, sc, gr, gc;
foreach (i; 0..H) {
foreach (j; 0..W) {
if (A[i][j] == 'S') {
sr = i;
sc = j;
} else if (A[i][j] == 'G') {
gr = i;
gc = j;
}
}
}
auto dist = new int[][][](2, H, W);
foreach (i; 0..2) foreach (j; 0..H) dist[i][j][] = 1 << 29;
auto pq = new BinaryHeap!(Array!(Tuple!(int, int, int, int)), "a[3] > b[3]");
pq.insert(tuple(0, sr, sc, 0));
while (!pq.empty) {
int k = pq.front[0];
int r = pq.front[1];
int c = pq.front[2];
int d = pq.front[3];
pq.removeFront;
if (dist[k][r][c] <= d) continue;
dist[k][r][c] = d;
foreach (i; 0..dr[k].length) {
int nr = r + dr[k][i];
int nc = c + dc[k][i];
if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
int nk = A[nr][nc] == 'R' ? k ^ 1 : k;
int nd = d + 1;
if (dist[nk][nr][nc] <= nd) continue;
pq.insert(tuple(nk, nr, nc, nd));
}
}
int ans = min(dist[0][gr][gc], dist[1][gr][gc]);
writeln(ans == 1 << 29 ? -1 : ans);
}