結果

問題 No.659 徘徊迷路
ユーザー tanzakutanzaku
提出日時 2017-03-26 23:28:30
言語 Java19
(openjdk 21)
結果
AC  
実行時間 220 ms / 2,000 ms
コード長 4,675 bytes
コンパイル時間 4,319 ms
コンパイル使用メモリ 75,348 KB
実行使用メモリ 58,480 KB
最終ジャッジ日時 2023-09-20 11:09:47
合計ジャッジ時間 8,227 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,960 KB
testcase_01 AC 183 ms
55,784 KB
testcase_02 AC 120 ms
55,744 KB
testcase_03 AC 117 ms
56,188 KB
testcase_04 AC 182 ms
57,884 KB
testcase_05 AC 137 ms
53,660 KB
testcase_06 AC 141 ms
56,344 KB
testcase_07 AC 121 ms
53,872 KB
testcase_08 AC 187 ms
57,868 KB
testcase_09 AC 211 ms
58,424 KB
testcase_10 AC 218 ms
58,480 KB
testcase_11 AC 218 ms
56,484 KB
testcase_12 AC 175 ms
55,692 KB
testcase_13 AC 209 ms
58,016 KB
testcase_14 AC 210 ms
57,992 KB
testcase_15 AC 215 ms
58,372 KB
testcase_16 AC 220 ms
58,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class _p1570_Validate {
	public static void main(String[] args) throws IOException {
		new _p1570_Validate().solve();
	}
	
	static int mod = (int)1e9+7;
	void solve() throws IOException {
		try (final InputValidator in = new InputValidator(System.in)) {
			int h = in.nextInt(3, 10);
			in.space();
			int w = in.nextInt(3, 10);
			in.space();
			int t = in.nextInt(1, 1_000_000_00);
			in.newLine();
			int sy = in.nextInt(1, 8);
			in.space();
			int sx = in.nextInt(1, 8);
			in.newLine();
			int gy = in.nextInt(1, 8);
			in.space();
			int gx = in.nextInt(1, 8);
			in.newLine();
			char[][] map = new char[h][w];
			for (int y = 0; y < h; y++) {
				for (int x = 0; x < w; x++) {
					map[y][x] = in.nextChar();
					if (x == w - 1) in.newLine();
					if (map[y][x] != '.' && map[y][x] != '#') throw new RuntimeException();
					if (x % (w-1) == 0 && map[y][x] == '.') throw new RuntimeException();
					if (y % (h-1) == 0 && map[y][x] == '.') throw new RuntimeException(); 
				}
			}
			in.eof();
			
			double[][] prob = new double[w*h][w*h];
			int[] d = new int[]{1, 0, -1, 0, 1};
			for (int y = 1; y < h - 1; y++) {
				for (int x = 1; x < w - 1; x++) if (map[y][x] == '.') {
					int cnt = 0;
					for (int i = 0; i < 4; i++) {
						int x2 = x + d[i];
						int y2 = y + d[i+1];
						if (map[y2][x2] == '.') {
							cnt++;
							prob[y*w+x][y2*w+x2] = 1;
						}
					}
					if (cnt == 0) {
						prob[y*w+x][y*w+x] = 1;
					} else {
						for (int i = 0; i < prob[y*w+x].length; i++) {
							prob[y*w+x][i] /= cnt;
						}
					}
				}
			}
			double[][] ans = new double[w*h][w*h];
			for (int i = 0; i < ans.length; i++) ans[i][i] = 1;
			for (int i = 1; i <= t; i <<= 1) {
				if ((t&i) != 0) { ans = mulmat(ans, prob); }
				prob = mulmat(prob, prob);
			}
			System.out.printf("%.10f\n", ans[sy*w+sx][gy*w+gx]);
		}
	}
	
	// a [n,v] * b [v,m] => c[n,m]
	static double[][] mulmat(double[][] a, double[][] b) {
		final long BIG = (2L * mod) * (2L * mod);
		
		assert(a[0].length == b.length);
		
		final int n = a.length;
		final int v = b.length;
		final int m = b[0].length;
		
		double[][] res = new double[n][m];
		for(int i = 0; i < n; i++)
			for(int k = 0; k < v; k++) {
				final double aa = a[i][k];
				for(int j = 0; j < m; j++) {
					res[i][j] += aa * b[k][j];
					if(res[i][j] >= BIG) res[i][j] -= BIG;
				}
			}
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				res[i][j] %= mod;
		
		return res;
	}
	
	static double[][] powmat(long r, double[][] mat) {
		final int n = mat.length;
		double[][] x = new double[n][n];
		for(int i = 0; i < n; i++) {
			x[i][i] = 1;
		}
		
		for(;r > 0; r >>>= 1) {
			if((r&1) == 1) {
				x = mulmat(x, mat);
			}
			mat = mulmat(mat, mat);
		}

		return x;
	}
	
	// for debug
	static void dump(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}
	
	static class InputValidator implements Closeable {
		final InputStream in;
		
		final int NO_BUFFER = -2;
		int buffer;
		
		public InputValidator(final InputStream in) {
			this.in = in;
			buffer = NO_BUFFER;
		}
		
		public char nextChar() throws IOException {
			int res = read();
			check("#.".indexOf((char)res) >= 0 || Character.isLetterOrDigit(res));
			return (char)res;
		}

		int read() throws IOException {
			final int res = buffer == NO_BUFFER ? in.read() : buffer;
			buffer = NO_BUFFER;
			return res;
		}
		
		void unread(int ch) throws IOException {
			buffer = ch;
		}
		
		// [low, high]
		long nextLong(long low, long high) throws IOException {
			long val = 0;
			int ch = -1;
			boolean read = false;
			while (true) {
				ch = read();
				if (!Character.isDigit(ch)) break;
				read = true;
				val = val * 10 + ch - '0';
				check(val <= high);
			}
			check(read);
			check(ch >= 0);
			check(val >= low);
			unread(ch);
			return val;
		}
		int nextInt(int low, int high) throws IOException { return (int)nextLong(low, high); }
		int[] nextInts(int n, int low, int high) throws IOException {
			int[] res = new int[n];
			for (int i = 0; i < n; i++) {
				res[i] = nextInt(low, high);
				if (i + 1 != n) space();
			}
			newLineOrEOF();
			return res;
		}
		
		void space() throws IOException { int ch = read(); check(ch == ' '); }
		void newLine() throws IOException {
			int ch = read();
			if (ch == '\r') ch = read();
			check(ch == '\n');
		}
		void newLineOrEOF() throws IOException { int ch = read(); check(ch == '\r' || ch == '\n' || ch < 0); }
		void eof() throws IOException { int ch = read(); check(ch < 0); }
		void check(boolean b) { if (!b) throw new RuntimeException(); }

		@Override
		public void close() throws IOException {
		}
	}
}
0