結果
| 問題 |
No.3199 Key-Door Grid
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-07-11 21:47:49 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,666 bytes |
| コンパイル時間 | 3,883 ms |
| コンパイル使用メモリ | 83,224 KB |
| 実行使用メモリ | 68,472 KB |
| 最終ジャッジ日時 | 2025-07-11 21:48:23 |
| 合計ジャッジ時間 | 21,085 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 WA * 2 |
ソースコード
import java.io.*;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
int m = sc.nextInt(), n = sc.nextInt(), M = sc.nextInt();
char[][] s = new char[m][n];
for (int i = 0; i < m; i++) {
s[i] = sc.next().toCharArray();
}
int sx = -1, sy = -1, tx = -1, ty = -1;
int[][] dir = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
PriorityQueue<int[]> p = new PriorityQueue<>((o1, o2) -> o1[2] - o2[2]);//x y dis state
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'S') {
sx = i;
sy = j;
p.offer(new int[]{sx, sy, 0, 0});
} else if (s[i][j] == 'T') {
tx = i;
ty = j;
}
}
}
int[][][] dis = new int[m][n][M + 1];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k <= M; k++) {
dis[i][j][k] = 1 << 30;
}
}
}
dis[sx][sy][0] = 0;
int ans = -1;
o:
while (!p.isEmpty()) {
int[] t = p.poll();
int x = t[0], y = t[1], d = t[2], state = t[3];
for (int[] dd : dir) {
int xx = x + dd[0], yy = y + dd[1];
if (xx < 0 || xx >= m || yy < 0 || yy >= n) continue;
if (s[xx][yy] == '#') continue;
if (s[xx][yy] == 'G') {
ans = d + 1;
break o;
}
if (s[xx][yy] == '.') {
if (dis[xx][yy][state] > d + 1) {
dis[xx][yy][state] = d + 1;
p.offer(new int[]{xx, yy, d + 1, state});
}
}
if (Character.isDigit(s[xx][yy])) {
int digit = s[xx][yy] - '0';
if (dis[xx][yy][digit] > d + 1) {
dis[xx][yy][digit] = d + 1;
p.offer(new int[]{xx, yy, d + 1, digit});
}
}
if (Character.isLetter(s[xx][yy])) {
int idx = s[xx][yy] - 'a' + 1;
if (state == idx) {
if(dis[xx][yy][state] > d + 1) {
dis[xx][yy][state] = d + 1;
p.offer(new int[]{xx, yy, d + 1, state});
}
}
}
}
}
out.println(ans);
out.close();
}
static Kattio sc = new Kattio();
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static class Kattio {
static BufferedReader r;
static StringTokenizer st;
public Kattio() {
r = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
try {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(r.readLine());
}
return st.nextToken();
} catch (Exception e) {
return null;
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}