結果

問題 No.124 門松列(3)
ユーザー uafr_csuafr_cs
提出日時 2015-09-16 18:19:58
言語 Java21
(openjdk 21)
結果
AC  
実行時間 251 ms / 5,000 ms
コード長 2,151 bytes
コンパイル時間 4,927 ms
コンパイル使用メモリ 76,184 KB
実行使用メモリ 60,956 KB
最終ジャッジ日時 2023-09-26 12:10:33
合計ジャッジ時間 9,359 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 249 ms
60,280 KB
testcase_01 AC 126 ms
55,764 KB
testcase_02 AC 127 ms
55,640 KB
testcase_03 AC 251 ms
60,928 KB
testcase_04 AC 246 ms
60,588 KB
testcase_05 AC 242 ms
60,956 KB
testcase_06 AC 127 ms
55,896 KB
testcase_07 AC 130 ms
55,648 KB
testcase_08 AC 128 ms
55,712 KB
testcase_09 AC 127 ms
55,648 KB
testcase_10 AC 128 ms
55,656 KB
testcase_11 AC 128 ms
55,640 KB
testcase_12 AC 127 ms
55,748 KB
testcase_13 AC 127 ms
55,948 KB
testcase_14 AC 137 ms
53,768 KB
testcase_15 AC 136 ms
55,656 KB
testcase_16 AC 199 ms
54,556 KB
testcase_17 AC 143 ms
55,712 KB
testcase_18 AC 136 ms
55,684 KB
testcase_19 AC 143 ms
55,740 KB
testcase_20 AC 165 ms
56,084 KB
testcase_21 AC 136 ms
55,700 KB
testcase_22 AC 153 ms
55,716 KB
testcase_23 AC 209 ms
59,408 KB
testcase_24 AC 220 ms
59,108 KB
testcase_25 AC 213 ms
59,752 KB
testcase_26 AC 195 ms
58,316 KB
testcase_27 AC 188 ms
56,344 KB
testcase_28 AC 237 ms
60,228 KB
testcase_29 AC 245 ms
60,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static final int[][] move_dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
	
	public static boolean is_valid(int x, int y, int W, int H){
		return x >= 0 && x < W && y >= 0 && y < H;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int W = sc.nextInt();
		final int H = sc.nextInt();
		int[][] heights = new int[H][W];
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				heights[i][j] = sc.nextInt();
			}
		}
		
		LinkedList<Integer> x_queue = new LinkedList<Integer>();
		LinkedList<Integer> y_queue = new LinkedList<Integer>();
		LinkedList<Integer> prev_queue = new LinkedList<Integer>();
		
		
		int[][][] minimum_cost = new int[10][H][W];
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < H; j++){
				for(int k = 0; k < W; k++){
					minimum_cost[i][j][k] = Integer.MAX_VALUE;
				}
			}
		}
		minimum_cost[0][0][0] = 0;
		x_queue.add(0);
		y_queue.add(0);
		prev_queue.add(0);
		
		while(!x_queue.isEmpty()){
			final int x = x_queue.poll();
			final int y = y_queue.poll();
			
			final int curr = heights[y][x];
			final int prev = prev_queue.poll();
			
			if(curr == prev){
				continue;
			}else if(x == W - 1 && y == H - 1 && prev > 0){
				System.out.println(minimum_cost[prev][y][x]);
				return;
			}
			
			//System.out.println(x + " " + y + " " + prev + " " + minimum_cost[prev][y][x]);
			
			for(final int[] move : move_dir){
				final int nx = x + move[0];
				final int ny = y + move[1];
				
				if(!is_valid(nx, ny, W, H)){
					continue;
				}
				
				final int next = heights[ny][nx];
				if(prev == next){
					continue;
				}
				
				final int next_cost = minimum_cost[prev][y][x] + 1;
				if(prev == 0 || prev < curr && curr > next || prev > curr && curr < next){
					if(minimum_cost[curr][ny][nx] > next_cost){
						minimum_cost[curr][ny][nx] = next_cost;
						x_queue.add(nx);
						y_queue.add(ny);
						prev_queue.add(curr);
					}
				}
				
			}
			
		}
		
		System.out.println(-1);
		
		
	}

}
0