結果

問題 No.2731 Two Colors
ユーザー ks2mks2m
提出日時 2024-04-19 21:52:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,076 ms / 3,000 ms
コード長 2,171 bytes
コンパイル時間 4,771 ms
コンパイル使用メモリ 77,460 KB
実行使用メモリ 65,372 KB
最終ジャッジ日時 2024-04-19 21:53:22
合計ジャッジ時間 17,829 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,075 ms
65,372 KB
testcase_01 AC 911 ms
64,648 KB
testcase_02 AC 1,076 ms
65,332 KB
testcase_03 AC 89 ms
39,396 KB
testcase_04 AC 173 ms
44,884 KB
testcase_05 AC 158 ms
43,804 KB
testcase_06 AC 254 ms
45,780 KB
testcase_07 AC 317 ms
46,596 KB
testcase_08 AC 153 ms
41,328 KB
testcase_09 AC 526 ms
53,636 KB
testcase_10 AC 105 ms
39,596 KB
testcase_11 AC 589 ms
59,532 KB
testcase_12 AC 524 ms
54,184 KB
testcase_13 AC 580 ms
56,776 KB
testcase_14 AC 369 ms
49,268 KB
testcase_15 AC 130 ms
40,632 KB
testcase_16 AC 372 ms
50,248 KB
testcase_17 AC 418 ms
50,320 KB
testcase_18 AC 207 ms
43,836 KB
testcase_19 AC 378 ms
50,456 KB
testcase_20 AC 400 ms
51,464 KB
testcase_21 AC 195 ms
44,544 KB
testcase_22 AC 512 ms
56,780 KB
testcase_23 AC 272 ms
45,852 KB
testcase_24 AC 221 ms
45,172 KB
testcase_25 AC 88 ms
38,404 KB
testcase_26 AC 470 ms
52,772 KB
testcase_27 AC 567 ms
55,196 KB
testcase_28 AC 509 ms
53,040 KB
testcase_29 AC 300 ms
45,260 KB
testcase_30 AC 360 ms
49,016 KB
testcase_31 AC 283 ms
46,536 KB
testcase_32 AC 562 ms
59,788 KB
testcase_33 AC 47 ms
36,852 KB
testcase_34 AC 47 ms
37,020 KB
testcase_35 AC 45 ms
37,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
		}
	}
}
0