結果

問題 No.424 立体迷路
ユーザー tentententen
提出日時 2023-01-06 12:21:12
言語 Java21
(openjdk 21)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 3,546 bytes
コンパイル時間 3,441 ms
コンパイル使用メモリ 93,452 KB
実行使用メモリ 37,196 KB
最終ジャッジ日時 2024-05-07 14:01:04
合計ジャッジ時間 5,812 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
36,416 KB
testcase_01 AC 57 ms
36,916 KB
testcase_02 AC 48 ms
37,196 KB
testcase_03 AC 45 ms
36,800 KB
testcase_04 AC 46 ms
36,912 KB
testcase_05 AC 45 ms
36,528 KB
testcase_06 AC 45 ms
37,196 KB
testcase_07 AC 45 ms
36,868 KB
testcase_08 AC 46 ms
36,688 KB
testcase_09 AC 45 ms
36,872 KB
testcase_10 AC 47 ms
36,904 KB
testcase_11 AC 45 ms
36,864 KB
testcase_12 AC 45 ms
36,752 KB
testcase_13 AC 45 ms
37,000 KB
testcase_14 AC 45 ms
36,668 KB
testcase_15 AC 46 ms
36,872 KB
testcase_16 AC 45 ms
36,856 KB
testcase_17 AC 48 ms
36,872 KB
testcase_18 AC 49 ms
36,544 KB
testcase_19 AC 48 ms
37,096 KB
testcase_20 AC 49 ms
36,544 KB
testcase_21 AC 45 ms
36,724 KB
testcase_22 AC 45 ms
36,912 KB
testcase_23 AC 45 ms
36,552 KB
testcase_24 AC 48 ms
36,928 KB
testcase_25 AC 48 ms
36,880 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();
        int w = sc.nextInt();
        int start = (sc.nextInt() - 1) * w + sc.nextInt() - 1;
        int goal = (sc.nextInt() - 1) * w + sc.nextInt() - 1;
        UnionFindTree uft = new UnionFindTree(h * w);
        char[][] field = new char[h][];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                if (i > 0) {
                    if (Math.abs(field[i - 1][j] - field[i][j]) <= 1) {
                        uft.unite(i * w + j, (i - 1) * w + j);
                    }
                }
                if (i > 1) {
                    if (field[i - 2][j] == field[i][j] && field[i - 1][j] < field[i][j]) {
                        uft.unite(i * w + j, (i - 2) * w + j);
                    }
                }
                if (j > 0) {
                    if (Math.abs(field[i][j - 1] - field[i][j]) <= 1) {
                        uft.unite(i * w + j, i * w + j - 1);
                    }
                }
                if (j > 1) {
                    if (field[i][j - 2] == field[i][j] && field[i][j - 1] < field[i][j]) {
                        uft.unite(i * w + j, i * w + j - 2);
                    }
                }
            }
        }
        if (uft.same(start, goal)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
    }
}

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