結果

問題 No.659 徘徊迷路
ユーザー algon_320algon_320
提出日時 2018-03-03 00:51:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 4,057 bytes
コンパイル時間 1,945 ms
コンパイル使用メモリ 182,836 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 21:46:34
合計ジャッジ時間 4,830 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 57 ms
4,348 KB
testcase_02 AC 3 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 87 ms
4,348 KB
testcase_05 AC 4 ms
4,348 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 95 ms
4,348 KB
testcase_09 AC 229 ms
4,348 KB
testcase_10 AC 263 ms
4,348 KB
testcase_11 AC 263 ms
4,348 KB
testcase_12 AC 49 ms
4,348 KB
testcase_13 AC 229 ms
4,348 KB
testcase_14 AC 230 ms
4,348 KB
testcase_15 AC 270 ms
4,348 KB
testcase_16 AC 269 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) ((y)<0||(x)<0||(y)>=(h)||(x)>=(w))
#define dump(...)
const signed INF_=1001001001; const ll INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}return 0;}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct{template<class T> operator T(){T x;cin>>x;return x;}} IN;
struct before_main_function {
    before_main_function() {
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
        cerr<<setprecision(4)<<fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

template<typename T>
void resize_matrix(vector<vector<T>> &A, int h, int w, T fill) {
    A.resize(h);
    for (int i = 0; i < h; i++) {
        A[i].resize(w, fill);
    }
}
template<typename T>
vector<vector<T>> multiple_matrix(vector<vector<T>> &A, vector<vector<T>> &B,
  function<T(T, T)> add = [](T a, T b){return a + b;},
  function<T(T, T)> mul = [](T a, T b){return a * b;},
  T zero = 0) {
    int m = A.size();
    int n = A[0].size();
    assert(n == B.size());
    int l = B[0].size();
    vector<vector<T>> res;
    resize_matrix(res, m, l, zero);
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < l; j++) {
            T tmp = 0;
            for (int k = 0; k < n; k++) {
                T p = mul(A[i][k], B[k][j]);
                tmp = add(tmp, p);
            }
            res[i][j] = tmp;
        }
    }
    return res;
}
template <typename T>
vector<vector<T>> pow_matrix(vector<vector<T>> A, int k,
  function<T(T, T)> add = [](T a, T b){return a + b;},
  function<T(T, T)> mul = [](T a, T b){return a * b;},
  T e = 1, T zero = 0) {
    int n = A.size();
    assert(n == A[0].size());

    vector<vector<T>> res;
    resize_matrix(res, n, n, zero);
    for (int i = 0; i < n; i++) res[i][i] = e;
    while (k > 0) {
        if (k & 1) res = multiple_matrix(res, A, add, mul, zero);
        A = multiple_matrix(A, A, add, mul, zero);
        k >>= 1;
    }
    return res;
}
signed main() {
    int R,C,T;
    cin>>R>>C>>T;
    int sy,sx,gy,gx;
    cin>>sy>>sx>>gy>>gx;
    vector<string> S(R);
    cin>>S;

    vector<vector<double>> Q(R*C,vector<double>(C*R,0));
    REP(i,R) {
        REP(j,C) {
            if(S[i][j]=='#') continue;

            int pos=i*C+j;

            int c=0;
            REP(d,4) {
                int ii=i+DY[d];
                int jj=j+DX[d];
                if(OUTOFRANGE(ii,jj,R,C)) continue;
                if(S[ii][jj]=='.') c++;
            }
            REP(d,4) {
                int ii=i+DY[d];
                int jj=j+DX[d];
                int next=ii*C+jj;
                if(OUTOFRANGE(ii,jj,R,C)) continue;
                if(S[ii][jj]=='.' && c>0) Q[pos][next]+=1.0/c;
            }
            if(c==0) {
                Q[pos][pos]+=1;
            }
        }
    }
    Q=pow_matrix(Q, T);
    int s=sy*C+sx;
    int g=gy*C+gx;
    cout<<Q[s][g]<<endl;
    return 0;
}

0