結果

問題 No.659 徘徊迷路
ユーザー veqccveqcc
提出日時 2019-02-19 18:36:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 2,737 bytes
コンパイル時間 1,113 ms
コンパイル使用メモリ 106,668 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 17:52:36
合計ジャッジ時間 2,174 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 14 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 20 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 23 ms
5,376 KB
testcase_09 AC 54 ms
5,376 KB
testcase_10 AC 61 ms
5,376 KB
testcase_11 AC 61 ms
5,376 KB
testcase_12 AC 12 ms
5,376 KB
testcase_13 AC 50 ms
5,376 KB
testcase_14 AC 52 ms
5,376 KB
testcase_15 AC 61 ms
5,376 KB
testcase_16 AC 59 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

template<class T> struct Matrix {
    vector <vector<T>> val;
    Matrix(int n = 1, int m = 1) { val.clear(); val.resize(n, vector<T>(m)); }
    Matrix(int n, int m, T x) { val.clear(); val.resize(n, vector<T>(m, x)); }
    void init(int n, int m, T x = 0) { val.clear(); val.resize(n, vector<T>(m, x)); }
    void resize(int n, int m, T x = 0) { val.resize(n); for (int i = 0; i < n; ++i) val[i].resize(m, x); }
    int size() { return val.size(); }
    inline vector <T> &operator[](int i) { return val[i]; }
    friend ostream &operator<<(ostream &s, Matrix<T> M) { s << endl; for (int i = 0; i < M.val.size(); ++i) s << M[i] << endl; return s; }
};

template<class T> Matrix<T> operator * (Matrix<T> A, Matrix<T> B) {
    Matrix<T> R(A.size(), B[0].size());
    for (int i = 0; i < A.size(); ++i)
        for (int j = 0; j < B[0].size(); ++j)
            for (int k = 0; k < B.size(); ++k)
                R[i][j] += A[i][k] * B[k][j];
    return R;
}

template<class T> vector<T> operator * (Matrix<T> A, vector<T> B) {
    vector <T> v(A.size());
    for (int i = 0; i < A.size(); ++i)
        for (int k = 0; k < B.size(); ++k)
            v[i] += A[i][k] * B[k];
    return v;
}

template<class T> Matrix<T> pow(Matrix<T> A, int n) {
    Matrix<T> R(A.size(), A.size());
    for (int i = 0; i < A.size(); ++i) R[i][i] = 1;
    while (n > 0) { if (n & 1) R = R * A; A = A * A; n >>= 1; }
    return R;
}

int main() {
    int r, c, t, sy, sx, gy, gx;
    cin >> r >> c >> t >> sy >> sx >> gy >> gx;
    string b[r];
    for (int i = 0; i < r; i++) {
        cin >> b[i];
    }

    int n = r * c;
    Matrix <double> M(n, n, 0.0);
    for (int i = 0; i < n; i++) {
        int x = i / c, y = i % c;
        if (b[x][y] == '#') continue;

        int count = 0;
        if (b[x-1][y] == '.') count++;
        if (b[x][y-1] == '.') count++;
        if (b[x+1][y] == '.') count++;
        if (b[x][y+1] == '.') count++;

        if (count == 0) {
            M[i][i] = 1.0;
        } else {
            double prob = 1.0 / (double)count;
            if (b[x-1][y] == '.') M[i-c][i] = prob;
            if (b[x][y-1] == '.') M[i-1][i] = prob;
            if (b[x+1][y] == '.') M[i+c][i] = prob;
            if (b[x][y+1] == '.') M[i+1][i] = prob;
        }
    }

    vector <double> vec(n, 0.0);
    vec[sy*c+sx] = 1.0;

    Matrix <double> P = pow(M, t);
    vector <double> V = P * vec;

    cout << fixed << setprecision(15) << V[gy*c+gx] << "\n";
    return 0;
}
0