結果
問題 | No.2328 Build Walls |
ユーザー | tenten |
提出日時 | 2023-07-27 09:30:16 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 914 ms / 3,000 ms |
コード長 | 3,187 bytes |
コンパイル時間 | 4,069 ms |
コンパイル使用メモリ | 85,040 KB |
実行使用メモリ | 62,860 KB |
最終ジャッジ日時 | 2024-10-04 02:03:25 |
合計ジャッジ時間 | 18,213 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 49 ms
50,452 KB |
testcase_01 | AC | 50 ms
50,408 KB |
testcase_02 | AC | 49 ms
50,504 KB |
testcase_03 | AC | 51 ms
50,532 KB |
testcase_04 | AC | 53 ms
50,680 KB |
testcase_05 | AC | 52 ms
50,564 KB |
testcase_06 | AC | 52 ms
50,044 KB |
testcase_07 | AC | 50 ms
50,220 KB |
testcase_08 | AC | 49 ms
50,348 KB |
testcase_09 | AC | 49 ms
50,140 KB |
testcase_10 | AC | 49 ms
50,380 KB |
testcase_11 | AC | 48 ms
50,164 KB |
testcase_12 | AC | 48 ms
50,424 KB |
testcase_13 | AC | 262 ms
57,916 KB |
testcase_14 | AC | 482 ms
58,064 KB |
testcase_15 | AC | 428 ms
57,944 KB |
testcase_16 | AC | 203 ms
57,616 KB |
testcase_17 | AC | 463 ms
59,996 KB |
testcase_18 | AC | 108 ms
51,840 KB |
testcase_19 | AC | 134 ms
55,520 KB |
testcase_20 | AC | 158 ms
55,288 KB |
testcase_21 | AC | 221 ms
57,608 KB |
testcase_22 | AC | 567 ms
60,844 KB |
testcase_23 | AC | 906 ms
62,836 KB |
testcase_24 | AC | 885 ms
62,600 KB |
testcase_25 | AC | 903 ms
62,772 KB |
testcase_26 | AC | 738 ms
62,860 KB |
testcase_27 | AC | 914 ms
62,436 KB |
testcase_28 | AC | 299 ms
62,644 KB |
testcase_29 | AC | 905 ms
62,688 KB |
testcase_30 | AC | 349 ms
62,580 KB |
testcase_31 | AC | 331 ms
62,572 KB |
testcase_32 | AC | 778 ms
62,360 KB |
testcase_33 | AC | 697 ms
62,384 KB |
testcase_34 | AC | 365 ms
62,788 KB |
testcase_35 | AC | 753 ms
62,472 KB |
testcase_36 | AC | 50 ms
50,416 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int h = sc.nextInt() - 2; int w = sc.nextInt(); int[][] fields = new int[h][w]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { fields[i][j] = sc.nextInt(); } } PriorityQueue<Path> queue = new PriorityQueue<>(); for (int i = 0; i < h; i++) { queue.add(new Path(i, 0, 0)); } int[][] costs = new int[h][w]; for (int[] arr : costs) { Arrays.fill(arr, Integer.MAX_VALUE); } while (queue.size() > 0) { Path p = queue.poll(); p.value += fields[p.r][p.c]; if (costs[p.r][p.c] <= p.value || fields[p.r][p.c] == -1) { continue; } costs[p.r][p.c] = p.value; for (int i = Math.max(0, p.r - 1); i <= p.r + 1 && i < h; i++) { for (int j = Math.max(0, p.c - 1); j <= p.c + 1 && j < w; j++) { if (costs[i][j] == Integer.MAX_VALUE) { queue.add(new Path(i, j, p.value)); } } } } int ans = Integer.MAX_VALUE; for (int[] arr : costs) { ans = Math.min(ans, arr[w - 1]); } if (ans == Integer.MAX_VALUE) { System.out.println(-1); } else { System.out.println(ans); } } static class Path implements Comparable<Path> { int r; int c; int value; public Path(int r, int c, int value) { this.r = r; this.c = c; this.value = value; } public int compareTo(Path another) { return value - another.value; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }