結果

問題 No.659 徘徊迷路
ユーザー tetsutetsu
提出日時 2018-03-03 17:02:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 2,945 bytes
コンパイル時間 3,175 ms
コンパイル使用メモリ 74,928 KB
実行使用メモリ 54,360 KB
最終ジャッジ日時 2023-09-12 16:49:47
合計ジャッジ時間 5,689 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
49,428 KB
testcase_01 AC 92 ms
51,928 KB
testcase_02 AC 48 ms
49,516 KB
testcase_03 AC 43 ms
49,320 KB
testcase_04 AC 97 ms
52,008 KB
testcase_05 AC 51 ms
49,372 KB
testcase_06 AC 68 ms
51,208 KB
testcase_07 AC 45 ms
49,376 KB
testcase_08 AC 101 ms
51,920 KB
testcase_09 AC 134 ms
54,028 KB
testcase_10 AC 146 ms
54,024 KB
testcase_11 AC 145 ms
53,904 KB
testcase_12 AC 91 ms
51,860 KB
testcase_13 AC 135 ms
53,464 KB
testcase_14 AC 134 ms
53,864 KB
testcase_15 AC 146 ms
54,236 KB
testcase_16 AC 149 ms
54,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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());
		}
	}
}
0