結果
問題 | No.2731 Two Colors |
ユーザー | ks2m |
提出日時 | 2024-04-19 21:52:51 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,154 ms / 3,000 ms |
コード長 | 2,171 bytes |
コンパイル時間 | 7,132 ms |
コンパイル使用メモリ | 77,664 KB |
実行使用メモリ | 65,064 KB |
最終ジャッジ日時 | 2024-10-11 14:52:25 |
合計ジャッジ時間 | 22,969 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,154 ms
65,064 KB |
testcase_01 | AC | 1,061 ms
64,516 KB |
testcase_02 | AC | 1,113 ms
64,540 KB |
testcase_03 | AC | 99 ms
39,168 KB |
testcase_04 | AC | 195 ms
45,172 KB |
testcase_05 | AC | 174 ms
43,812 KB |
testcase_06 | AC | 308 ms
45,852 KB |
testcase_07 | AC | 314 ms
46,480 KB |
testcase_08 | AC | 147 ms
41,284 KB |
testcase_09 | AC | 564 ms
53,844 KB |
testcase_10 | AC | 113 ms
39,604 KB |
testcase_11 | AC | 664 ms
59,452 KB |
testcase_12 | AC | 557 ms
53,656 KB |
testcase_13 | AC | 625 ms
57,844 KB |
testcase_14 | AC | 412 ms
49,004 KB |
testcase_15 | AC | 141 ms
40,696 KB |
testcase_16 | AC | 387 ms
49,916 KB |
testcase_17 | AC | 447 ms
50,092 KB |
testcase_18 | AC | 214 ms
44,724 KB |
testcase_19 | AC | 434 ms
49,868 KB |
testcase_20 | AC | 444 ms
51,360 KB |
testcase_21 | AC | 222 ms
45,280 KB |
testcase_22 | AC | 581 ms
56,304 KB |
testcase_23 | AC | 317 ms
45,800 KB |
testcase_24 | AC | 225 ms
44,760 KB |
testcase_25 | AC | 94 ms
38,164 KB |
testcase_26 | AC | 521 ms
52,704 KB |
testcase_27 | AC | 583 ms
57,308 KB |
testcase_28 | AC | 478 ms
53,972 KB |
testcase_29 | AC | 276 ms
45,172 KB |
testcase_30 | AC | 379 ms
49,416 KB |
testcase_31 | AC | 287 ms
46,800 KB |
testcase_32 | AC | 647 ms
60,352 KB |
testcase_33 | AC | 54 ms
37,100 KB |
testcase_34 | AC | 53 ms
36,984 KB |
testcase_35 | AC | 54 ms
37,248 KB |
ソースコード
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.PriorityQueue; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int h = Integer.parseInt(sa[0]); int w = Integer.parseInt(sa[1]); int[][] a = new int[h][w]; for (int i = 0; i < h; i++) { sa = br.readLine().split(" "); for (int j = 0; j < w; j++) { a[i][j] = Integer.parseInt(sa[j]); } } br.close(); int hw = h * w; boolean[] b1 = new boolean[hw]; boolean[] b2 = new boolean[hw]; int[] c = new int[hw]; b1[0] = true; b2[hw - 1] = true; c[0] = 1; c[hw - 1] = 2; PriorityQueue<Node> que1 = new PriorityQueue<Node>(); PriorityQueue<Node> que2 = new PriorityQueue<Node>(); que1.add(new Node(0, 1, 0)); que2.add(new Node(hw - 1, 2, 0)); int ans = -2; int[] dx = {1, 0, -1, 0}; int[] dy = {0, 1, 0, -1}; label: while (!que1.isEmpty() && !que2.isEmpty()) { Node cur = que1.poll(); int cx = cur.v / w; int cy = cur.v % w; c[cur.v] = cur.a; ans++; for (int i = 0; i < 4; i++) { int nx = cx + dx[i]; int ny = cy + dy[i]; if (nx < 0 || h <= nx || ny < 0 || w <= ny) { continue; } int next = nx * w + ny; if (!b1[next]) { que1.add(new Node(next, cur.a, a[nx][ny])); b1[next] = true; } if (c[next] == 2) { break label; } } cur = que2.poll(); cx = cur.v / w; cy = cur.v % w; c[cur.v] = cur.a; ans++; for (int i = 0; i < 4; i++) { int nx = cx + dx[i]; int ny = cy + dy[i]; if (nx < 0 || h <= nx || ny < 0 || w <= ny) { continue; } int next = nx * w + ny; if (!b2[next]) { que2.add(new Node(next, cur.a, a[nx][ny])); b2[next] = true; } if (c[next] == 1) { break label; } } } System.out.println(ans); } static class Node implements Comparable<Node> { int v, a; long d; public Node(int v, int a, long d) { this.v = v; this.a = a; this.d = d; } public int compareTo(Node o) { return Long.compare(d, o.d); } } }