結果

問題 No.323 yuki国
ユーザー ぴろずぴろず
提出日時 2015-12-15 00:19:39
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,614 bytes
コンパイル時間 4,980 ms
コンパイル使用メモリ 76,312 KB
実行使用メモリ 78,656 KB
最終ジャッジ日時 2023-10-13 16:52:52
合計ジャッジ時間 12,743 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukiguni;

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

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		int a = sc.nextInt();
		int si = sc.nextInt();
		int sj = sc.nextInt();
		int b = sc.nextInt();
		int gi = sc.nextInt();
		int gj = sc.nextInt();
		char[][] m = new char[h][];
		for(int i=0;i<h;i++) {
			m[i] = sc.next().toCharArray();
		}
		sc.close();
		System.out.println(solve(h,w,a,b,si,sj,gi,gj,m));
	}

	public static final int[] DI = {0,-1,0,1};
	public static final int[] DJ = {1,0,-1,0};
	public static int solve(int h,int w,int a,int b,int si,int sj,int gi,int gj,char[][] m) {
		int max = Math.max(a+h+w-1,b+2*(h+w));
		int[][][] dist = new int[h][w][max+1];
		for(int i=0;i<h;i++) {
			for(int j=0;j<w;j++) {
				Arrays.fill(dist[i][j], 1 << 29); //INF
			}
		}
		Queue<Integer> q = new ArrayDeque<>();
		q.offer(a << 16 | si << 8 | sj);
		dist[si][sj][a] = 0;
		while(!q.isEmpty()) {
			int p = q.poll();
			int cs = p >>> 16;
			int ci = p >>> 8 & 0xff;
			int cj = p & 0xff;
			for(int i=0;i<4;i++) {
				int ni = ci + DI[i];
				int nj = cj + DJ[i];
				if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
					continue;
				}
				int ns = cs + (m[ni][nj] == '*' ? 1 : -1);
				if (ns > max || ns <= 0 || dist[ni][nj][ns] <= dist[ci][cj][cs] + 1) {
					continue;
				}
				dist[ni][nj][ns] = dist[ci][cj][cs] + 1;
				q.offer(ns << 16 | ni << 8 | nj);
			}
		}
		return dist[gi][gj][b] >= 1 << 29 ? -1 : dist[gi][gj][b];
	}

}
0