結果

問題 No.659 徘徊迷路
ユーザー tsutajtsutaj
提出日時 2018-03-02 22:48:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 3,508 bytes
コンパイル時間 1,243 ms
コンパイル使用メモリ 111,200 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 05:07:04
合計ジャッジ時間 2,478 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 10 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 15 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 17 ms
4,380 KB
testcase_09 AC 36 ms
4,380 KB
testcase_10 AC 42 ms
4,376 KB
testcase_11 AC 42 ms
4,380 KB
testcase_12 AC 9 ms
4,376 KB
testcase_13 AC 37 ms
4,380 KB
testcase_14 AC 37 ms
4,380 KB
testcase_15 AC 42 ms
4,380 KB
testcase_16 AC 43 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

// 行列ライブラリ
// Verified: TopCoder SRM 704 Div.2 (ModEquationEasy)
// 行列の積 と 累乗(繰り返し二乗法)

// Matrix Library Begin (C++11)

template <typename T>
using Matrix = vector< vector<T> >;

template <typename T>
void init_mat(Matrix<T> &A, int h, int w) {
    A.resize(h, vector<T>(w, 0));
}

template <typename T>
Matrix<T> calc_mat(Matrix<T> A, Matrix<T> B) {
    Matrix<T> C(A.size(), vector<T>(B[0].size()));
    for(int i=0; i<A.size(); i++) {
        for(int k=0; k<B.size(); k++) {
            for(int j=0; j<B[0].size(); j++) {
                C[i][j] += A[i][k] * B[k][j]; // modなし
                // C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; // modあり
            }
        }
    }
    return C;
}

template <typename T>
Matrix<T> mat_pow(Matrix<T> A, ll n) {
    Matrix<T> B(A.size(), vector<T>(A.size()));
    for(int i=0; i<A.size(); i++) B[i][i] = 1;
    while(n > 0) {
        if(n & 1) B = calc_mat(B, A);
        A = calc_mat(A, A);
        n >>= 1;
    }
    return B;
}

// Matrix Library End

int R, C, T;
int get_idx(int x, int y) {
    return C*x + y;
}
 
char board[15][15];
signed main() {
    cin >> R >> C >> T;

    int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy;
    for(int i=0; i<R; i++) {
        for(int j=0; j<C; j++) {
            cin >> board[i][j];
        }
    }

    Matrix<double> mat;
    init_mat(mat, R*C, R*C);
    for(int i=0; i<R; i++) {
        for(int j=0; j<C; j++) {
            if(board[i][j] != '.') continue;

            int cnt = 0;
            for(int k=0; k<4; k++) {
                int nx = i + dx[k], ny = j + dy[k];
                if(nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
                if(board[nx][ny] == '#') continue;
                cnt++;
            }

            for(int k=0; k<4; k++) {
                int nx = i + dx[k], ny = j + dy[k];
                if(nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
                if(board[nx][ny] == '#') continue;
                int i1 = get_idx(i, j), i2 = get_idx(nx, ny);
                mat[i2][i1] += 1.0 / cnt;
            }

            if(cnt == 0) {
                int idx = get_idx(i, j);
                mat[idx][idx] += 1;
            }
        }
    }

    Matrix <double> vec;
    init_mat(vec, R*C, 1);
    vec[get_idx(sx, sy)][0] = 1.0;

    mat = calc_mat(mat_pow(mat, T), vec);
    printf("%.12f\n", mat[get_idx(gx, gy)][0]);
    return 0;
}
0