結果

問題 No.2731 Two Colors
ユーザー ks2mks2m
提出日時 2024-04-19 21:52:51
言語 Java
(openjdk 23)
結果
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
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

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