結果

問題 No.659 徘徊迷路
ユーザー homesentinelhomesentinel
提出日時 2018-03-02 23:57:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 147 ms / 2,000 ms
コード長 3,677 bytes
コンパイル時間 1,353 ms
コンパイル使用メモリ 127,396 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 02:30:08
合計ジャッジ時間 3,437 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 32 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 46 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 52 ms
4,380 KB
testcase_09 AC 125 ms
4,376 KB
testcase_10 AC 143 ms
4,376 KB
testcase_11 AC 143 ms
4,376 KB
testcase_12 AC 27 ms
4,380 KB
testcase_13 AC 125 ms
4,376 KB
testcase_14 AC 124 ms
4,376 KB
testcase_15 AC 146 ms
4,380 KB
testcase_16 AC 147 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する
typedef vector<long double> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B) {
    mat C(A.size(), vec(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] = (C[i][j] + A[i][k] * B[k][j]);
            }
        }
    }
    return C;
}

mat pow(mat A, ll n) {
    mat B(A.size(), vec(A.size()));
    for (int i = 0; i < A.size(); i++) {
        B[i][i] = 1;
    }
    while (n > 0) {
        if (n & 1) B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;

}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int R, C, T;
    cin >> R >> C >> T;
    int sx, sy, gx, gy;
    cin >> sy >> sx >> gy >> gx;
    vector<vector<char> > B(R, vector<char>(C));
    vector<vector<int> > vv(R, vector<int>(C));

    rep(i, 0, R) {
        rep(j, 0, C) {
            char c;
            cin >> c;
            B[i][j] = c;
        }
    }

    auto conv = [&](int x, int y) {
        return y * C + x;
    };
    int dx[] = {1, 0, -1, 0};
    int dy[] = {0, 1, 0, -1};
    rep(i, 0, R) {
        rep(j, 0, C) {
            if (B[i][j] == '#') continue;
            rep(k, 0, 4) {
                if (B[i + dy[k]][j + dx[k]] != '#')
                    vv[i][j]++;
            }
        }
    }
    if(vv[sy][sx] == 0){
        if(sy == gy && sx == gx){
            printf("%.7Lf\n",1.0L);
        }else{
            printf("%.7Lf\n",0.0L);
        }
        return 0;
    }
    vec b(R * C);
    b[conv(sx, sy)] = 1.0;
    mat A(R * C, vec(R * C));
//    rep(i,0,R){
//        rep(j,0,C){
//            if(B[i][j] != '#'){
//                rep(k,0,4){
//                    A[i][j] += 1.0/vv[i+dy[k]][j+dx[k]];
//                }
//            }
//        }
//    }
    rep(i, 0, R) {
        rep(j, 0, C) {
            if (B[i][j] != '#') {
                rep(k, 0, 4) {
                    if (vv[i + dy[k]][j + dx[k]] != 0)
                        A[conv(j, i)][conv(j + dx[k], i + dy[k])] = 1.0 / vv[i + dy[k]][j + dx[k]];
                }
            }
        }
    }


    A = pow(A, T);
//    cout << A[conv(gx, gy)][conv(sx, sy)] << endl;
    printf("%.7Lf\n",A[conv(gx, gy)][conv(sx, sy)]);


    return 0;
}
0