結果

問題 No.2328 Build Walls
ユーザー tentententen
提出日時 2023-05-30 14:55:49
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,455 ms / 3,000 ms
コード長 3,624 bytes
コンパイル時間 4,291 ms
コンパイル使用メモリ 85,568 KB
実行使用メモリ 55,900 KB
最終ジャッジ日時 2023-08-28 00:38:16
合計ジャッジ時間 21,837 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
33,468 KB
testcase_01 AC 43 ms
33,440 KB
testcase_02 AC 42 ms
33,588 KB
testcase_03 AC 41 ms
33,440 KB
testcase_04 AC 42 ms
33,796 KB
testcase_05 AC 42 ms
33,536 KB
testcase_06 AC 42 ms
33,584 KB
testcase_07 AC 43 ms
33,676 KB
testcase_08 AC 43 ms
33,904 KB
testcase_09 AC 41 ms
33,580 KB
testcase_10 AC 41 ms
33,984 KB
testcase_11 AC 42 ms
33,668 KB
testcase_12 AC 42 ms
33,476 KB
testcase_13 AC 278 ms
43,760 KB
testcase_14 AC 680 ms
51,384 KB
testcase_15 AC 684 ms
50,692 KB
testcase_16 AC 203 ms
41,920 KB
testcase_17 AC 669 ms
51,812 KB
testcase_18 AC 95 ms
36,220 KB
testcase_19 AC 127 ms
39,028 KB
testcase_20 AC 181 ms
40,576 KB
testcase_21 AC 226 ms
43,312 KB
testcase_22 AC 1,023 ms
53,448 KB
testcase_23 AC 1,360 ms
55,604 KB
testcase_24 AC 1,418 ms
55,840 KB
testcase_25 AC 1,455 ms
55,632 KB
testcase_26 AC 1,211 ms
55,732 KB
testcase_27 AC 1,224 ms
55,336 KB
testcase_28 AC 310 ms
45,460 KB
testcase_29 AC 1,298 ms
54,372 KB
testcase_30 AC 352 ms
45,408 KB
testcase_31 AC 326 ms
45,240 KB
testcase_32 AC 1,215 ms
55,900 KB
testcase_33 AC 1,301 ms
51,176 KB
testcase_34 AC 379 ms
46,924 KB
testcase_35 AC 1,088 ms
50,308 KB
testcase_36 AC 43 ms
33,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int MOD;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt() - 2;
        int w = sc.nextInt();
        int[][] field = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                field[i][j] = sc.nextInt();
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        for (int i = 0; i < h; i++) {
            queue.add(new Path(i * w, 0));
        }
        int[] costs = new int[h * w];
        Arrays.fill(costs, Integer.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            int r = p.idx / w;
            int c = p.idx % w;
            if (field[r][c] < 0) {
                continue;
            }
            p.value += field[r][c];
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            if (c >= w - 1) {
                continue;
            }
            if (r > 0) {
                queue.add(new Path(p.idx - w + 1, p.value));
                queue.add(new Path(p.idx - w, p.value));
                if (c > 0) {
                    queue.add(new Path(p.idx - w - 1, p.value));
                }
            }
            queue.add(new Path(p.idx + 1, p.value));
            if (c > 0) {
                queue.add(new Path(p.idx - 1, p.value));
            }
            if (r < h - 1) {
                queue.add(new Path(p.idx + w + 1, p.value));
                queue.add(new Path(p.idx + w, p.value));
                if (c > 0) {
                    queue.add(new Path(p.idx + w - 1, p.value));
                }
            }
        }
        int ans = Integer.MAX_VALUE;
        for (int i = w - 1; i < h * w; i += w) {
            ans = Math.min(ans, costs[i]);
        }
        if (ans == Integer.MAX_VALUE) {
            ans = -1;
        }
        System.out.println(ans);
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            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