結果
問題 | No.367 ナイトの転身 |
ユーザー | roaris |
提出日時 | 2019-11-01 15:42:07 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 275 ms / 2,000 ms |
コード長 | 1,901 bytes |
コンパイル時間 | 1,109 ms |
コンパイル使用メモリ | 86,920 KB |
実行使用メモリ | 88,280 KB |
最終ジャッジ日時 | 2023-10-13 00:49:39 |
合計ジャッジ時間 | 6,410 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 94 ms
71,620 KB |
testcase_01 | AC | 94 ms
71,660 KB |
testcase_02 | AC | 93 ms
71,688 KB |
testcase_03 | AC | 91 ms
71,708 KB |
testcase_04 | AC | 93 ms
71,796 KB |
testcase_05 | AC | 95 ms
71,780 KB |
testcase_06 | AC | 96 ms
72,332 KB |
testcase_07 | AC | 95 ms
71,404 KB |
testcase_08 | AC | 91 ms
71,648 KB |
testcase_09 | AC | 90 ms
71,660 KB |
testcase_10 | AC | 199 ms
84,288 KB |
testcase_11 | AC | 275 ms
88,280 KB |
testcase_12 | AC | 222 ms
81,988 KB |
testcase_13 | AC | 208 ms
81,620 KB |
testcase_14 | AC | 218 ms
81,604 KB |
testcase_15 | AC | 143 ms
78,316 KB |
testcase_16 | AC | 205 ms
80,880 KB |
testcase_17 | AC | 152 ms
78,388 KB |
testcase_18 | AC | 157 ms
78,152 KB |
testcase_19 | AC | 161 ms
78,588 KB |
testcase_20 | AC | 138 ms
78,316 KB |
testcase_21 | AC | 178 ms
79,204 KB |
testcase_22 | AC | 103 ms
76,824 KB |
testcase_23 | AC | 129 ms
78,084 KB |
testcase_24 | AC | 132 ms
78,200 KB |
testcase_25 | AC | 124 ms
77,344 KB |
testcase_26 | AC | 113 ms
77,124 KB |
ソースコード
import sys input = sys.stdin.readline from collections import deque def bfs(sx, sy): dist = [[[-1] * W for _ in range(H)] for _ in range(2)] dist[0][sx][sy] = 0 q = deque([]) q.append((sx, sy, 0)) while q: cx, cy, f = q.popleft() if f == 0: for nx, ny in [(cx+1, cy+2), (cx+1, cy-2), (cx+2, cy+1), (cx+2, cy-1), (cx-1, cy+2), (cx-1, cy-2), (cx-2, cy+1), (cx-2, cy-1)]: if 0<=nx<H and 0<=ny<W and S[nx][ny] == 'R': if dist[1][nx][ny] == -1: dist[1][nx][ny] = dist[0][cx][cy]+1 q.append((nx, ny, 1)) elif 0<=nx<H and 0<=ny<W and S[nx][ny] != 'R': if dist[0][nx][ny] == -1: dist[0][nx][ny] = dist[0][cx][cy]+1 q.append((nx, ny, 0)) elif f == 1: for nx, ny in [(cx+1, cy+1), (cx+1, cy-1), (cx-1, cy+1), (cx-1, cy-1)]: if 0<=nx<H and 0<=ny<W and S[nx][ny] == 'R': if dist[0][nx][ny] == -1: dist[0][nx][ny] = dist[1][cx][cy]+1 q.append((nx, ny, 0)) elif 0<=nx<H and 0<=ny<W and S[nx][ny] != 'R': if dist[1][nx][ny] == -1: dist[1][nx][ny] = dist[1][cx][cy]+1 q.append((nx, ny, 1)) return dist H, W = map(int, input().split()) S = [input() for _ in range(H)] for i in range(H): for j in range(W): if S[i][j] == 'S': sx, sy = i, j if S[i][j] == 'G': gx, gy = i, j dist = bfs(sx, sy) if dist[0][gx][gy] == -1 and dist[1][gx][gy] == -1: print(-1) elif dist[0][gx][gy] == -1: print(dist[1][gx][gy]) elif dist[1][gx][gy] == -1: print(dist[0][gx][gy]) else: print(min(dist[0][gx][gy], dist[1][gx][gy]))