結果

問題 No.659 徘徊迷路
ユーザー 37zigen37zigen
提出日時 2018-06-02 02:50:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 237 ms / 2,000 ms
コード長 2,243 bytes
コンパイル時間 2,889 ms
コンパイル使用メモリ 74,184 KB
実行使用メモリ 59,008 KB
最終ジャッジ日時 2023-09-12 21:30:35
合計ジャッジ時間 6,993 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
56,260 KB
testcase_01 AC 162 ms
55,852 KB
testcase_02 AC 155 ms
55,880 KB
testcase_03 AC 124 ms
55,932 KB
testcase_04 AC 175 ms
58,784 KB
testcase_05 AC 148 ms
56,200 KB
testcase_06 AC 149 ms
56,348 KB
testcase_07 AC 127 ms
56,136 KB
testcase_08 AC 178 ms
58,320 KB
testcase_09 AC 225 ms
59,008 KB
testcase_10 AC 233 ms
58,428 KB
testcase_11 AC 233 ms
58,676 KB
testcase_12 AC 159 ms
57,880 KB
testcase_13 AC 224 ms
58,396 KB
testcase_14 AC 221 ms
58,852 KB
testcase_15 AC 237 ms
58,744 KB
testcase_16 AC 233 ms
58,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int R = sc.nextInt();
		int C = sc.nextInt();
		int T = sc.nextInt();
		int Sy = sc.nextInt();
		int Sx = sc.nextInt();
		int Gy = sc.nextInt();
		int Gx = sc.nextInt();
		char[][] map = new char[R][C];
		for (int i = 0; i < R; ++i) {
			map[i] = sc.next().toCharArray();
		}
		double[][] to = new double[R * C][R * C];
		for (int i = 0; i < R; ++i) {
			for (int j = 0; j < C; ++j) {
				if (map[i][j] == '#')
					continue;
				double cnt = 0;
				for (int di = -1; di <= 1; ++di) {
					for (int dj = -1; dj <= 1; ++dj) {
						if (Math.abs(di) + Math.abs(dj) != 1)
							continue;
						if (i + di < 0 || i + di >= R || j + dj < 0 || j + dj >= C || map[i + di][j + dj] == '#')
							continue;
						++cnt;
					}
				}
				if (cnt == 0) {
					to[i * C + j][i * C + j] = 1;
				} else {
					for (int di = -1; di <= 1; ++di) {
						for (int dj = -1; dj <= 1; ++dj) {
							if (Math.abs(di) + Math.abs(dj) != 1)
								continue;
							if (i + di < 0 || i + di >= R || j + dj < 0 || j + dj >= C || map[i + di][j + dj] == '#')
								continue;
							to[(i + di) * C + (j + dj)][i * C + j] += 1 / cnt;
						}
					}
				}
			}
		}
		double[][] vec = new double[R * C][1];
		vec[Sy * C + Sx][0] = 1;
		vec = mul(pow(to, T), vec);
		System.out.println(vec[Gy * C + Gx][0]);
	}

	double[][] pow(double[][] A, long n) {
		double[][] ret = new double[A.length][A.length];
		for (int i = 0; i < A.length; ++i)
			ret[i][i] = 1;
		for (; n > 0; n >>= 1, A = mul(A, A)) {
			if (n % 2 == 1) {
				ret = mul(A, ret);
			}
		}
		return ret;
	}

	double[][] mul(double[][] a, double[][] b) {
		double[][] ret = new double[a.length][b[0].length];
		for (int i = 0; i < a.length; ++i) {
			for (int j = 0; j < b[i].length; ++j) {
				for (int k = 0; k < a[0].length; ++k) {
					ret[i][j] += a[i][k] * b[k][j];
				}
			}
		}
		return ret;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0