結果

問題 No.424 立体迷路
ユーザー takeya_okinotakeya_okino
提出日時 2019-07-21 11:53:18
言語 Java21
(openjdk 21)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 3,914 bytes
コンパイル時間 2,361 ms
コンパイル使用メモリ 74,944 KB
実行使用メモリ 57,908 KB
最終ジャッジ日時 2023-09-18 17:07:28
合計ジャッジ時間 7,235 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,764 KB
testcase_01 AC 128 ms
55,984 KB
testcase_02 AC 132 ms
55,968 KB
testcase_03 AC 128 ms
55,500 KB
testcase_04 AC 132 ms
56,044 KB
testcase_05 AC 127 ms
55,512 KB
testcase_06 AC 129 ms
55,512 KB
testcase_07 AC 134 ms
55,964 KB
testcase_08 AC 130 ms
55,880 KB
testcase_09 AC 132 ms
55,744 KB
testcase_10 AC 131 ms
55,772 KB
testcase_11 AC 129 ms
55,728 KB
testcase_12 AC 131 ms
55,820 KB
testcase_13 AC 136 ms
55,764 KB
testcase_14 AC 134 ms
55,764 KB
testcase_15 AC 138 ms
55,516 KB
testcase_16 AC 134 ms
55,940 KB
testcase_17 AC 166 ms
57,576 KB
testcase_18 AC 166 ms
57,908 KB
testcase_19 AC 165 ms
57,864 KB
testcase_20 AC 166 ms
57,592 KB
testcase_21 AC 132 ms
56,012 KB
testcase_22 AC 138 ms
55,904 KB
testcase_23 AC 131 ms
55,820 KB
testcase_24 AC 155 ms
56,528 KB
testcase_25 AC 159 ms
56,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
  static int[] par;
  static int[] rank;
  static int[] height;
  static int h;
  static int w;
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    h = sc.nextInt();
    w = sc.nextInt();
    par = new int[h * w];
    rank = new int[h * w];
    height = new int[h * w];

    int sx = sc.nextInt();
    int sy = sc.nextInt();
    int gx = sc.nextInt();
    int gy = sc.nextInt();
    init(h * w);
    for(int i = 0; i < h; i++) {
      String b = sc.next();
      for(int j = 0; j < w; j++) {
        height[(w * i) + j] = Integer.parseInt(String.valueOf(b.charAt(j)));
      }
    }
    for(int i = 0; i < h * w; i++) {
      if((i - w) >= 0) unite((i - w), i);
      if((i + w) < (h * w)) unite((i + w), i);
      if((i - 1) >= 0) unite((i - 1), i);
      if((i + 1) < (h * w)) unite((i + 1), i);

      if((i - (2 * w)) >= 0) unite((i - (2 * w)), i);
      if((i + (2 * w)) < (h * w)) unite((i + (2 * w)), i);
      if((i - 2) >= 0) unite((i - 2), i);
      if((i + 2) < (h * w)) unite((i + 2), i);
    }
    String ans = "YES";
    if(!same(((w * (sx - 1)) + sy - 1), ((w * (gx - 1)) + gy - 1))) ans = "NO";
    System.out.println(ans);
  }
  static void init(int m) {
      for(int i = 0; i < m; i++) {
        par[i] = i;
        rank[i] = 0;
      }
    }
  static int find(int x) {
      if(par[x] == x) {
        return x;
      } else {
        return par[x] = find(par[x]);
      }
    }
  static void unite(int x, int y) {
      if(find(x) != find(y)) {
      int i1 = x / w;
      int j1 = x - (i1 * w);
      int i2 = y / w;
      int j2 = y - (i2 * w);      
      if(((i1 == i2) && (Math.abs(j1 - j2) == 1)) || ((j1 == j2) && (Math.abs(i1 - i2) == 1))) {
        if(Math.abs(height[x] - height[y]) <= 1) {
            int x1 = find(x);
            int y1 = find(y);
          if(rank[x1] > rank[y1]) {
            par[y1] = x1;
          } else {
            par[x1] = y1;
            if(rank[x1] == rank[y1]) {
              rank[y1]++;
            } else {
            }
          }
        }
      }
      if(((i1 == i2) && (Math.abs(j1 - j2) == 2)) || ((j1 == j2) && (Math.abs(i1 - i2) == 2))) {
        if((j1 - j2) == 2) {
          if((height[x] == height[y]) && (height[x] > height[x - 1])) {
            int x1 = find(x);
            int y1 = find(y);
          if(rank[x1] > rank[y1]) {
            par[y1] = x1;
          } else {
            par[x1] = y1;
            if(rank[x1] == rank[y1]) {
              rank[y1]++;
            } else {
            }
          }
          }
        }
        if((j1 - j2) == (-1) * 2) {
          if((height[x] == height[y]) && (height[x] > height[x + 1])) {
            int x1 = find(x);
            int y1 = find(y);
          if(rank[x1] > rank[y1]) {
            par[y1] = x1;
          } else {
            par[x1] = y1;
            if(rank[x1] == rank[y1]) {
              rank[y1]++;
            } else {
            }
          }
          }
        }
        if((i1 - i2) == 2) {
          if((height[x] == height[y]) && (height[x] > height[x - w])) {
            int x1 = find(x);
            int y1 = find(y);
          if(rank[x1] > rank[y1]) {
            par[y1] = x1;
          } else {
            par[x1] = y1;
            if(rank[x1] == rank[y1]) {
              rank[y1]++;
            } else {
            }
          }
          }
        }
        if((i1 - i2) == (-1) * 2) {
          if((height[x] == height[y]) && (height[x] > height[x + w])) {
            int x1 = find(x);
            int y1 = find(y);
          if(rank[x1] > rank[y1]) {
            par[y1] = x1;
          } else {
            par[x1] = y1;
            if(rank[x1] == rank[y1]) {
              rank[y1]++;
            } else {
            }
          }
          }
        }
      }
      }
    }
  static boolean same(int x, int y) {
      return find(x) == find(y);
    }
}
0