結果

問題 No.367 ナイトの転身
ユーザー sekiya9311sekiya9311
提出日時 2016-08-26 16:17:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 2,231 bytes
コンパイル時間 3,459 ms
コンパイル使用メモリ 79,160 KB
実行使用メモリ 59,436 KB
最終ジャッジ日時 2024-04-25 17:01:56
合計ジャッジ時間 9,287 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
53,016 KB
testcase_01 AC 124 ms
54,080 KB
testcase_02 AC 123 ms
54,076 KB
testcase_03 AC 124 ms
54,172 KB
testcase_04 AC 126 ms
54,132 KB
testcase_05 AC 125 ms
54,296 KB
testcase_06 AC 160 ms
54,648 KB
testcase_07 AC 125 ms
54,160 KB
testcase_08 AC 124 ms
54,636 KB
testcase_09 AC 125 ms
54,260 KB
testcase_10 AC 372 ms
58,668 KB
testcase_11 AC 416 ms
59,436 KB
testcase_12 AC 214 ms
56,856 KB
testcase_13 AC 227 ms
57,104 KB
testcase_14 AC 265 ms
58,156 KB
testcase_15 AC 141 ms
54,456 KB
testcase_16 AC 255 ms
58,300 KB
testcase_17 AC 186 ms
54,548 KB
testcase_18 AC 181 ms
55,308 KB
testcase_19 AC 183 ms
54,876 KB
testcase_20 AC 144 ms
54,392 KB
testcase_21 AC 184 ms
54,824 KB
testcase_22 AC 116 ms
53,080 KB
testcase_23 AC 127 ms
54,120 KB
testcase_24 AC 128 ms
54,168 KB
testcase_25 AC 117 ms
52,900 KB
testcase_26 AC 125 ms
54,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

public class Yuki367 {
	static Scanner sc = new Scanner(System.in);
	static int[] nX = { -2, -2, -1, -1, 1, 1, 2, 2 };
	static int[] nY = { -1, 1, -2, 2, -2, 2, -1, 1 };
	static int[] bX = { -1, -1, 1, 1 };
	static int[] bY = { -1, 1, -1, 1 };

	public static void main(String[] args) {
		int H, W;
		H = sc.nextInt();
		W = sc.nextInt();
		sc.nextLine();
		boolean[][][] is = new boolean[2][H][W];
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < H; j++) {
				Arrays.fill(is[i][j], false);
			}
		}
		int[] start = new int[2];
		String[] S = new String[H];
		for (int i = 0; i < H; i++) {
			S[i] = sc.nextLine();
			for (int j = 0; j < W; j++) {
				if (S[i].charAt(j) == 'S') {
					start[0] = j;
					start[1] = i;
				}
			}
		}
		ArrayDeque<Now> q = new ArrayDeque<>();
		q.add(new Now(start[0], start[1], 0, true));
		while (!q.isEmpty()) {
			Now n = q.poll();
			boolean f = n.knightFlag;
			int nowStep = n.step + 1;
			if (f) {
				for (int i = 0; i < nX.length; i++) {
					int x = n.x + nX[i];
					int y = n.y + nY[i];
					if (x < 0 || x >= W)
						continue;
					if (y < 0 || y >= H)
						continue;
					if (is[1][y][x])
						continue;
					is[1][y][x] = true;
					if (S[y].charAt(x) == 'G') {
						System.out.println(nowStep);
						return;
					}
					if (S[y].charAt(x) == 'R') {
						q.add(new Now(x, y, nowStep, !f));
					} else {
						q.add(new Now(x, y, nowStep, f));
					}
				}
			} else {
				for (int i = 0; i < bX.length; i++) {
					int x = n.x + bX[i];
					int y = n.y + bY[i];
					if (x < 0 || x >= W)
						continue;
					if (y < 0 || y >= H)
						continue;
					if (is[0][y][x])
						continue;
					is[0][y][x] = true;
					if (S[y].charAt(x) == 'G') {
						System.out.println(nowStep);
						return;
					}
					if (S[y].charAt(x) == 'R') {
						q.add(new Now(x, y, nowStep, !f));
					} else {
						q.add(new Now(x, y, nowStep, f));
					}
				}
			}
		}
		System.out.println(-1);
	}

	public static class Now {
		int x;
		int y;
		int step;
		boolean knightFlag;

		Now(int x, int y, int step, boolean f) {
			this.x = x;
			this.y = y;
			this.step = step;
			this.knightFlag = f;
		}
	}
}
0