結果

問題 No.1572 XI
ユーザー tentententen
提出日時 2021-06-29 12:11:51
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,913 bytes
コンパイル時間 2,688 ms
コンパイル使用メモリ 75,168 KB
実行使用メモリ 133,768 KB
最終ジャッジ日時 2023-09-08 00:14:19
合計ジャッジ時間 28,762 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,508 KB
testcase_01 AC 43 ms
49,476 KB
testcase_02 AC 42 ms
49,552 KB
testcase_03 AC 42 ms
49,388 KB
testcase_04 AC 986 ms
71,464 KB
testcase_05 AC 1,838 ms
94,384 KB
testcase_06 TLE -
testcase_07 AC 153 ms
56,944 KB
testcase_08 AC 1,662 ms
96,672 KB
testcase_09 AC 973 ms
73,660 KB
testcase_10 AC 1,459 ms
80,612 KB
testcase_11 AC 232 ms
58,104 KB
testcase_12 AC 776 ms
66,044 KB
testcase_13 AC 275 ms
60,928 KB
testcase_14 AC 848 ms
65,820 KB
testcase_15 AC 251 ms
60,348 KB
testcase_16 AC 727 ms
65,560 KB
testcase_17 AC 147 ms
56,600 KB
testcase_18 AC 879 ms
71,808 KB
testcase_19 AC 1,883 ms
94,600 KB
testcase_20 AC 1,278 ms
78,960 KB
testcase_21 TLE -
testcase_22 AC 1,722 ms
92,640 KB
testcase_23 AC 133 ms
56,576 KB
testcase_24 AC 44 ms
49,612 KB
testcase_25 AC 45 ms
49,372 KB
testcase_26 AC 48 ms
50,904 KB
testcase_27 AC 44 ms
49,604 KB
testcase_28 AC 48 ms
49,644 KB
testcase_29 AC 47 ms
49,928 KB
testcase_30 AC 49 ms
49,928 KB
testcase_31 AC 44 ms
49,788 KB
testcase_32 AC 44 ms
49,424 KB
testcase_33 AC 45 ms
49,728 KB
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int h = sc.nextInt();
        int w = sc.nextInt();
        int sr = sc.nextInt() - 1;
        int sx = sc.nextInt() - 1;
        int gr = sc.nextInt() - 1;
        int gc = sc.nextInt() - 1;
        char[][] field = new char[h][];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
        }
        int[][][] costs = new int[h][w][6];
        for (int[][] arr : costs) {
            for (int[] arr1 : arr) {
                Arrays.fill(arr1, Integer.MAX_VALUE);
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(sr, sx, 0, 0));
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (field[p.r][p.c] == '#') {
                continue;
            }
            if (costs[p.r][p.c][p.idx] <= p.value) {
                continue;
            }
            costs[p.r][p.c][p.idx] = p.value;
            if (p.r > 0) {
                int nextIdx = p.idx;
                if (p.idx <= 2) {
                    nextIdx++;
                } else if(p.idx == 3) {
                    nextIdx = 0;
                }
                queue.add(new Path(p.r - 1, p.c, p.value + 1, nextIdx));
            }
            if (p.r < h - 1) {
                int nextIdx = p.idx;
                if (p.idx == 0) {
                    nextIdx = 3;
                } else if (p.idx <= 3) {
                    nextIdx--;
                }
                queue.add(new Path(p.r + 1, p.c, p.value + 1, nextIdx));
            }
            if (p.c > 0) {
                int nextIdx;
                if (p.idx == 0) {
                    nextIdx = 4;
                } else if (p.idx == 4) {
                    nextIdx = 2;
                } else if (p.idx == 2) {
                    nextIdx = 5;
                } else if (p.idx == 5) {
                    nextIdx = 0;
                } else {
                    nextIdx = p.idx;
                }
                queue.add(new Path(p.r, p.c - 1, p.value + 1, nextIdx));
            }
            if (p.c < w - 1) {
                int nextIdx;
                if (p.idx == 0) {
                    nextIdx = 5;
                } else if (p.idx == 5) {
                    nextIdx = 2;
                } else if (p.idx == 2) {
                    nextIdx = 4;
                } else if (p.idx == 4) {
                    nextIdx = 0;
                } else {
                    nextIdx = p.idx;
                }
                queue.add(new Path(p.r, p.c + 1, p.value + 1, nextIdx));
            }
        }
        if (costs[gr][gc][0] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(costs[gr][gc][0]);
        }
    }
    
    static class Path implements Comparable<Path> {
        int r;
        int c;
        int value;
        int idx;
        
        public Path(int r, int c, int value, int idx) {
            this.r = r;
            this.c = c;
            this.value = value;
            this.idx = idx;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}


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