結果

問題 No.367 ナイトの転身
ユーザー htensaihtensai
提出日時 2019-11-21 12:00:21
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,964 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 78,244 KB
実行使用メモリ 105,816 KB
最終ジャッジ日時 2024-04-17 22:25:46
合計ジャッジ時間 7,104 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
61,212 KB
testcase_01 AC 125 ms
54,056 KB
testcase_02 AC 127 ms
54,012 KB
testcase_03 AC 125 ms
54,020 KB
testcase_04 AC 130 ms
54,272 KB
testcase_05 AC 113 ms
53,032 KB
testcase_06 AC 150 ms
54,576 KB
testcase_07 AC 125 ms
54,348 KB
testcase_08 AC 124 ms
54,560 KB
testcase_09 AC 114 ms
52,868 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	static char[][] field;
	static int[][][] visited;
	static int h;
	static int w;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		h = sc.nextInt();
		w = sc.nextInt();
		field = new char[h][];
		visited = new int[2][h][w];
		int sh = 0;
		int sw = 0;
		int gh = 0;
		int gw = 0;
		for (int i = 0; i < h; i++) {
		    Arrays.fill(visited[0][i], Integer.MAX_VALUE);
		    Arrays.fill(visited[1][i], Integer.MAX_VALUE);
		    field[i] = sc.next().toCharArray();
		    for (int j = 0; j < w; j++) {
		        if (field[i][j] == 'S') {
		            sh = i;
		            sw = j;
		        } else if (field[i][j] == 'G') {
		            gh = i;
		            gw = j;
		        }
		    }
		}
		search(0, sh, sw, 0);
		if (visited[0][gh][gw] == Integer.MAX_VALUE && visited[1][gh][gw] == Integer.MAX_VALUE) {
		    System.out.println(-1);
		} else {
	        System.out.println(Math.min(visited[0][gh][gw], visited[1][gh][gw]));
		}
	}
	
	static void search(int type, int hh, int ww, int count) {
	    if (hh < 0 || ww < 0 || hh >= h || ww >= w) {
	        return;
	    }
	    if (field[hh][ww] == 'R') {
	        type = (type + 1) % 2;
	    }
	    if (visited[type][hh][ww] <= count) {
	        return;
	    }
	    visited[type][hh][ww] = count;
	    if (type == 0) {
	        search(type, hh - 2, ww - 1, count + 1);
	        search(type, hh - 1, ww - 2, count + 1);
	        search(type, hh + 2, ww - 1, count + 1);
	        search(type, hh + 1, ww - 2, count + 1);
	        search(type, hh - 2, ww + 1, count + 1);
	        search(type, hh - 1, ww + 2, count + 1);
	        search(type, hh + 2, ww + 1, count + 1);
	        search(type, hh + 1, ww + 2, count + 1);
	    } else {
	        search(type, hh - 1, ww - 1, count + 1);
	        search(type, hh + 1, ww - 1, count + 1);
	        search(type, hh - 1, ww + 1, count + 1);
	        search(type, hh + 1, ww + 1, count + 1);
	    }
	}
}
0