結果

問題 No.124 門松列(3)
ユーザー tentententen
提出日時 2021-05-19 17:45:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 3,557 bytes
コンパイル時間 3,104 ms
コンパイル使用メモリ 81,296 KB
実行使用メモリ 57,100 KB
最終ジャッジ日時 2024-04-18 04:22:11
合計ジャッジ時間 7,760 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
57,100 KB
testcase_01 AC 54 ms
50,636 KB
testcase_02 AC 54 ms
50,516 KB
testcase_03 AC 134 ms
57,024 KB
testcase_04 AC 118 ms
56,992 KB
testcase_05 AC 121 ms
57,068 KB
testcase_06 AC 54 ms
50,240 KB
testcase_07 AC 55 ms
50,188 KB
testcase_08 AC 54 ms
50,460 KB
testcase_09 AC 54 ms
50,256 KB
testcase_10 AC 56 ms
50,128 KB
testcase_11 AC 56 ms
50,468 KB
testcase_12 AC 55 ms
50,492 KB
testcase_13 AC 55 ms
50,660 KB
testcase_14 AC 55 ms
50,460 KB
testcase_15 AC 57 ms
50,244 KB
testcase_16 AC 69 ms
51,156 KB
testcase_17 AC 57 ms
50,544 KB
testcase_18 AC 55 ms
50,528 KB
testcase_19 AC 57 ms
50,552 KB
testcase_20 AC 74 ms
50,576 KB
testcase_21 AC 56 ms
50,488 KB
testcase_22 AC 57 ms
50,488 KB
testcase_23 AC 97 ms
53,732 KB
testcase_24 AC 94 ms
53,892 KB
testcase_25 AC 96 ms
54,084 KB
testcase_26 AC 81 ms
51,452 KB
testcase_27 AC 63 ms
50,276 KB
testcase_28 AC 124 ms
56,732 KB
testcase_29 AC 107 ms
56,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int w = sc.nextInt();
        int h = 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<>();
        int[][][][] costs = new int[10][10][h][w];
        for (int[][][] arr1 : costs) {
            for (int[][] arr2 : arr1) {
                for (int[] arr3 : arr2) {
                    Arrays.fill(arr3, Integer.MAX_VALUE);
                }
            }
        }
        queue.add(new Path(0, 0, 0, 0, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (!isKadomatsu(p.prepre, p.pre, field[p.r][p.c]) || costs[p.prepre][p.pre][p.r][p.c] <= p.value) {
                continue;
            }
            costs[p.prepre][p.pre][p.r][p.c] = p.value;
            if (p.r > 0) {
                queue.add(new Path(p.r - 1, p.c, p.value + 1, p.pre, field[p.r][p.c]));
            }
            if (p.r < h - 1) {
                queue.add(new Path(p.r + 1, p.c, p.value + 1, p.pre, field[p.r][p.c]));
            }
            if (p.c > 0) {
                queue.add(new Path(p.r, p.c - 1, p.value + 1, p.pre, field[p.r][p.c]));
            }
            if (p.c < w - 1) {
                queue.add(new Path(p.r, p.c + 1, p.value + 1, p.pre, field[p.r][p.c]));
            }
        }
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                min = Math.min(min, costs[i][j][h - 1][w - 1]);
            }
        }
        if (min == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
    
    static boolean isKadomatsu(int prepre, int pre, int cur) {
        if (pre == 0) {
            return true;
        } else if (prepre == 0) {
            return pre != cur;
        } else {
            return prepre != cur && ((prepre < pre && pre > cur) || (prepre > pre && pre < cur));
        }
    }
    
    static class Path implements Comparable<Path> {
        int r;
        int c;
        int value;
        int prepre;
        int pre;
        
        public Path(int r, int c, int value, int prepre, int pre) {
            this.r = r;
            this.c = c;
            this.value = value;
            this.prepre = prepre;
            this.pre = pre;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
        
        public String toString() {
            return r + ":" + c + ":" + value + ":" + prepre + ":" + pre;
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0