結果

問題 No.1572 XI
ユーザー tentententen
提出日時 2023-04-12 13:19:37
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 4,524 bytes
コンパイル時間 2,561 ms
コンパイル使用メモリ 95,988 KB
実行使用メモリ 128,828 KB
最終ジャッジ日時 2024-04-17 02:25:26
合計ジャッジ時間 33,240 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
42,676 KB
testcase_01 AC 44 ms
50,292 KB
testcase_02 AC 48 ms
37,036 KB
testcase_03 AC 44 ms
36,980 KB
testcase_04 AC 861 ms
61,032 KB
testcase_05 AC 1,557 ms
81,608 KB
testcase_06 AC 1,810 ms
100,272 KB
testcase_07 AC 135 ms
44,916 KB
testcase_08 AC 1,552 ms
82,564 KB
testcase_09 AC 914 ms
63,224 KB
testcase_10 AC 1,267 ms
69,400 KB
testcase_11 AC 184 ms
47,408 KB
testcase_12 AC 703 ms
56,160 KB
testcase_13 AC 243 ms
48,440 KB
testcase_14 AC 721 ms
55,440 KB
testcase_15 AC 199 ms
47,616 KB
testcase_16 AC 591 ms
53,624 KB
testcase_17 AC 133 ms
44,392 KB
testcase_18 AC 756 ms
61,080 KB
testcase_19 AC 1,683 ms
97,996 KB
testcase_20 AC 1,121 ms
68,344 KB
testcase_21 AC 1,923 ms
102,708 KB
testcase_22 AC 1,499 ms
80,156 KB
testcase_23 AC 123 ms
44,104 KB
testcase_24 AC 47 ms
37,236 KB
testcase_25 AC 46 ms
37,020 KB
testcase_26 AC 48 ms
37,044 KB
testcase_27 AC 49 ms
37,172 KB
testcase_28 AC 47 ms
37,236 KB
testcase_29 AC 47 ms
36,980 KB
testcase_30 AC 46 ms
37,144 KB
testcase_31 AC 44 ms
37,164 KB
testcase_32 AC 45 ms
36,940 KB
testcase_33 AC 45 ms
37,220 KB
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 TLE -
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
testcase_45 TLE -
testcase_46 TLE -
testcase_47 AC 307 ms
111,228 KB
testcase_48 TLE -
権限があれば一括ダウンロードができます

ソースコード

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();
        int w = sc.nextInt();
        int srr = sc.nextInt() - 1;
        int scc = sc.nextInt() - 1;
        int grr = sc.nextInt() - 1;
        int gcc = sc.nextInt() - 1;
        char[][] field = new char[h][];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(srr, scc, 0, 0));
        int[][][] costs = new int[h][w][6];
        for (int[][] arr1 : costs) {
            for (int[] arr2 : arr1) {
                Arrays.fill(arr2, Integer.MAX_VALUE);
            }
        }
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.r][p.c][p.type] <= p.value || field[p.r][p.c] == '#') {
                continue;
            }
            costs[p.r][p.c][p.type] = p.value;
            if (p.r > 0) {
                queue.add(p.add(-1, 0));
            }
            if (p.r < h - 1) {
                queue.add(p.add(1, 0));
            }
            if (p.c > 0) {
                queue.add(p.add(0, -1));
            }
            if (p.c < w - 1) {
                queue.add(p.add(0, 1));
            }
        }
        if (costs[grr][gcc][0] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(costs[grr][gcc][0]);
        }
    }
    
    static class Path implements Comparable<Path> {
        int r;
        int c;
        int type;
        int value;
        
        public Path(int r, int c, int type, int value) {
            this.r = r;
            this.c = c;
            this.type = type;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
        
        public Path add(int x, int y) {
            int next;
            if (x == -1) {
                if (type < 4) {
                    next = (type + 3) % 4;
                } else {
                    next = type;
                }
            } else if (x == 1) {
                if (type < 4) {
                    next = (type + 1) % 4;
                } else {
                    next = type;
                }
            } else if (y == -1) {
                if (type == 1 || type == 3) {
                    next = type;
                } else if (type == 0) {
                    next = 4;
                } else if (type == 2) {
                    next = 5;
                } else if (type == 4) {
                    next = 2;
                } else {
                    next = 0;
                }
            } else {
                if (type == 1 || type == 3) {
                    next = type;
                } else if (type == 0) {
                    next = 5;
                } else if (type == 2) {
                    next = 4;
                } else if (type == 4) {
                    next = 0;
                } else {
                    next = 2;
                }
            }
            return new Path(r + x, c + y, next, value + 1);
        }
    }
}
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