結果
| 問題 |
No.424 立体迷路
|
| コンテスト | |
| ユーザー |
takeya_okino
|
| 提出日時 | 2019-07-20 17:03:23 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,095 bytes |
| コンパイル時間 | 2,634 ms |
| コンパイル使用メモリ | 77,940 KB |
| 実行使用メモリ | 54,332 KB |
| 最終ジャッジ日時 | 2024-12-29 18:40:02 |
| 合計ジャッジ時間 | 8,160 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 WA * 1 |
| other | AC * 20 WA * 1 |
ソースコード
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) {
x = find(x);
y = find(y);
if(rank[x] > rank[y]) {
par[y] = x;
rank[x] += rank[y];
} else {
par[x] = y;
if(rank[x] == rank[y]) {
rank[y]++;
} else {
par[x] = y;
rank[y] += rank[x];
}
}
}
}
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])) {
x = find(x);
y = find(y);
if(rank[x] > rank[y]) {
par[y] = x;
rank[x] += rank[y];
} else {
par[x] = y;
if(rank[x] == rank[y]) {
rank[y]++;
} else {
par[x] = y;
rank[y] += rank[x];
}
}
}
}
if((x - y) == (-1) * 2) {
if((height[x] == height[y]) && (height[x] > height[x + 1])) {
x = find(x);
y = find(y);
if(rank[x] > rank[y]) {
par[y] = x;
rank[x] += rank[y];
} else {
par[x] = y;
if(rank[x] == rank[y]) {
rank[y]++;
} else {
par[x] = y;
rank[y] += rank[x];
}
}
}
}
if((x - y) == 2 * w) {
if((height[x] == height[y]) && (height[x] > height[x - w])) {
x = find(x);
y = find(y);
if(rank[x] > rank[y]) {
par[y] = x;
rank[x] += rank[y];
} else {
par[x] = y;
if(rank[x] == rank[y]) {
rank[y]++;
} else {
par[x] = y;
rank[y] += rank[x];
}
}
}
}
if((x - y) == (-1) * 2 * w) {
if((height[x] == height[y]) && (height[x] > height[x + w])) {
x = find(x);
y = find(y);
if(rank[x] > rank[y]) {
par[y] = x;
rank[x] += rank[y];
} else {
par[x] = y;
if(rank[x] == rank[y]) {
rank[y]++;
} else {
par[x] = y;
rank[y] += rank[x];
}
}
}
}
}
}
}
static boolean same(int x, int y) {
return find(x) == find(y);
}
}
takeya_okino