結果
問題 | No.659 徘徊迷路 |
ユーザー | tanzaku |
提出日時 | 2017-04-03 22:34:09 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 362 ms / 2,000 ms |
コード長 | 5,268 bytes |
コンパイル時間 | 3,981 ms |
コンパイル使用メモリ | 80,068 KB |
実行使用メモリ | 41,568 KB |
最終ジャッジ日時 | 2024-07-08 05:34:35 |
合計ジャッジ時間 | 7,588 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 112 ms
40,864 KB |
testcase_01 | AC | 97 ms
39,860 KB |
testcase_02 | AC | 154 ms
41,004 KB |
testcase_03 | AC | 134 ms
41,568 KB |
testcase_04 | AC | 273 ms
41,460 KB |
testcase_05 | AC | 106 ms
40,704 KB |
testcase_06 | AC | 115 ms
40,968 KB |
testcase_07 | AC | 106 ms
40,588 KB |
testcase_08 | AC | 142 ms
41,292 KB |
testcase_09 | AC | 300 ms
41,540 KB |
testcase_10 | AC | 362 ms
41,280 KB |
testcase_11 | AC | 312 ms
41,424 KB |
testcase_12 | AC | 90 ms
40,060 KB |
testcase_13 | AC | 323 ms
41,160 KB |
testcase_14 | AC | 319 ms
41,372 KB |
testcase_15 | AC | 267 ms
41,436 KB |
testcase_16 | AC | 316 ms
41,364 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)) { h = in.nextInt(3, 10); in.space(); w = in.nextInt(3, 10); in.space(); t = in.nextInt(1, 1_000_000_00); in.newLine(); sy = in.nextInt(1, 8); in.space(); sx = in.nextInt(1, 8); in.newLine(); gy = in.nextInt(1, 8); in.space(); gx = in.nextInt(1, 8); in.newLine(); 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 (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.newLine(); } in.eof(); System.out.printf("%.10f\n", solveIterate()); // System.out.printf("%.10f\n", solveMatrix()); } } int w, h, t, sx, sy, gx, gy; char[][] map; double solveIterate() { double[][] prob = new double[2][h*w]; prob[0][sy*w+sx] = 1; int[] d = new int[]{1, 0, -1, 0, 1}; int it; for (it = 0; it < t && it < 100000 + t%2; it++) { double[] cur = prob[it&1], next = prob[1^it&1]; Arrays.fill(next, 0); for (int y = 1; y < h - 1; y++) { for (int x = 1; x < w - 1; 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++; } } if (cnt == 0) { next[y*w+x] = cur[y*w+x]; } else { for (int i = 0; i < 4; i++) { int x2 = x + d[i]; int y2 = y + d[i+1]; if (map[y2][x2] == '.') { next[y2*w+x2] += cur[y*w+x] / cnt; } } } } } } return prob[it&1][gy*w+gx]; } double solveMatrix() { 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); } return 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; } // 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 { } } }