結果

問題 No.659 徘徊迷路
コンテスト
ユーザー linjunye
提出日時 2025-11-14 12:42:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 1,836 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 205,180 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-11-14 12:42:55
合計ジャッジ時間 4,479 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int N=500010;
const int mod=1e9+7;
const int INF=0x3f3f3f3f3f3f3f3f;
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
// const int mod=1e9+7;
namespace Matrix{
	typedef vector<vector<double> > Mat;
	Mat init(int n,double c){return Mat(n,vector<double>(n,c));}
	void print(Mat a){
		cerr<<"---------------------------\n";
		for(auto x:a){
			for(auto y:x)cerr<<y<<" ";
			cerr<<"\n";
		}
		cerr<<"---------------------------\n";
		return;
	}
	Mat operator * (Mat a,Mat b){
		assert(a.size()==b.size());
		int n=a.size();
		Mat z=init(n,0);
		for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)z[i][j]+=a[i][k]*b[k][j];
		return z;
	}
	Mat operator ^ (Mat a,int b){
		int n=a.size();
		Mat z=init(n,0);
		for(int i=0;i<n;i++)z[i][i]=1;
		while(b){
			if(b&1)z=z*a;
			a=a*a;
			b>>=1;
		}
		return z;
	}
}
using namespace Matrix;
int n,m,k;
char ch[110][110];
int sx,sy,tx,ty;
int dot(int x,int y){
	return x*m+y;
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>k;
	cin>>sx>>sy>>tx>>ty;
	for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>ch[i][j];
	Mat a,b;
	a=init(n*m,0.0);
	b=init(n*m,0.0);
	a[dot(sx,sy)][0]=1.0;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(ch[i][j]=='#')continue;
			int cnt=0;
			for(int k=0;k<4;k++){
				int nx=i+dx[k];
				int ny=j+dy[k];
				if(nx<0||nx>=n||ny<0||ny>=m||ch[nx][ny]=='#')continue;
				cnt++;
			}
			if(cnt==0)b[dot(i,j)][dot(i,j)]=1.0;
			else{
				for(int k=0;k<4;k++){
					int nx=i+dx[k];
					int ny=j+dy[k];
					if(nx<0||nx>=n||ny<0||ny>=m||ch[nx][ny]=='#')continue;
					b[dot(nx,ny)][dot(i,j)]=1.0/(double)cnt;
				}
			}
		}
	}
	// print(b);
	b=b^k;
	Mat ans=b*a;
	cout<<fixed<<setprecision(16)<<ans[dot(tx,ty)][0]<<"\n";
	return 0;
}
0