結果

問題 No.367 ナイトの転身
ユーザー uafr_csuafr_cs
提出日時 2017-11-01 18:44:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 769 ms / 2,000 ms
コード長 2,705 bytes
コンパイル時間 2,963 ms
コンパイル使用メモリ 79,408 KB
実行使用メモリ 59,400 KB
最終ジャッジ日時 2024-05-02 04:14:12
合計ジャッジ時間 10,154 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
40,376 KB
testcase_01 AC 106 ms
40,392 KB
testcase_02 AC 98 ms
39,964 KB
testcase_03 AC 112 ms
41,380 KB
testcase_04 AC 110 ms
41,388 KB
testcase_05 AC 108 ms
40,772 KB
testcase_06 AC 139 ms
41,308 KB
testcase_07 AC 97 ms
40,208 KB
testcase_08 AC 96 ms
39,656 KB
testcase_09 AC 110 ms
41,024 KB
testcase_10 AC 422 ms
55,356 KB
testcase_11 AC 769 ms
59,400 KB
testcase_12 AC 458 ms
52,976 KB
testcase_13 AC 487 ms
53,120 KB
testcase_14 AC 481 ms
53,700 KB
testcase_15 AC 143 ms
42,728 KB
testcase_16 AC 540 ms
53,356 KB
testcase_17 AC 179 ms
43,820 KB
testcase_18 AC 189 ms
45,140 KB
testcase_19 AC 223 ms
48,060 KB
testcase_20 AC 199 ms
44,424 KB
testcase_21 AC 225 ms
49,436 KB
testcase_22 AC 117 ms
41,212 KB
testcase_23 AC 119 ms
40,616 KB
testcase_24 AC 130 ms
41,904 KB
testcase_25 AC 129 ms
41,288 KB
testcase_26 AC 115 ms
41,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	
	public static long MOD = 1000000007;
	
	public static final int INF = Integer.MAX_VALUE / 2 - 1;
	public static boolean in_range(int x, int y, int W, int H){
		return 0 <= x && x < W && 0 <= y && y < H;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int H = sc.nextInt();
		final int W = sc.nextInt();
		
		int sx = -1, sy = -1, gx = -1, gy = -1;
		
		boolean[][] is_red = new boolean[H][W];
		for(int i = 0; i < H; i++){
			final char[] ins = sc.next().toCharArray();
			
			for(int j = 0; j < W; j++){
				if(ins[j] == 'R'){
					is_red[i][j] = true;
				}else if(ins[j] == 'S'){
					sx = j; sy = i;
				}else if(ins[j] == 'G'){
					gx = j; gy = i;
				}
			}
		}
		
		LinkedList<Integer> queue = new LinkedList<Integer>();
		final int KNIGHT = 0, MINB = 1;
		int[][][] costs = new int[H][W][2];
		for(int i = 0; i < H; i++){
			for(int j = 0; j < W; j++){
				costs[i][j][0] = INF;
				costs[i][j][1] = INF;
			}
		}	
		costs[sy][sx][KNIGHT] = 0;
		queue.add(sx); queue.add(sy); queue.add(KNIGHT);
		
		while(!queue.isEmpty()){
			final int x = queue.poll();
			final int y = queue.poll();
			final int type = queue.poll();
			//System.out.println(x + " " + y + " " + (type == KNIGHT ? "N" : "B"));
			
			if(type == MINB){
				for(int dy = -1; dy <= 1; dy++){
					for(int dx = -1; dx <= 1; dx++){
						if(dx == 0 && dy == 0){ continue; }
						if(Math.abs(dx) == 0 || Math.abs(dy) == 0){ continue; }
						
						final int nx = x + dx;
						final int ny = y + dy;
						if(!in_range(nx, ny, W, H)){ continue; }
						
						final int next_type = is_red[ny][nx] ? 1 - type : type;
						
						if(costs[ny][nx][next_type] <= costs[y][x][type] + 1){ continue; }
						
						costs[ny][nx][next_type] = costs[y][x][type] + 1;
						queue.add(nx); queue.add(ny); queue.add(next_type);
					}
				}
			}else{
				for(int dy = -2; dy <= 2; dy++){
					for(int dx = -2; dx <= 2; dx++){
						if(Math.abs(dx) == Math.abs(dy)){ continue; }
						if(Math.min(Math.abs(dx), Math.abs(dy)) == 0){ continue; }
						
						final int nx = x + dx;
						final int ny = y + dy;
						if(!in_range(nx, ny, W, H)){ continue; }
						
						final int next_type = is_red[ny][nx] ? 1 - type : type;
						
						if(costs[ny][nx][next_type] <= costs[y][x][type] + 1){ continue; }
						
						costs[ny][nx][next_type] = costs[y][x][type] + 1;
						queue.add(nx); queue.add(ny); queue.add(next_type);
					}
				}
			}
		}
		
		final int min_cost = Math.min(costs[gy][gx][KNIGHT], costs[gy][gx][MINB]);
		
		System.out.println(min_cost >= INF ? -1 : min_cost);
	}
}
0