結果

問題 No.659 徘徊迷路
ユーザー furafura
提出日時 2020-06-23 11:27:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 2,201 bytes
コンパイル時間 2,012 ms
コンパイル使用メモリ 206,064 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-16 19:50:26
合計ジャッジ時間 3,435 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 14 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 20 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 24 ms
4,376 KB
testcase_09 AC 54 ms
4,376 KB
testcase_10 AC 63 ms
4,376 KB
testcase_11 AC 62 ms
4,376 KB
testcase_12 AC 13 ms
4,376 KB
testcase_13 AC 55 ms
4,380 KB
testcase_14 AC 55 ms
4,380 KB
testcase_15 AC 63 ms
4,376 KB
testcase_16 AC 64 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class R>
class matrix{
	vector<vector<R>> a;
public:
	matrix(int n):a(n,vector<R>(n)){}
	matrix(int m,int n):a(m,vector<R>(n)){}

	matrix& operator+=(const matrix& A){
		assert(h()==A.h() && w()==A.w());
		int m=h(),n=w();
		rep(i,m) rep(j,n) (*this)[i][j]+=A[i][j];
		return *this;
	}
	matrix& operator-=(const matrix& A){
		assert(h()==A.h() && w()==A.w());
		int m=h(),n=w();
		rep(i,m) rep(j,n) (*this)[i][j]-=A[i][j];
		return *this;
	}
	matrix& operator*=(const matrix& A){
		assert(w()==A.h());
		int m=h(),n=w(),l=A.w();
		matrix B(m,l);
		rep(i,m) rep(j,l) rep(k,n) B[i][j]+=(*this)[i][k]*A[k][j];
		swap(*this,B);
		return *this;
	}
	matrix operator+(const matrix& A)const{ return matrix(*this)+=A; }
	matrix operator-(const matrix& A)const{ return matrix(*this)-=A; }
	matrix operator*(const matrix& A)const{ return matrix(*this)*=A; }
	const vector<R>& operator[](int i)const{ return a[i]; }
	vector<R>& operator[](int i){ return a[i]; }

	vector<R> operator*(const vector<R>& v)const{
		assert(w()==v.size());
		int m=h(),n=w();
		vector<R> res(m);
		rep(i,m) rep(j,n) res[i]+=(*this)[i][j]*v[j];
		return res;
	}

	int h()const{ return a.size(); }
	int w()const{ return a.empty()?0:a[0].size(); }

	static matrix identity(int n){
		matrix I(n);
		rep(i,n) I[i][i]=R{1};
		return I;
	}
};

template<class R>
matrix<R> pow(matrix<R> A,long long k){
	assert(A.h()==A.w());
	matrix<R> B=matrix<R>::identity(A.h());
	for(;k>0;k>>=1){
		if(k&1) B*=A;
		A*=A;
	}
	return B;
}

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};

int main(){
	int h,w,l; scanf("%d%d%d",&h,&w,&l);
	int sy,sx,gy,gx; scanf("%d%d%d%d",&sy,&sx,&gy,&gx);
	char B[10][11];
	rep(i,h) scanf("%s",B[i]);

	matrix<double> A(h*w);
	rep(i,h) rep(j,w) if(B[i][j]=='.') {
		int cnt=0;
		rep(k,4){
			int y=i+dy[k],x=j+dx[k];
			if(0<=y && y<h && 0<=x && x<w && B[y][x]=='.') cnt++;
		}
		if(cnt>0){
			rep(k,4){
				int y=i+dy[k],x=j+dx[k];
				if(0<=y && y<h && 0<=x && x<w && B[y][x]=='.'){
					A[y*w+x][i*w+j]+=1.0/cnt;
				}
			}
		}
		else{
			A[i*w+j][i*w+j]=1;
		}
	}
	printf("%.9f\n",pow(A,l)[gy*w+gx][sy*w+sx]);

	return 0;
}
0