結果
問題 | No.659 徘徊迷路 |
ユーザー | tanzaku |
提出日時 | 2017-03-26 23:28:30 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 220 ms / 2,000 ms |
コード長 | 4,675 bytes |
コンパイル時間 | 3,154 ms |
コンパイル使用メモリ | 79,480 KB |
実行使用メモリ | 56,192 KB |
最終ジャッジ日時 | 2024-07-06 06:35:59 |
合計ジャッジ時間 | 6,598 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 112 ms
53,888 KB |
testcase_01 | AC | 166 ms
54,324 KB |
testcase_02 | AC | 105 ms
53,772 KB |
testcase_03 | AC | 100 ms
54,152 KB |
testcase_04 | AC | 176 ms
56,192 KB |
testcase_05 | AC | 123 ms
41,144 KB |
testcase_06 | AC | 120 ms
41,784 KB |
testcase_07 | AC | 86 ms
39,764 KB |
testcase_08 | AC | 170 ms
43,048 KB |
testcase_09 | AC | 194 ms
44,816 KB |
testcase_10 | AC | 219 ms
45,128 KB |
testcase_11 | AC | 203 ms
44,992 KB |
testcase_12 | AC | 172 ms
42,984 KB |
testcase_13 | AC | 194 ms
45,120 KB |
testcase_14 | AC | 196 ms
44,688 KB |
testcase_15 | AC | 220 ms
45,308 KB |
testcase_16 | AC | 215 ms
45,072 KB |
ソースコード
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 { } } }