結果
問題 | No.424 立体迷路 |
ユーザー | takeya_okino |
提出日時 | 2019-07-20 18:50:23 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,228 bytes |
コンパイル時間 | 2,023 ms |
コンパイル使用メモリ | 78,148 KB |
実行使用メモリ | 55,912 KB |
最終ジャッジ日時 | 2024-06-09 13:08:57 |
合計ジャッジ時間 | 6,013 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 100 ms
52,716 KB |
testcase_01 | AC | 114 ms
54,244 KB |
testcase_02 | AC | 104 ms
52,912 KB |
testcase_03 | WA | - |
testcase_04 | AC | 110 ms
54,000 KB |
testcase_05 | AC | 104 ms
52,840 KB |
testcase_06 | AC | 100 ms
52,484 KB |
testcase_07 | AC | 104 ms
52,892 KB |
testcase_08 | AC | 112 ms
54,096 KB |
testcase_09 | AC | 113 ms
53,776 KB |
testcase_10 | AC | 113 ms
53,928 KB |
testcase_11 | AC | 98 ms
52,636 KB |
testcase_12 | AC | 104 ms
53,008 KB |
testcase_13 | AC | 127 ms
54,172 KB |
testcase_14 | AC | 117 ms
53,856 KB |
testcase_15 | AC | 119 ms
54,068 KB |
testcase_16 | AC | 119 ms
54,196 KB |
testcase_17 | AC | 131 ms
54,640 KB |
testcase_18 | AC | 139 ms
54,432 KB |
testcase_19 | AC | 137 ms
55,100 KB |
testcase_20 | AC | 155 ms
54,948 KB |
testcase_21 | AC | 114 ms
54,056 KB |
testcase_22 | AC | 114 ms
53,664 KB |
testcase_23 | WA | - |
testcase_24 | AC | 125 ms
55,912 KB |
testcase_25 | AC | 130 ms
53,852 KB |
ソースコード
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)) { if((Math.abs(x - y) == 1) || (Math.abs(x - y) == w)) { if(Math.abs(height[x] - height[y]) <= 1) { int x1 = find(x); int y1 = find(y); if(rank[x1] > rank[y1]) { par[y1] = x1; rank[x1] += rank[y1]; } else { par[x1] = y1; if(rank[x1] == rank[y1]) { rank[y1]++; } else { par[x1] = y1; rank[y1] += rank[x1]; } } } } if((Math.abs(x - y) == 2) || (Math.abs(x - y) == (2 * w))) { if((x - y) == 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; rank[x1] += rank[y1]; } else { par[x1] = y1; if(rank[x1] == rank[y1]) { rank[y1]++; } else { par[x1] = y1; rank[y1] += rank[x1]; } } } } if((x - y) == (-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; rank[x1] += rank[y1]; } else { par[x1] = y1; if(rank[x1] == rank[y1]) { rank[y1]++; } else { par[x1] = y1; rank[y1] += rank[x1]; } } } } if((x - y) == 2 * w) { 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; rank[x1] += rank[y1]; } else { par[x1] = y1; if(rank[x1] == rank[y1]) { rank[y1]++; } else { par[x1] = y1; rank[y1] += rank[x1]; } } } } if((x - y) == (-1) * 2 * w) { 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; rank[x1] += rank[y1]; } else { par[x1] = y1; if(rank[x1] == rank[y1]) { rank[y1]++; } else { par[x1] = y1; rank[y1] += rank[x1]; } } } } } } } static boolean same(int x, int y) { return find(x) == find(y); } }