結果
問題 | No.659 徘徊迷路 |
ユーザー |
|
提出日時 | 2018-03-03 17:02:13 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 173 ms / 2,000 ms |
コード長 | 2,945 bytes |
コンパイル時間 | 3,670 ms |
コンパイル使用メモリ | 78,216 KB |
実行使用メモリ | 54,776 KB |
最終ジャッジ日時 | 2024-06-30 04:44:01 |
合計ジャッジ時間 | 6,268 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 12 |
ソースコード
package yukicoder; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class P659 { static double [][] pow(double[][] A, long n) { if(n==1) { return A; } if(n%2==0) { double[][] B = pow(A, n/2); return mul(B, B); } else { double[][] B = mul(A, pow(A, n-1)); return B; } } static double[][] mul(double [][] A, double [][] B) { assert A[0].length == B.length; int n = A.length; int k = A[0].length; int m = B[0].length; double[][] C = new double[n][m]; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { for(int l=0; l<k; l++) { C[i][j] = (C[i][j] + A[i][l]*B[l][j]); } } } return C; } static int[][] ds = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static void main(String[] args) throws IOException { MyScanner sc = new MyScanner(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++) { String s = sc.next(); for(int j=0; j<c; j++) map[i][j] = s.charAt(j); } { int cnt = 0; for(int k=0; k<4; k++) { int ny = sy+ds[k][0]; int nx = sx+ds[k][1]; if(0<=ny && ny < r && 0<=nx && nx < c && map[ny][nx]=='.') cnt++; } if(cnt==0 && gx == sx && gy == sy) { System.out.println(1); return; } if(cnt==0) { System.out.println(0); return; } } double[][] A = new double[r*c][r*c]; for(int y=0; y<r; y++) { for(int x=0; x<c; x++) { int cnt = 0; for(int k=0; k<4; k++) { int ny = y+ds[k][0]; int nx = x+ds[k][1]; if(0<=ny && ny < r && 0<=nx && nx < c && map[ny][nx]=='.') cnt++; } int here = c*y+x; if(cnt==0) { A[here][here] = 1; continue; } for(int k=0; k<4; k++) { int ny = y+ds[k][0]; int nx = x+ds[k][1]; int next = c*ny+nx; if(0<=ny && ny < r && 0<=nx && nx < c && map[ny][nx]=='.') A[here][next] = (double)1/(double)cnt; } } } System.out.println(pow(A, t)[sy*c+sx][gy*c+gx]); } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner(InputStream s) { br=new BufferedReader(new InputStreamReader(s)); } public String nextLine() throws IOException { return br.readLine(); } public String next() throws IOException { while(st==null || !st.hasMoreTokens()) st=new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }