結果

問題 No.124 門松列(3)
ユーザー ぴろずぴろず
提出日時 2015-01-11 23:43:38
言語 Java19
(openjdk 21)
結果
AC  
実行時間 342 ms / 5,000 ms
コード長 2,398 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 75,880 KB
実行使用メモリ 69,920 KB
最終ジャッジ日時 2023-09-03 23:43:38
合計ジャッジ時間 9,131 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 282 ms
62,400 KB
testcase_01 AC 122 ms
56,288 KB
testcase_02 AC 122 ms
55,936 KB
testcase_03 AC 340 ms
69,520 KB
testcase_04 AC 342 ms
69,920 KB
testcase_05 AC 331 ms
69,636 KB
testcase_06 AC 121 ms
53,668 KB
testcase_07 AC 119 ms
55,480 KB
testcase_08 AC 120 ms
56,272 KB
testcase_09 AC 120 ms
55,444 KB
testcase_10 AC 121 ms
56,068 KB
testcase_11 AC 119 ms
56,028 KB
testcase_12 AC 120 ms
55,832 KB
testcase_13 AC 120 ms
55,400 KB
testcase_14 AC 130 ms
56,372 KB
testcase_15 AC 131 ms
56,052 KB
testcase_16 AC 191 ms
58,304 KB
testcase_17 AC 139 ms
55,948 KB
testcase_18 AC 131 ms
56,140 KB
testcase_19 AC 140 ms
55,756 KB
testcase_20 AC 164 ms
56,400 KB
testcase_21 AC 132 ms
56,116 KB
testcase_22 AC 153 ms
55,644 KB
testcase_23 AC 241 ms
62,232 KB
testcase_24 AC 264 ms
66,652 KB
testcase_25 AC 263 ms
66,352 KB
testcase_26 AC 214 ms
59,328 KB
testcase_27 AC 196 ms
58,628 KB
testcase_28 AC 329 ms
69,196 KB
testcase_29 AC 329 ms
69,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no124;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int[] di = {1,0,-1,0};
	static int[] dj = {0,1,0,-1};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int w = sc.nextInt();
		int h = sc.nextInt();
		int[][] map = new int[h][w];
		for(int i=0;i<h;i++) {
			for(int j=0;j<w;j++) {
				map[i][j] = sc.nextInt();
			}
		}
		Graph g = new Graph(w*h*10);
		for(int i=0;i<h;i++) {
			for(int j=0;j<w;j++) {
				for(int bef=0;bef<=9;bef++) {
					for(int dir=0;dir<4;dir++) {
						int ni = i + di[dir];
						int nj = j + dj[dir];
						if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
							continue;
						}
						if (isKasomatsuSequence(bef, map[i][j], map[ni][nj])) {
							g.addEdge(i + j * h + bef * h * w, ni + nj * h + map[i][j] * h * w, 1);
						}
					}
				}
			}
		}
		int[] dist = g.minDistQueue(0);
		int ans = Graph.INF;
		for(int i=0;i<=9;i++) {
			ans = Math.min(ans, dist[h-1 + (w-1) * h + i * h * w]);
		}
		if (ans == Graph.INF) {
			System.out.println(-1);
		}else{
			System.out.println(ans);
		}
	}

	public static boolean isKasomatsuSequence(int a,int b,int c) {
		if (a == 0) {
			return true;
		}
		if (a == b || a == c || b == c) {
			return false;
		}
		return b < a && b < c || b > a && b > c;
	}

}
class Graph {
	public static final int INF = 1<<29;
	int n;
	ArrayList<Edge>[] graph;

	@SuppressWarnings("unchecked")
	public Graph(int n) {
		this.n = n;
		this.graph = new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i] = new ArrayList<Edge>();
		}
	}

	public void addBidirectionalEdge(int from,int to,int cost) {
		addEdge(from,to,cost);
		addEdge(to,from,cost);
	}
	public void addEdge(int from,int to,int cost) {
		graph[from].add(new Edge(to, cost));
	}

	//O(E) all cost is 0 or 1
	public int[] minDistQueue(int s) {
		int[] d = new int[n];
		Arrays.fill(d, INF);
		ArrayDeque<Integer> q = new ArrayDeque<Integer>();
		q.add(s);
		d[s] = 0;
		while(!q.isEmpty()) {
			int v = q.pollFirst();
			for(Edge e:graph[v]) {
				int u = e.to;
				if (d[v] + e.cost < d[u]) {
					d[u] = d[v] + e.cost;
					if (e.cost == 0) {
						q.addFirst(u);
					}else{
						q.addLast(u);
					}
				}
			}
		}
		return d;
	}

	class Edge {
		int to;
		int cost;
		public Edge(int to,int cost) {
			this.to = to;
			this.cost = cost;
		}
	}
}
0