結果
| 問題 |
No.659 徘徊迷路
|
| コンテスト | |
| ユーザー |
tanzaku
|
| 提出日時 | 2017-04-03 22:34:09 |
| 言語 | Java (openjdk 23) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 |
ソースコード
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 {
}
}
}
tanzaku