結果

問題 No.367 ナイトの転身
ユーザー htensaihtensai
提出日時 2020-05-12 10:49:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,051 ms / 2,000 ms
コード長 2,914 bytes
コンパイル時間 2,686 ms
コンパイル使用メモリ 75,948 KB
実行使用メモリ 72,388 KB
最終ジャッジ日時 2023-10-11 04:44:47
合計ジャッジ時間 12,942 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
55,484 KB
testcase_01 AC 131 ms
55,736 KB
testcase_02 AC 129 ms
55,716 KB
testcase_03 AC 132 ms
55,624 KB
testcase_04 AC 131 ms
56,028 KB
testcase_05 AC 132 ms
55,780 KB
testcase_06 AC 175 ms
56,288 KB
testcase_07 AC 133 ms
55,880 KB
testcase_08 AC 130 ms
55,512 KB
testcase_09 AC 128 ms
55,608 KB
testcase_10 AC 831 ms
68,384 KB
testcase_11 AC 1,051 ms
72,388 KB
testcase_12 AC 785 ms
69,840 KB
testcase_13 AC 742 ms
69,768 KB
testcase_14 AC 748 ms
67,432 KB
testcase_15 AC 190 ms
57,572 KB
testcase_16 AC 671 ms
69,780 KB
testcase_17 AC 257 ms
59,964 KB
testcase_18 AC 258 ms
60,904 KB
testcase_19 AC 320 ms
64,032 KB
testcase_20 AC 254 ms
60,704 KB
testcase_21 AC 314 ms
60,792 KB
testcase_22 AC 138 ms
55,740 KB
testcase_23 AC 178 ms
56,124 KB
testcase_24 AC 185 ms
55,900 KB
testcase_25 AC 165 ms
55,596 KB
testcase_26 AC 144 ms
55,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		char[][] field = new char[h][];
		int sr = 0;
		int sx = 0;
		int gr = 0;
		int gc = 0;
		int[][] kCosts = new int[h][w];
		int[][] bCosts = new int[h][w];
		for (int i = 0; i < h; i++) {
		    field[i] = sc.next().toCharArray();
    		Arrays.fill(kCosts[i], Integer.MAX_VALUE);
    		Arrays.fill(bCosts[i], Integer.MAX_VALUE);
		    for (int j = 0; j < w; j++) {
		        if (field[i][j] == 'S') {
		            sr = i;
		            sx = j;
		        } else if (field[i][j] == 'G') {
		            gr = i;
		            gc = j;
		        }
		    }
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(sr, sx, 0, true));
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (p.row < 0 || p.row >= h || p.col < 0 || p.col >= w) {
		        continue;
		    }
		    if (p.isKnight) {
		        if (kCosts[p.row][p.col] <= p.value) {
		            continue;
		        }
		        kCosts[p.row][p.col] = p.value;
		    } else {
		        if (bCosts[p.row][p.col] <= p.value) {
		            continue;
		        }
		        bCosts[p.row][p.col] = p.value;
		    }
		    if (field[p.row][p.col] == 'R') {
		        p.isKnight ^= true;
		    }
		    if (p.isKnight) {
		        queue.add(new Path(p.row - 2, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 2, p.col + 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 2, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 2, p.col + 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 1, p.col - 2, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 1, p.col + 2, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col - 2, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col + 2, p.value + 1, p.isKnight));
		    } else {
		        queue.add(new Path(p.row - 1, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row - 1, p.col + 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col - 1, p.value + 1, p.isKnight));
		        queue.add(new Path(p.row + 1, p.col + 1, p.value + 1, p.isKnight));
		    }
		}
		int ans = Math.min(kCosts[gr][gc], bCosts[gr][gc]);
		if (ans == Integer.MAX_VALUE) {
		    System.out.println(-1);
		} else {
		    System.out.println(ans);
		}
	}
	
	static class Path implements Comparable<Path> {
	    int row;
	    int col;
	    int value;
	    boolean isKnight;
	    
	    public Path(int row, int col, int value, boolean isKnight) {
	        this.row = row;
	        this.col = col;
	        this.value = value;
	        this.isKnight = isKnight;
	    }
	    
	    public int compareTo(Path another) {
	        return value - another.value;
	    }
	}
}
0