結果
| 問題 |
No.659 徘徊迷路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-03-25 12:21:53 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 66 ms / 2,000 ms |
| コード長 | 2,555 bytes |
| コンパイル時間 | 1,280 ms |
| コンパイル使用メモリ | 110,220 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-25 05:47:22 |
| 合計ジャッジ時間 | 2,407 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 12 |
ソースコード
#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;
// 行列の積
template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
int a = x.size();
int b = x[0].size();
int c = y[0].size();
vector<vector<T> > z(a, vector<T>(c, 0));
for(int i=0; i<a; ++i){
for(int j=0; j<c; ++j){
for(int k=0; k<b; ++k){
z[i][j] += x[i][k] * y[k][j];
}
}
}
return z;
}
// 行列の累乗
template <class T>
vector<vector<T> > matrixPower(const vector<vector<T> >& x, int k)
{
int n = x.size();
vector<vector<T> > y(n, vector<T>(n, 0));
for(int i=0; i<n; ++i)
y[i][i] = 1; // 積の単位元
vector<vector<T> > z = x;
while(k > 0){
if(k & 1)
y = matrixProduct(y, z);
z = matrixProduct(z, z);
k >>= 1;
}
return y;
}
const int dy[] = {-1, 0, 0, 1};
const int dx[] = {0, 1, -1, 0};
int main()
{
int h, w, t;
int sy, sx, gy, gx;
cin >> h >> w >> t >> sy >> sx >> gy >> gx;
int si = sy * w + sx;
int gi = gy * w + gx;
vector<string> grid(h);
for(int i=0; i<h; ++i)
cin >> grid[i];
int n = h * w;
vector<vector<double> > mat(n, vector<double>(n, 0.0));
for(int y=0; y<h; ++y){
for(int x=0; x<w; ++x){
if(grid[y][x] == '#')
continue;
int cnt = 0;
for(int k=0; k<4; ++k){
int y2 = y + dy[k];
int x2 = x + dx[k];
if(grid[y2][x2] != '#')
++ cnt;
}
int i = y * w + x;
if(cnt == 0){
mat[i][i] = 1.0;
}
else{
for(int k=0; k<4; ++k){
int y2 = y + dy[k];
int x2 = x + dx[k];
if(grid[y2][x2] != '#'){
int j = y2 * w + x2;
mat[j][i] += 1.0 / cnt;
}
}
}
}
}
mat = matrixPower(mat, t);
printf("%.10f\n", mat[gi][si]);
return 0;
}