# @topcoder - SRM614D1L3@ TorusSailing

## @accepted code@

#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;

class TorusSailing{
private:
#define MAXN (200)
struct node{
double a[MAXN], b; int cnt;
node() {}
node(int n) {
cnt = n, b = 0;
for(int i=0;i<n;i++)
a[i] = 0;
}
friend node operator + (const node &x, const node &y) {
node z(x.cnt); z.b = x.b + y.b;
for(int i=0;i<x.cnt;i++) z.a[i] = x.a[i] + y.a[i];
return z;
}
friend node operator + (const node &x, const double &k) {
node z = x; z.b += k;
return z;
}
friend node operator / (const node &x, const double &k) {
node z(x.cnt); z.b = x.b / k;
for(int i=0;i<x.cnt;i++) z.a[i] = x.a[i]/k;
return z;
}
}a[MAXN][MAXN];

double A[MAXN][MAXN];
void gauss(int n, int m) {
int r = 0, c = 0;
while( r < n && c < m ) {
int mxr = r;
for(int i=r+1;i<n;i++)
if( fabs(A[i][c]) >= fabs(A[mxr][c]) )
mxr = i;
if( r != mxr ) {
for(int j=c;j<m;j++)
swap(A[r][j], A[mxr][j]);
}
if( A[r][c] ) {
double k = A[r][c];
for(int j=c;j<m;j++)
A[r][j] /= k;
for(int i=0;i<n;i++) {
if( i == r ) continue;
k = A[i][c];
for(int j=c;j<m;j++)
A[i][j] -= k*A[r][j];
}
r++;
}
c++;
}
}
public:
double expectedTime(int N, int M, int goalX, int goalY) {
int K = (N - 1) + (M - 1);
for(int i=0;i<N-1;i++)
a[i][M-1] = node(K), a[i][M-1].a[i] = 1;
for(int j=0;j<M-1;j++)
a[N-1][j] = node(K), a[N-1][j].a[j+N-1] = 1;
a[N-1][M-1] = node(K);
for(int j=M-2;j>=0;j--)
for(int i=N-2;i>=0;i--)
a[i][j] = (a[i+1][j] + a[i][j+1]) / 2 + 1;
for(int i=0;i<N-1;i++) {
node b = (a[i][0] + a[i+1][M-1]) / 2 + 1;
for(int j=0;j<K;j++) A[i][j] = -b.a[j];
A[i][i]++, A[i][K] = b.b;
}
for(int j=0;j<M-1;j++) {
node b = (a[0][j] + a[N-1][j+1]) / 2 + 1;
for(int i=0;i<K;i++) A[j+N-1][i] = -b.a[i];
A[j+N-1][j+N-1]++, A[j+N-1][K] = b.b;
}
gauss(K, K + 1);
int sx = N - goalX - 1, sy = M - goalY - 1;
double ans = a[sx][sy].b;
for(int i=0;i<a[sx][sy].cnt;i++)
ans += a[sx][sy].a[i] * A[i][K];
return ans;
}
};


## @details@

posted @ 2020-01-08 15:36  Tiw_Air_OAO  阅读(91)  评论(0编辑  收藏