結果

問題 No.2328 Build Walls
ユーザー tentententen
提出日時 2023-07-27 09:30:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,055 ms / 3,000 ms
コード長 3,187 bytes
コンパイル時間 6,946 ms
コンパイル使用メモリ 84,364 KB
実行使用メモリ 64,652 KB
最終ジャッジ日時 2024-04-14 22:12:22
合計ジャッジ時間 19,722 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,320 KB
testcase_01 AC 53 ms
50,708 KB
testcase_02 AC 53 ms
50,552 KB
testcase_03 AC 53 ms
50,304 KB
testcase_04 AC 54 ms
50,500 KB
testcase_05 AC 56 ms
50,272 KB
testcase_06 AC 57 ms
50,492 KB
testcase_07 AC 54 ms
50,228 KB
testcase_08 AC 53 ms
50,556 KB
testcase_09 AC 53 ms
50,572 KB
testcase_10 AC 54 ms
50,688 KB
testcase_11 AC 54 ms
50,152 KB
testcase_12 AC 55 ms
50,516 KB
testcase_13 AC 297 ms
58,072 KB
testcase_14 AC 505 ms
57,960 KB
testcase_15 AC 476 ms
57,932 KB
testcase_16 AC 223 ms
57,684 KB
testcase_17 AC 480 ms
60,176 KB
testcase_18 AC 108 ms
51,900 KB
testcase_19 AC 140 ms
55,360 KB
testcase_20 AC 155 ms
55,408 KB
testcase_21 AC 243 ms
57,820 KB
testcase_22 AC 708 ms
61,076 KB
testcase_23 AC 1,023 ms
62,964 KB
testcase_24 AC 1,029 ms
64,652 KB
testcase_25 AC 1,055 ms
62,856 KB
testcase_26 AC 824 ms
62,328 KB
testcase_27 AC 978 ms
62,828 KB
testcase_28 AC 347 ms
62,860 KB
testcase_29 AC 1,039 ms
62,648 KB
testcase_30 AC 399 ms
62,680 KB
testcase_31 AC 377 ms
62,684 KB
testcase_32 AC 911 ms
62,588 KB
testcase_33 AC 835 ms
62,800 KB
testcase_34 AC 398 ms
62,552 KB
testcase_35 AC 848 ms
62,868 KB
testcase_36 AC 55 ms
50,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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();
    }
    
}
0