結果
問題 | No.367 ナイトの転身 |
ユーザー | uafr_cs |
提出日時 | 2017-11-01 18:44:25 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 714 ms / 2,000 ms |
コード長 | 2,705 bytes |
コンパイル時間 | 4,248 ms |
コンパイル使用メモリ | 77,480 KB |
実行使用メモリ | 70,484 KB |
最終ジャッジ日時 | 2023-08-14 16:07:37 |
合計ジャッジ時間 | 10,194 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 124 ms
56,536 KB |
testcase_01 | AC | 124 ms
56,088 KB |
testcase_02 | AC | 124 ms
55,776 KB |
testcase_03 | AC | 126 ms
55,968 KB |
testcase_04 | AC | 125 ms
55,820 KB |
testcase_05 | AC | 125 ms
55,908 KB |
testcase_06 | AC | 145 ms
56,248 KB |
testcase_07 | AC | 123 ms
55,772 KB |
testcase_08 | AC | 124 ms
55,712 KB |
testcase_09 | AC | 123 ms
55,816 KB |
testcase_10 | AC | 440 ms
67,968 KB |
testcase_11 | AC | 714 ms
70,484 KB |
testcase_12 | AC | 466 ms
63,804 KB |
testcase_13 | AC | 550 ms
66,100 KB |
testcase_14 | AC | 437 ms
63,348 KB |
testcase_15 | AC | 192 ms
56,756 KB |
testcase_16 | AC | 427 ms
62,748 KB |
testcase_17 | AC | 206 ms
59,044 KB |
testcase_18 | AC | 219 ms
59,128 KB |
testcase_19 | AC | 251 ms
60,824 KB |
testcase_20 | AC | 212 ms
59,460 KB |
testcase_21 | AC | 250 ms
62,028 KB |
testcase_22 | AC | 130 ms
55,908 KB |
testcase_23 | AC | 141 ms
56,032 KB |
testcase_24 | AC | 151 ms
56,608 KB |
testcase_25 | AC | 142 ms
56,080 KB |
testcase_26 | AC | 129 ms
55,744 KB |
ソースコード
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); } }