Problem:
Problem G: スプリング・タイル
ある朝、あなたが目覚めると、そこはバネだらけの迷宮の中だった。
なぜ自分がこんな見知らぬ場所にいるのかはわからない。この場でじっと助けを待つという選択肢も
あるにはあるが、こういった迷宮の中に長居していると、いつかは必ず突風が吹いてきて吹き飛ばさ
れてしまうことを、あなたは経験上知っていた。しかし、突風が吹いてくるまでの時間は判断のしよ
うがない。そこであなたは、迷宮から脱出するまでに必要な移動回数の期待値が最小になるように行
動するのが最善の戦略だと考えた。
足元に落ちていた正体不明の巻物を拾ってためしに読んでみたところ、偶然にも迷宮の全体マップを
知覚することができた。さらに、たまたま持っていた草を飲むことで、すべてのワナの位置も知るこ
とができた。どうやらこの迷宮には、危険なモンスターやバネ以外のワナは存在しないらしい。
この迷宮は、いくつかの種類のタイルが長方形に並んだ形をしており、たとえば以下のようにあらわ
される。
########
#..##g.#
#*#.*#.#
#......#
#*s#*.*#
########
「.」：床。この上なら自由に歩き回ることができる。
「#」：壁。あなたは壁の中に入ることはできない。
「s」：あなたの最初の位置。このタイルの下は床になっている。
「g」：階段。このタイルの上に乗れば、迷宮を脱出したことになる。
「*」：バネ。このタイルの上に乗ると、いずれかの床の上（階段、バネのタイルは除く）にランダムに飛ばされる。それぞれの床に飛ばされる確率はすべて等しい。
タイルの種類は、この５つのいずれかである。
あなたは、現在乗っているタイルから隣り合ったタイルへ、上下左右いずれかの方向に１マスずつ移
動することができる。ただし、ナナメに移動することはできない。
迷宮のマップが与えられたとき、最善の戦略をとった場合に迷宮から脱出するまでに必要な移動回数
の期待値を求めよ。バネで飛ばされるのは移動回数には含めない。
Input
W H
c
11
c
12
...
c
1
w
c
21
c
22
...
c
2
w
::    :
c
h
1
c
h
2
...
c
c
w
入力の１行目には、整数
W
（3 ≤
W
≤ 500）と整数
H
（3 ≤
H
≤ 500）が、この順に空白区切りで書かれている。整数
W
は迷宮の横幅を、整数
H
は迷宮の縦幅をあらわす。
続く
H
行には、迷宮のマップをあらわす
W
個の文字が書かれている（空白区切りではない）。この部分の形式は、先に挙げた例のとおりである。なお、データ中に「s」と「g」は、それぞれちょうど１回ずつ出現する。また、迷宮の周囲１マスは、必ず壁で囲まれている。
なお、与えられる迷宮では、どのように移動し、バネでどのように飛ばされたとしても、「バネで飛
ばされる床を任意に設定できたとしても脱出不可能な状態」に陥ることはないと仮定してよい。
Output
最善の戦略をとったときに、脱出までにかかる移動回数の期待値を出力せよ。出力は誤差を含んでい
てもよいが、真の値との相対誤差が10
-9
未満でなければならない。
Sample Input 1
8 6
########
#..##g.#
#*#.*#.#
#......#
#*s#*.*#
########
Sample Output 1
5.857142857143
Sample Input 2
21 11
#####################
#*#*.*.*.*.*.*.*.*#*#
#*#...............#*#
#*#.#.#.#.#.#.#.#.#*#
##.#.#.#.#.#.#.#.#.##
#...................#
##.#.#.#.#.#.#.#.#.##
#s#.#.#.#.#.#.#.#.#g#
#...................#
#...................#
#####################
Sample Output 2
20.000000000000
Sample Input 3
9 8
#########
#...#*.*#
#.*.#...#
#...#*.*#
#########
#.s....g#
#.*#.*..#
#########
Sample Output 3
5.000000000000

Submitted solutions:

code_snippet: #include <queue>
#include <string>
#include <iomanip>
#include <iostream>
#pragma warning(disable : 4996)
using namespace std;
struct state {
	int x, y; long double dist;
};
bool operator<(const state& s1, const state& s2) {
	return s1.dist < s2.dist;
}
int dir[4] = { 0, 1, 0, -1 };
int H, W, sx, sy; long double dist[555][555]; string s[555];
int main() {
	cin >> W >> H;
	for (int i = 0; i < H; i++) {
		cin >> s[i];
		if (s[i].find('s') != string::npos) {
			sx = i; sy = s[i].find('s');
		}
	}
	long double l = 0.0, r = 1.0e+10;
	for (int i = 0; i < 80; i++) {
		long double m = (l + r) * 0.5;
		priority_queue<state> que;
		for (int j = 0; j < H; j++) {
			for (int k = 0; k < W; k++) {
				dist[j][k] = 1.0e+15;
				if (s[j][k] == 'g') {
					dist[j][k] = 0.0;
					que.push(state{ j, k, 0.0 });
				}
				if (s[j][k] == '*') {
					dist[j][k] = m;
					que.push(state{ j, k, -m });
				}
			}
		}
		while (!que.empty()) {
			state u = que.top(); que.pop();
			for (int i = 0; i < 4; i++) {
				int tx = u.x + dir[i], ty = u.y + dir[i ^ 1];
				if (0 <= tx && tx < H && 0 <= ty && ty < W && s[tx][ty] != '#' && s[tx][ty] != '*' && dist[tx][ty] > dist[u.x][u.y] + 1.0L) {
					dist[tx][ty] = dist[u.x][u.y] + 1.0L;
					que.push(state{ tx, ty, -dist[tx][ty] });
				}
			}
		}
		long double sum = 0.0; int cnt = 0;
		for (int i = 0; i < H; i++) {
			for (int j = 0; j < W; j++) {
				if (s[i][j] == 's' || s[i][j] == '.') {
					sum += dist[i][j];
					cnt++;
				}
			}
		}
		if (sum < m * cnt) r = m;
		else l = m;
	}
	cout << fixed << setprecision(10) << dist[sx][sy] << endl;
	return 0;
}
accuracy: 1
time_ms: 1730
memory_kb: 10468
final_rank: 4

code_snippet: // AOJ #2336
// Spring Tiles 2025.2.10

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1000000000000000LL;
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int W, H;
    cin >> W >> H;
    vector<string> grid(H);
    for (int i = 0; i < H; i++) cin >> grid[i];
 
    int start_r = -1, start_c = -1;
    vector<pair<int,int>> floorCells;
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            char ch = grid[r][c];
            if(ch == 's') start_r = r, start_c = c;
            if(ch == '.' || ch == 's')
                floorCells.push_back({r, c});
        }
    }
    int N = floorCells.size();
 
    vector<vector<ll>> Vdist(H, vector<ll>(W, INF));
    deque<pair<int,int>> dq;
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            if(grid[r][c] == 'g'){
                Vdist[r][c] = 0;
                dq.push_back({r, c});
            }
        }
    }
    int dr[4] = {1, -1, 0, 0};
    int dc[4] = {0, 0, 1, -1};
    while(!dq.empty()){
        auto [r, c] = dq.front(); dq.pop_front();
        ll d = Vdist[r][c];
        for (int i = 0; i < 4; i++){
            int nr = r + dr[i], nc = c + dc[i];
            if(nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
            char ch = grid[nr][nc];
            if(ch == '#' || ch == '*') continue;
            if(Vdist[nr][nc] > d + 1){
                Vdist[nr][nc] = d + 1;
                dq.push_back({nr, nc});
            }
        }
    }
 
    vector<vector<ll>> Udist(H, vector<ll>(W, INF));
    deque<pair<int,int>> dq2;
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            if(grid[r][c] == '*'){
                Udist[r][c] = 0;
                dq2.push_back({r, c});
            }
        }
    }
    while(!dq2.empty()){
        auto [r, c] = dq2.front(); dq2.pop_front();
        ll d = Udist[r][c];
        for (int i = 0; i < 4; i++){
            int nr = r + dr[i], nc = c + dc[i];
            if(nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
            char ch = grid[nr][nc];
            if(ch == '#' || ch == 'g') continue;
            if(Udist[nr][nc] > d + 1){
                Udist[nr][nc] = d + 1;
                dq2.push_back({nr, nc});
            }
        }
    }
 
    vector<ll> floorV, floorU;
    floorV.reserve(N);
    floorU.reserve(N);
    for(auto &p : floorCells){
        int r = p.first, c = p.second;
        floorV.push_back(Vdist[r][c]);
        floorU.push_back(Udist[r][c]);
    }
 
    auto f = [&](double m_val) -> double {
        double s = 0.0;
        for (int i = 0; i < N; i++){
            double candidate = (double) floorU[i] + m_val;
            double v = (double) floorV[i];
            double val = (candidate < v ? candidate : v);
            s += val;
        }
        return s / N - m_val;
    };
 
    double low_m = 0.0, high_m = 1.0;
    while(f(high_m) > 0) {
        high_m *= 2.0;
        if(high_m > 1e15) break;
    }
    for (int iter = 0; iter < 100; iter++){
        double mid = (low_m + high_m) / 2.0;
        double val = f(mid);
        if(val > 1e-12) low_m = mid;
        else           high_m = mid;
    }
    double m_star = (low_m + high_m) / 2.0;
 
    double V_start = Vdist[start_r][start_c];
    double U_start = Udist[start_r][start_c];
    double T_start = min(V_start, U_start + m_star);
    cout << fixed << setprecision(12) << T_start << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 13148
final_rank: 3

code_snippet: // AOJ #2336
// Spring Tiles 2025.2.10

#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
 
    int W, H;
    cin >> W >> H;
    vector<string> grid(H);
    for (int i = 0; i < H; i++) cin >> grid[i];
 
    vector<vector<bool>> isFloor(H, vector<bool>(W, false));
    vector<vector<bool>> hasSpringOption(H, vector<bool>(W, false));
    int start_r = -1, start_c = -1;
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            char ch = grid[r][c];
            if(ch == '.' || ch == 's'){
                isFloor[r][c] = true;
                if(ch == 's') start_r = r, start_c = c;
            }
        }
    }
    int dr[4] = {1,-1,0,0}, dc[4] = {0,0,1,-1};
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            if(isFloor[r][c]){
                for (int d = 0; d < 4; d++){
                    int nr = r + dr[d], nc = c + dc[d];
                    if(nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
                    if(grid[nr][nc] == '*'){
                        hasSpringOption[r][c] = true;
                        break;
                    }
                }
            }
        }
    }
 
    vector<vector<int>> F(H, vector<int>(W, INF));
    deque<pair<int,int>> dq;
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            if(isFloor[r][c]){
                bool adjExit = false;
                for (int d = 0; d < 4; d++){
                    int nr = r + dr[d], nc = c + dc[d];
                    if(nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
                    if(grid[nr][nc] == 'g'){
                        adjExit = true;
                        break;
                    }
                }
                if(adjExit){
                    F[r][c] = 1;
                    dq.push_back({r,c});
                }
            }
        }
    }
    while(!dq.empty()){
        auto [r, c] = dq.front(); dq.pop_front();
        int d = F[r][c];
        for (int i = 0; i < 4; i++){
            int nr = r + dr[i], nc = c + dc[i];
            if(nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
            if(!isFloor[nr][nc]) continue;
            if(F[nr][nc] > d + 1){
                F[nr][nc] = d + 1;
                dq.push_back({nr,nc});
            }
        }
    }
 
    vector<vector<int>> S(H, vector<int>(W, INF));
    deque<pair<int,int>> dq2;
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            if(isFloor[r][c] && hasSpringOption[r][c]){
                S[r][c] = 0;
                dq2.push_back({r,c});
            }
        }
    }
    while(!dq2.empty()){
        auto [r, c] = dq2.front(); dq2.pop_front();
        int d = S[r][c];
        for (int i = 0; i < 4; i++){
            int nr = r + dr[i], nc = c + dc[i];
            if(nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
            if(!isFloor[nr][nc]) continue;
            if(S[nr][nc] > d + 1){
                S[nr][nc] = d + 1;
                dq2.push_back({nr,nc});
            }
        }
    }
 
    vector<pair<double,double>> cells;
    for (int r = 0; r < H; r++){
        for (int c = 0; c < W; c++){
            if(isFloor[r][c]){
                cells.push_back({(double)F[r][c], (double)S[r][c]});
            }
        }
    }
    int N = cells.size();
 
    auto g = [&](double X) -> double {
        double tot = 0.0;
        for (int i = 0; i < N; i++){
            double Fi = cells[i].first, Si = cells[i].second;
            if(Si + X <= Fi) tot += Si + X;
            else             tot += Fi;
        }
        return tot - N * (X - 1.0);
    };
 
    double lowX = 1.0, highX = 1.0;
    while(g(highX) > 0) {
        highX *= 2.0;
        if(highX > 1e9) break;
    }
 
    for (int iter = 0; iter < 100; iter++){
        double mid = (lowX + highX) / 2.0;
        double val = g(mid);
        if(val > 1e-12) lowX = mid;
        else           highX = mid;
    }
    double Xstar = (lowX + highX) / 2.0;
 
    double F_start = (double) F[start_r][start_c];
    double S_start = (double) S[start_r][start_c];
    double ans = min(F_start, S_start + Xstar);
    cout << fixed << setprecision(12) << ans << endl;
    return 0;
}
accuracy: 0.55
time_ms: 20
memory_kb: 7408
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const ll inf = 0x3fffffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};

int main() {
    int N, M;
    cin >> M >> N;
    vector<string> S(N);
    rep(i,0,N) cin >> S[i];
    int SX, SY;
    rep(i,0,N) rep(j,0,M) if (S[i][j] == 's') SX = i, SY = j, S[i][j] = '.';
    vector<vector<ll>> DG(N,vector<ll>(M,(ll)inf));
    vector<vector<ll>> DS(N,vector<ll>(M,(ll)inf));
    queue<pair<int,int>> Q;
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == 'g') DG[i][j] = 0, Q.push({i,j});
        }
    }
    while(!Q.empty()) {
        pair<int,int> P = Q.front();
        Q.pop();
        int X = P.first, Y = P.second;
        rep(i,0,4) {
            int NX = X + dx[i], NY = Y + dy[i];
            if (0 <= NX && NX < N && 0 <= NY && NY < M) {
                if (S[NX][NY] == '.' && DG[NX][NY] == inf) {
                    DG[NX][NY] = DG[X][Y] + 1;
                    Q.push({NX,NY});
                }
            }
        }
    }
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == '*') DS[i][j] = 0, Q.push({i,j});
        }
    }
    while(!Q.empty()) {
        pair<int,int> P = Q.front();
        Q.pop();
        int X = P.first, Y = P.second;
        rep(i,0,4) {
            int NX = X + dx[i], NY = Y + dy[i];
            if (0 <= NX && NX < N && 0 <= NY && NY < M) {
                if (S[NX][NY] == '.' && DS[NX][NY] == inf) {
                    DS[NX][NY] = DS[X][Y] + 1;
                    Q.push({NX,NY});
                }
            }
        }
    }
    vector<pair<ll,ll>> VP;
    vector<ll> VX;
    int L = 0;
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == '.') {
                VP.push_back({DG[i][j], DS[i][j]});
                VX.push_back(DG[i][j] - DS[i][j]);
                L++;
            }
        }
    }
    ll COUNT = 0;
    rep(i,0,L) COUNT += VP[i].second;
    sort(ALL(VX));
    VX.push_back(inf);
    long double ANS = -1;
    rep(i,1,L+1) {
        COUNT += VX[i-1];
        if (VX[i-1] * (ll)i <= COUNT && COUNT < VX[i] * (ll)i) {
            ANS = (long double)COUNT / (long double)i;
            break;
        }
    }
    print(min((long double)DG[SX][SY], (long double)DS[SX][SY] + ANS));
}
accuracy: 1
time_ms: 10
memory_kb: 15328
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};

int main() {
    int N, M;
    cin >> M >> N;
    vector<string> S(N);
    rep(i,0,N) cin >> S[i];
    int SX, SY;
    rep(i,0,N) rep(j,0,M) if (S[i][j] == 's') SX = i, SY = j, S[i][j] = '.';
    vector<vector<ll>> DG(N,vector<ll>(M,(ll)inf));
    vector<vector<ll>> DS(N,vector<ll>(M,(ll)inf));
    queue<pair<int,int>> Q;
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == 'g') DG[i][j] = 0, Q.push({i,j});
        }
    }
    while(!Q.empty()) {
        pair<int,int> P = Q.front();
        Q.pop();
        int X = P.first, Y = P.second;
        rep(i,0,4) {
            int NX = X + dx[i], NY = Y + dy[i];
            if (0 <= NX && NX < N && 0 <= NY && NY < M) {
                if (S[NX][NY] == '.' && DG[NX][NY] == inf) {
                    DG[NX][NY] = DG[X][Y] + 1;
                    Q.push({NX,NY});
                }
            }
        }
    }
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == '*') DS[i][j] = 0, Q.push({i,j});
        }
    }
    while(!Q.empty()) {
        pair<int,int> P = Q.front();
        Q.pop();
        int X = P.first, Y = P.second;
        rep(i,0,4) {
            int NX = X + dx[i], NY = Y + dy[i];
            if (0 <= NX && NX < N && 0 <= NY && NY < M) {
                if (S[NX][NY] == '.' && DS[NX][NY] == inf) {
                    DS[NX][NY] = DS[X][Y] + 1;
                    Q.push({NX,NY});
                }
            }
        }
    }
    vector<pair<ll,ll>> VP;
    vector<ll> VX;
    int L = 0;
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == '.') {
                VP.push_back({DG[i][j], DS[i][j]});
                VX.push_back(DG[i][j] - DS[i][j]);
                L++;
            }
        }
    }
    ll COUNT = 0;
    rep(i,0,L) COUNT += VP[i].second;
    sort(ALL(VX));
    VX.push_back(inf);
    long double ANS = -1;
    rep(i,1,L+1) {
        COUNT += VX[i-1];
        if (VX[i-1] * (ll)i <= COUNT && COUNT < VX[i] * (ll)i) {
            ANS = (long double)COUNT / (long double)i;
            break;
        }
    }
    print(min((long double)DG[SX][SY], (long double)DS[SX][SY] + ANS));
}
accuracy: 0.55
time_ms: 10
memory_kb: 11508
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};

int main() {
    int N, M;
    cin >> M >> N;
    vector<string> S(N);
    rep(i,0,N) cin >> S[i];
    int SX, SY;
    rep(i,0,N) rep(j,0,M) if (S[i][j] == 's') SX = i, SY = j, S[i][j] = '.';
    vector<vector<ll>> DG(N,vector<ll>(M,(ll)inf));
    vector<vector<ll>> DS(N,vector<ll>(M,(ll)inf));
    queue<pair<int,int>> Q;
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == 'g') DG[i][j] = 0, Q.push({i,j});
        }
    }
    while(!Q.empty()) {
        pair<int,int> P = Q.front();
        Q.pop();
        int X = P.first, Y = P.second;
        rep(i,0,4) {
            int NX = X + dx[i], NY = Y + dy[i];
            if (0 <= NX && NX < N && 0 <= NY && NY < M) {
                if (S[NX][NY] == '.' && DG[NX][NY] == inf) {
                    DG[NX][NY] = DG[X][Y] + 1;
                    Q.push({NX,NY});
                }
            }
        }
    }
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == '*') DS[i][j] = 0, Q.push({i,j});
        }
    }
    while(!Q.empty()) {
        pair<int,int> P = Q.front();
        Q.pop();
        int X = P.first, Y = P.second;
        rep(i,0,4) {
            int NX = X + dx[i], NY = Y + dy[i];
            if (0 <= NX && NX < N && 0 <= NY && NY < M) {
                if (S[NX][NY] == '.' && DS[NX][NY] == inf) {
                    DS[NX][NY] = DS[X][Y] + 1;
                    Q.push({NX,NY});
                }
            }
        }
    }
    vector<pair<ll,ll>> VP;
    vector<ll> VX;
    int L = 0;
    rep(i,0,N) {
        rep(j,0,M) {
            if (S[i][j] == '.') {
                VP.push_back({DG[i][j], DS[i][j]});
                VX.push_back(DG[i][j] - DS[i][j]);
                L++;
            }
        }
    }
    ll COUNT = 0;
    rep(i,0,L) COUNT += VP[i].second;
    sort(ALL(VX));
    VX.push_back(inf);
    long double ANS = -1;
    rep(i,1,L+1) {
        COUNT += VX[i-1];
        if (VX[i-1] * i <= COUNT && COUNT < VX[i] * i) {
            ANS = (double)COUNT / (double)i;
            break;
        }
    }
    print(min((long double)DG[SX][SY], (long double)DS[SX][SY] + ANS));
}
accuracy: 0.55
time_ms: 10
memory_kb: 11504
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)



int main(){
    ll H,W;
    cin>>W>>H;
    vector<string> S(H);
    queue<pair<ll,ll>> Q;
    ll SX,SY,GX,GY;
    vector<vll> D(H,vll(W,2e18)),E(H,vll(W,2e18));
    ll cnt=1;
    rep(h,H){
        cin>>S[h];
        rep(w,W){
            if(S[h][w]=='s'){
                SY=h;
                SX=w;
            }
            else if(S[h][w]=='g'){
                GY=h;
                GX=w;
            }
            else if(S[h][w]=='*'){
                Q.push({h,w});
                E[h][w]=0;
            }
            else if(S[h][w]=='.')cnt++;
        }
    }
    vll dx={1,0,-1,0};
    vll dy={0,1,0,-1};
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(E[ny][nx]>E[y][x]+1){
                E[ny][nx]=E[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    cout<<fixed<<setprecision(15);
    Q.push({GY,GX});
    D[GY][GX]=0;
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(D[ny][nx]>D[y][x]+1){
                D[ny][nx]=D[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    long double L=0.0,R=3e10;
    rep(j,1){
        // cout<<L<<" "<<R<<endl;
        rep(i,100){
            long double M=(R+L)/2.0;
            long double res=0.0;
            rep(h,H)rep(w,W){
                if(S[h][w]=='.'||S[h][w]=='s'){
                    res+=min((long double)(E[h][w])+M,(long double)(D[h][w]))/(long double)(cnt);
                    if(res>M+1e17)res=1e17;
                }
            }
            // cout<<res<<endl;

            if(res<=M+1e-10){
                R=M;
            }
            else{
                L=M;
            }
        }
        long double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min((long double)(E[h][w])+L,(long double)(D[h][w]))/(long double)(cnt);
                if(res>1e17)res=1e17;
            }
        }
        R=res;
        
    }
    

    long double an=min((long double)(E[SY][SX])+L,(long double)(D[SY][SX]));
    cout<<fixed<<setprecision(15)<<an<<endl;
}
accuracy: 1
time_ms: 80
memory_kb: 8208
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)



int main(){
    ll H,W;
    cin>>W>>H;
    vector<string> S(H);
    queue<pair<ll,ll>> Q;
    ll SX,SY,GX,GY;
    vector<vll> D(H,vll(W,2e18)),E(H,vll(W,2e18));
    ll cnt=1;
    rep(h,H){
        cin>>S[h];
        rep(w,W){
            if(S[h][w]=='s'){
                SY=h;
                SX=w;
            }
            else if(S[h][w]=='g'){
                GY=h;
                GX=w;
            }
            else if(S[h][w]=='*'){
                Q.push({h,w});
                E[h][w]=0;
            }
            else if(S[h][w]=='.')cnt++;
        }
    }
    vll dx={1,0,-1,0};
    vll dy={0,1,0,-1};
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(E[ny][nx]>E[y][x]+1){
                E[ny][nx]=E[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    cout<<fixed<<setprecision(15);
    Q.push({GY,GX});
    D[GY][GX]=0;
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(D[ny][nx]>D[y][x]+1){
                D[ny][nx]=D[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    long double L=0.0,R=3e9;
    rep(j,1){
        // cout<<L<<" "<<R<<endl;
        rep(i,100){
            long double M=(R+L)/2.0;
            long double res=0.0;
            rep(h,H)rep(w,W){
                if(S[h][w]=='.'||S[h][w]=='s'){
                    res+=min((long double)(E[h][w])+M,(long double)(D[h][w]))/(long double)(cnt);
                    if(res>M+1e17)res=1e17;
                }
            }
            // cout<<res<<endl;

            if(res<=M+1e-10){
                R=M;
            }
            else{
                L=M;
            }
        }
        long double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min((long double)(E[h][w])+L,(long double)(D[h][w]))/(long double)(cnt);
                if(res>1e17)res=1e17;
            }
        }
        R=res;
        
    }
    

    long double an=min((long double)(E[SY][SX])+L,(long double)(D[SY][SX]));
    cout<<fixed<<setprecision(15)<<an<<endl;
}
accuracy: 0.55
time_ms: 40
memory_kb: 7908
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)



int main(){
    ll H,W;
    cin>>W>>H;
    vector<string> S(H);
    queue<pair<ll,ll>> Q;
    ll SX,SY,GX,GY;
    vector<vll> D(H,vll(W,2e18)),E(H,vll(W,2e18));
    ll cnt=1;
    rep(h,H){
        cin>>S[h];
        rep(w,W){
            if(S[h][w]=='s'){
                SY=h;
                SX=w;
            }
            else if(S[h][w]=='g'){
                GY=h;
                GX=w;
            }
            else if(S[h][w]=='*'){
                Q.push({h,w});
                E[h][w]=0;
            }
            else if(S[h][w]=='.')cnt++;
        }
    }
    vll dx={1,0,-1,0};
    vll dy={0,1,0,-1};
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(E[ny][nx]>E[y][x]+1){
                E[ny][nx]=E[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    cout<<fixed<<setprecision(15);
    Q.push({GY,GX});
    D[GY][GX]=0;
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(D[ny][nx]>D[y][x]+1){
                D[ny][nx]=D[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    double L=0.0,R=3e11;
    rep(j,1){
        // cout<<L<<" "<<R<<endl;
        rep(i,150){
            double M=(R+L)/2.0;
            double res=0.0;
            rep(h,H)rep(w,W){
                if(S[h][w]=='.'||S[h][w]=='s'){
                    res+=min(double(E[h][w])+M,double(D[h][w]))/double(cnt);
                    if(res>M+1e17)res=1e17;
                }
            }
            // cout<<res<<endl;

            if(res<=M+1e-10){
                R=M;
            }
            else{
                L=M;
            }
        }
        double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min(double(E[h][w])+L,double(D[h][w]))/double(cnt);
                if(res>1e17)res=1e17;
            }
        }
        R=res;
        
    }
    

    double an=min(double(E[SY][SX])+L,double(D[SY][SX]));
    cout<<fixed<<setprecision(15)<<an<<endl;
}
accuracy: 0.6
time_ms: 90
memory_kb: 8016
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)



int main(){
    ll H,W;
    cin>>W>>H;
    vector<string> S(H);
    queue<pair<ll,ll>> Q;
    ll SX,SY,GX,GY;
    vector<vll> D(H,vll(W,2e18)),E(H,vll(W,2e18));
    ll cnt=1;
    rep(h,H){
        cin>>S[h];
        rep(w,W){
            if(S[h][w]=='s'){
                SY=h;
                SX=w;
            }
            else if(S[h][w]=='g'){
                GY=h;
                GX=w;
            }
            else if(S[h][w]=='*'){
                Q.push({h,w});
                E[h][w]=0;
            }
            else if(S[h][w]=='.')cnt++;
        }
    }
    vll dx={1,0,-1,0};
    vll dy={0,1,0,-1};
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(E[ny][nx]>E[y][x]+1){
                E[ny][nx]=E[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    cout<<fixed<<setprecision(15);
    Q.push({GY,GX});
    D[GY][GX]=0;
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(D[ny][nx]>D[y][x]+1){
                D[ny][nx]=D[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    double L=0.0,R=3e10;
    rep(i,150){
        double M=(R+L)/2.0;
        double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min(double(E[h][w])+M,double(D[h][w]))/double(cnt);
                if(res>M)res=M+10.0;
            }
        }
        // cout<<res<<endl;

        if(res<=M){
            R=M;
        }
        else{
            L=M;
        }
    }
    double res=0.0;
    rep(h,H)rep(w,W){
        if(S[h][w]=='.'||S[h][w]=='s'){
            res+=min(double(E[h][w])+L,double(D[h][w]))/double(cnt);
            if(res>L)res=L+10.0;
        }
    }
    R=res;
    rep(i,150){
        double M=(R+L)/2.0;
        double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min(double(E[h][w])+M,double(D[h][w]))/double(cnt);
                if(res>M)res=M+10.0;
            }
        }
        // cout<<res<<endl;

        if(res<=M){
            R=M;
        }
        else{
            L=M;
        }
    }


    double an=min(double(E[SY][SX])+L,double(D[SY][SX]));
    cout<<fixed<<setprecision(15)<<an<<endl;
}
accuracy: 0.6
time_ms: 100
memory_kb: 8012
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)



int main(){
    ll H,W;
    cin>>W>>H;
    vector<string> S(H);
    queue<pair<ll,ll>> Q;
    ll SX,SY,GX,GY;
    vector<vll> D(H,vll(W,2e18)),E(H,vll(W,2e18));
    ll cnt=1;
    rep(h,H){
        cin>>S[h];
        rep(w,W){
            if(S[h][w]=='s'){
                SY=h;
                SX=w;
            }
            else if(S[h][w]=='g'){
                GY=h;
                GX=w;
            }
            else if(S[h][w]=='*'){
                Q.push({h,w});
                E[h][w]=0;
            }
            else if(S[h][w]=='.')cnt++;
        }
    }
    vll dx={1,0,-1,0};
    vll dy={0,1,0,-1};
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(E[ny][nx]>E[y][x]+1){
                E[ny][nx]=E[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    Q.push({GY,GX});
    D[GY][GX]=0;
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(D[ny][nx]>D[y][x]+1){
                D[ny][nx]=D[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    double L=0.0,R=3e10;
    rep(i,150){
        double M=(R+L)/2.0;
        double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min(double(E[h][w])+M,double(D[h][w]))/double(cnt);
                if(res>3e18)res=3e18;
            }
        }
        // cout<<res<<endl;

        if(res<=M){
            R=M;
        }
        else{
            L=M;
        }
    }
    double an=min(double(E[SY][SX])+R,double(D[SY][SX]));
    cout<<fixed<<setprecision(15)<<an<<endl;
}
accuracy: 0.6
time_ms: 70
memory_kb: 8020
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)



int main(){
    ll H,W;
    cin>>W>>H;
    vector<string> S(H);
    queue<pair<ll,ll>> Q;
    ll SX,SY,GX,GY;
    vector<vll> D(H,vll(W,1e18)),E(H,vll(W,1e18));
    ll cnt=1;
    rep(h,H){
        cin>>S[h];
        rep(w,W){
            if(S[h][w]=='s'){
                SY=h;
                SX=w;
            }
            else if(S[h][w]=='g'){
                GY=h;
                GX=w;
            }
            else if(S[h][w]=='*'){
                Q.push({h,w});
                E[h][w]=0;
            }
            else if(S[h][w]=='.')cnt++;
        }
    }
    vll dx={1,0,-1,0};
    vll dy={0,1,0,-1};
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(E[ny][nx]>E[y][x]+1){
                E[ny][nx]=E[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    Q.push({GY,GX});
    D[GY][GX]=0;
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(D[ny][nx]>D[y][x]+1){
                D[ny][nx]=D[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    double L=0.0,R=1e18;
    rep(i,120){
        double M=(R+L)/2.0;
        double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min(double(E[h][w])+M,double(D[h][w]));
            }
        }
        if(res/double(cnt)<=M){
            R=M;
        }
        else{
            L=M;
        }
    }
    double an=min(double(E[SY][SX])+L,double(D[SY][SX]));
    cout<<fixed<<setprecision(15)<<an<<endl;
}
accuracy: 0.55
time_ms: 20
memory_kb: 7960
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
using vll=vector<ll>;
#define rep(i,n) for(ll i=0;i<ll(n);i++)



int main(){
    ll H,W;
    cin>>W>>H;
    vector<string> S(H);
    queue<pair<ll,ll>> Q;
    ll SX,SY,GX,GY;
    vector<vll> D(H,vll(W,1e18)),E(H,vll(W,1e18));
    ll cnt=1;
    rep(h,H){
        cin>>S[h];
        rep(w,W){
            if(S[h][w]=='s'){
                SY=h;
                SX=w;
            }
            else if(S[h][w]=='g'){
                GY=h;
                GX=w;
            }
            else if(S[h][w]=='*'){
                Q.push({h,w});
                E[h][w]=0;
            }
            else if(S[h][w]=='.')cnt++;
        }
    }
    vll dx={1,0,-1,0};
    vll dy={0,1,0,-1};
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(E[ny][nx]>E[y][x]+1){
                E[ny][nx]=E[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    Q.push({GY,GX});
    D[GY][GX]=0;
    while(!Q.empty()){
        ll y=Q.front().first;
        ll x=Q.front().second;
        Q.pop();
        rep(d,4){
            ll ny=y+dy[d];
            ll nx=x+dx[d];
            if(S[ny][nx]=='#')continue;
            if(S[ny][nx]=='*')continue;
            if(D[ny][nx]>D[y][x]+1){
                D[ny][nx]=D[y][x]+1;
                Q.push({ny,nx});
            }
        }
    }
    double L=0.0,R=1e9;
    rep(i,60){
        double M=(R+L)/2.0;
        double res=0.0;
        rep(h,H)rep(w,W){
            if(S[h][w]=='.'||S[h][w]=='s'){
                res+=min(double(E[h][w])+M,double(D[h][w]));
            }
        }
        if(res/double(cnt)<=M){
            R=M;
        }
        else{
            L=M;
        }
    }
    double an=min(double(E[SY][SX])+L,double(D[SY][SX]));
    cout<<fixed<<setprecision(15)<<an<<endl;
}
accuracy: 0.55
time_ms: 10
memory_kb: 7960
final_rank: 15

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

using f64 = long double;

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

vector<vector<int>> bfs(int H, int W, const vector<string>& S, vector<pair<int,int>> ss) {
    vector dist(H, vector(W, -1));
    queue<pair<int,int>> q;
    for(auto [x, y] : ss) {
        dist[x][y] = 0;
        q.push({x, y});
    }
    while(!q.empty()) {
        auto [x, y] = q.front(); q.pop();
        for(auto [dx, dy] : dir4) {
            int nx = x + dx, ny = y + dy;
            if(0 <= nx and nx < H and 0 <= ny and ny < W) {
                if(S[nx][ny] == '.' and dist[nx][ny] == -1) {
                    dist[nx][ny] = dist[x][y] + 1;
                    q.push({nx, ny});
                }
            }
        }
    }
    return dist;
}

int main() {
    int W = in(), H = in();
    vector<string> S = in(H);

    int si = -1, sj = -1, gi = -1, gj = -1;
    for(int i : rep(H)) for(int j : rep(W)) {
        if(S[i][j] == 's') tie(si, sj) = make_pair(i, j), S[i][j] = '.';
        if(S[i][j] == 'g') tie(gi, gj) = make_pair(i, j), S[i][j] = '.';
    }

    vector<vector<int>> dist_g = bfs(H, W, S, {{gi, gj}});
    vector<pair<int,int>> ws;
    for(int i : rep(H)) for(int j : rep(W)) if(S[i][j] == '*') {
        ws.push_back({i, j});
        S[i][j] = '.';
    }
    vector<vector<int>> dist_w = bfs(H, W, S, ws);
    for(auto [i, j] : ws) S[i][j] = '*';

    f64 t = bin_search_real<f64>(0.0, 4e18, [&](f64 x) {
        f64 sum = 0;
        for(int i : rep(H)) for(int j : rep(W)) if(S[i][j] == '.' and make_pair(i, j) != make_pair(gi, gj)) {
            f64 now = 8e18;
            if(dist_g[i][j] != -1) chmin(now, (f64)dist_g[i][j] - x);
            if(dist_w[i][j] != -1) chmin(now, (f64)dist_w[i][j]);
            if(now == 8e18) return false;
            sum += now;
        }
        return sum >= 0;
    }, 120);

    f64 ans = 8e18;
    if(dist_g[si][sj] != -1) chmin(ans, (f64)dist_g[si][sj]);
    if(dist_w[si][sj] != -1) chmin(ans, (f64)dist_w[si][sj] + t);
    printer::precision(20);
    print(ans);
}
accuracy: 1
time_ms: 90
memory_kb: 5992
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

template<class T>
bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}

using ll = long long;
using ld = long double;

bool is_end = false;
const ld INF = 1e18;

ll W, H;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
using tup = tuple<ld, int, int>;

bool isvalid(int x, int y)
{
	return 0 <= x && x < H && 0 <= y && y < W;
}

void fill_dist(ld mid, const vector<vector<ll>> &B, vector<vector<ld>> &dist)
{
	priority_queue<tup, vector<tup>, greater<tup>> pq;
	for (int i = 0; i < H; ++i)
	{
		for (int j = 0; j < W; ++j)
		{
			if (B[i][j] == 2) {dist[i][j] = 0.0; pq.emplace(dist[i][j], i, j);}
			else if (B[i][j] == 1) {dist[i][j] = mid; pq.emplace(dist[i][j], i, j);}
			else {dist[i][j] = INF;}
		}
	}
	
	while (!pq.empty())
	{
		auto [d, x, y] = pq.top();
		pq.pop();
		
		if (dist[x][y] < d) {continue;}
		
		for (int id = 0; id < 4; ++id)
		{
			int nx = x + dx[id], ny = y + dy[id];
			if (isvalid(nx, ny))
			{
				if (B[nx][ny] == 0 && chmin(dist[nx][ny], dist[x][y] + 1.0))
				{
					pq.emplace(dist[nx][ny], nx, ny);
				}
			}
		}
	}
	
	return;
}

void calc()
{
	cin >> W >> H;
	vector<vector<ll>> B(H, vector<ll>(W, 0));
	// 0 -> none, 1 -> jump, 2 -> goal, 3 -> wall
	ll sx, sy, gx, gy;
	for (int i = 0; i < H; ++i)
	{
		string S; cin >> S;
		for (int j = 0; j < W; ++j)
		{
			char c = S[j];
			if (c == '#') {B[i][j] = 3;}
			if (c == '*') {B[i][j] = 1;}
			
			if (c == 's') {sx = i, sy = j;}
			if (c == 'g') {B[i][j] = 2; gx = i, gy = j;}
		}
	}
	
	vector<vector<ld>> dist(H, vector<ld>(W, INF));
	ld ok = 0, ng = INF;
	while (abs(ok - ng) > 1e-9)
	{
		ld mid = (ok + ng) / 2;
		fill_dist(mid, B, dist);
		
		ld ave = 0.0;
		int cnt = 0;
		for (int i = 0; i < H; ++i)
		{
			for (int j = 0; j < W; ++j)
			{
				if (B[i][j] == 0 && dist[i][j] < INF) {ave += dist[i][j], cnt += 1;}
			}
		}
		ave /= cnt;
		
		if (mid < ave) {ok = mid;}
		else {ng = mid;}
		
		// cout << mid << " " << ave << endl;
	}
	
	cout << fixed << setprecision(10) << dist[sx][sy] << endl;
	
	return;	
}

int main()
{
	calc();
	
	return 0;
}
accuracy: 1
time_ms: 2810
memory_kb: 10392
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

template<class T>
bool chmin(T &a, const T &b) {if (a > b) {a = b; return 1;} return 0;}

using ll = long long;
using ld = long double;

bool is_end = false;
const ld INF = 1e9;

ll W, H;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};
using tup = tuple<ld, int, int>;

bool isvalid(int x, int y)
{
	return 0 <= x && x < H && 0 <= y && y < W;
}

void fill_dist(ld mid, const vector<vector<ll>> &B, vector<vector<ld>> &dist)
{
	priority_queue<tup, vector<tup>, greater<tup>> pq;
	for (int i = 0; i < H; ++i)
	{
		for (int j = 0; j < W; ++j)
		{
			if (B[i][j] == 2) {dist[i][j] = 0.0; pq.emplace(dist[i][j], i, j);}
			else if (B[i][j] == 1) {dist[i][j] = mid; pq.emplace(dist[i][j], i, j);}
			else {dist[i][j] = INF;}
		}
	}
	
	while (!pq.empty())
	{
		auto [d, x, y] = pq.top();
		pq.pop();
		
		if (dist[x][y] < d) {continue;}
		
		for (int id = 0; id < 4; ++id)
		{
			int nx = x + dx[id], ny = y + dy[id];
			if (isvalid(nx, ny))
			{
				if (B[nx][ny] == 0 && chmin(dist[nx][ny], dist[x][y] + 1.0))
				{
					pq.emplace(dist[nx][ny], nx, ny);
				}
			}
		}
	}
	
	return;
}

void calc()
{
	cin >> W >> H;
	vector<vector<ll>> B(H, vector<ll>(W, 0));
	// 0 -> none, 1 -> jump, 2 -> goal, 3 -> wall
	ll sx, sy, gx, gy;
	for (int i = 0; i < H; ++i)
	{
		string S; cin >> S;
		for (int j = 0; j < W; ++j)
		{
			char c = S[j];
			if (c == '#') {B[i][j] = 3;}
			if (c == '*') {B[i][j] = 1;}
			
			if (c == 's') {sx = i, sy = j;}
			if (c == 'g') {B[i][j] = 2; gx = i, gy = j;}
		}
	}
	
	vector<vector<ld>> dist(H, vector<ld>(W, INF));
	ld ok = 0, ng = INF;
	while (abs(ok - ng) > 1e-9)
	{
		ld mid = (ok + ng) / 2;
		fill_dist(mid, B, dist);
		
		ld ave = 0.0;
		int cnt = 0;
		for (int i = 0; i < H; ++i)
		{
			for (int j = 0; j < W; ++j)
			{
				if (B[i][j] == 0) {ave += dist[i][j], cnt += 1;}
			}
		}
		ave /= cnt;
		
		if (mid < ave) {ok = mid;}
		else {ng = mid;}
	}
	
	cout << fixed << setprecision(10) << dist[sx][sy] << endl;
	
	return;	
}

int main()
{
	calc();
	
	return 0;
}
accuracy: 0.55
time_ms: 70
memory_kb: 8880
final_rank: 18

code_snippet: #include <iostream>
#include <queue>
#include <functional>
using namespace std;

int H, W, sx, sy, gx, gy;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
char c[509][509];
long double dist[509][509];
bool used[509][509];

// 定数倍間に合うかな…
bool check(long double tm) {
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) dist[i][j] = 1.0e16;
		for (int j = 0; j < W; j++) used[i][j] = false;
	}

	// Init Dijkstra
	priority_queue<tuple<long double, int, int>, vector<tuple<long double, int, int>>, greater<tuple<long double, int, int>>> Q;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (c[i][j] != '*') continue;
			Q.push(make_tuple(tm, i, j));
			dist[i][j] = tm;
		}
	}
	Q.push(make_tuple(0.0L, gx, gy));
	dist[gx][gy] = 0.0L;
	
	// DIjkstra Start
	while (!Q.empty()) {
		int px = get<1>(Q.top());
		int py = get<2>(Q.top());
		Q.pop();
		if (used[px][py] == true) continue;
		used[px][py] = true;
		for (int i = 0; i < 4; i++) {
			int cx = px + dx[i];
			int cy = py + dy[i];
			if (c[cx][cy] == '#' || c[cx][cy] == '*') continue;
			if (dist[cx][cy] > dist[px][py] + 1.0L) {
				dist[cx][cy] = dist[px][py] + 1.0L;
				Q.push(make_tuple(dist[cx][cy], cx, cy));
			}
		}
	}

	// Compare
	long double avg = 0.0L, cnt = 0.0L;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (c[i][j] == '#' || c[i][j] == '*') continue;
			if (i == gx && j == gy) continue;
			avg += dist[i][j];
			cnt += 1.0L;
		}
	}
	avg /= cnt;
	if (avg > tm) return true;
	return false;
}

int main() {
	// Step 1. Input
	cin >> W >> H;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> c[i][j];
			if (c[i][j] == 's') { sx = i; sy = j; c[i][j] = '.'; }
			if (c[i][j] == 'g') { gx = i; gy = j; c[i][j] = '.'; }
		}
	}

	// Step 2. Binary Search
	long double cl = 0.0L, cr = 1.0e15, cm;
	for (int i = 0; i < 120; i++) {
		cm = (cl + cr) / 2.0L;
		bool ret = check(cm);
		if (ret == true) cl = cm;
		else cr = cm;
	}

	// Step 3. Output
	check(cm);
	printf("%.12Lf\n", dist[sx][sy]);
	return 0;
}
accuracy: 1
time_ms: 3750
memory_kb: 10756
final_rank: 8

code_snippet: #include <iostream>
#include <queue>
#include <functional>
using namespace std;

int H, W, sx, sy, gx, gy;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
char c[509][509];
double dist[509][509];
bool used[509][509];

// 定数倍間に合うかな…
bool check(double tm) {
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) dist[i][j] = 1e16;
		for (int j = 0; j < W; j++) used[i][j] = false;
	}

	// Init Dijkstra
	priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> Q;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (c[i][j] != '*') continue;
			Q.push(make_tuple(tm, i, j));
			dist[i][j] = tm;
		}
	}
	Q.push(make_tuple(0, gx, gy));
	dist[gx][gy] = 0;
	
	// DIjkstra Start
	while (!Q.empty()) {
		int px = get<1>(Q.top());
		int py = get<2>(Q.top());
		Q.pop();
		if (used[px][py] == true) continue;
		used[px][py] = true;
		for (int i = 0; i < 4; i++) {
			int cx = px + dx[i];
			int cy = py + dy[i];
			if (c[cx][cy] == '#' || c[cx][cy] == '*') continue;
			if (dist[cx][cy] > dist[px][py] + 1.0) {
				dist[cx][cy] = dist[px][py] + 1.0;
				Q.push(make_tuple(dist[cx][cy], cx, cy));
			}
		}
	}

	// Compare
	double avg = 0.0, cnt = 0.0;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (c[i][j] == '#' || c[i][j] == '*') continue;
			if (i == gx && j == gy) continue;
			avg += dist[i][j];
			cnt += 1.0;
		}
	}
	avg /= cnt;
	if (avg > tm) return true;
	return false;
}

int main() {
	// Step 1. Input
	cin >> W >> H;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> c[i][j];
			if (c[i][j] == 's') { sx = i; sy = j; c[i][j] = '.'; }
			if (c[i][j] == 'g') { gx = i; gy = j; c[i][j] = '.'; }
		}
	}

	// Step 2. Binary Search
	double cl = 0, cr = 1e15, cm;
	for (int i = 0; i < 100; i++) {
		cm = (cl + cr) / 2.0;
		bool ret = check(cm);
		if (ret == true) cl = cm;
		else cr = cm;
	}

	// Step 3. Output
	check(cm);
	printf("%.12lf\n", dist[sx][sy]);
	return 0;
}
accuracy: 0.55
time_ms: 170
memory_kb: 6032
final_rank: 13

code_snippet: #include <iostream>
#include <queue>
#include <functional>
using namespace std;

int H, W, sx, sy, gx, gy;
int dx[4] = { 0, 1, 0, -1 };
int dy[4] = { 1, 0, -1, 0 };
char c[509][509];
double dist[509][509];
bool used[509][509];

// 定数倍間に合うかな…
bool check(double tm) {
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) dist[i][j] = 1e9;
		for (int j = 0; j < W; j++) used[i][j] = false;
	}

	// Init Dijkstra
	priority_queue<tuple<double, int, int>, vector<tuple<double, int, int>>, greater<tuple<double, int, int>>> Q;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (c[i][j] != '*') continue;
			Q.push(make_tuple(tm, i, j));
			dist[i][j] = tm;
		}
	}
	Q.push(make_tuple(0, gx, gy));
	dist[gx][gy] = 0;
	
	// DIjkstra Start
	while (!Q.empty()) {
		int px = get<1>(Q.top());
		int py = get<2>(Q.top());
		Q.pop();
		if (used[px][py] == true) continue;
		used[px][py] = true;
		for (int i = 0; i < 4; i++) {
			int cx = px + dx[i];
			int cy = py + dy[i];
			if (c[cx][cy] == '#' || c[cx][cy] == '*') continue;
			if (dist[cx][cy] > dist[px][py] + 1.0) {
				dist[cx][cy] = dist[px][py] + 1.0;
				Q.push(make_tuple(dist[cx][cy], cx, cy));
			}
		}
	}

	// Compare
	double avg = 0.0, cnt = 0.0;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (c[i][j] == '#' || c[i][j] == '*') continue;
			if (i == gx && j == gy) continue;
			avg += dist[i][j];
			cnt += 1.0;
		}
	}
	avg /= cnt;
	if (avg > tm) return true;
	return false;
}

int main() {
	// Step 1. Input
	cin >> W >> H;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			cin >> c[i][j];
			if (c[i][j] == 's') { sx = i; sy = j; c[i][j] = '.'; }
			if (c[i][j] == 'g') { gx = i; gy = j; c[i][j] = '.'; }
		}
	}

	// Step 2. Binary Search
	double cl = 0, cr = 1e8, cm;
	for (int i = 0; i < 70; i++) {
		cm = (cl + cr) / 2.0;
		bool ret = check(cm);
		if (ret == true) cl = cm;
		else cr = cm;
	}

	// Step 3. Output
	check(cm);
	printf("%.12lf\n", dist[sx][sy]);
	return 0;
}
accuracy: 0.55
time_ms: 120
memory_kb: 5964
final_rank: 12

code_snippet: #include "bits/stdc++.h"

using namespace std;

constexpr int MOD=998244353;

int N,M,K,W,H;
long double ans;

bool Check(vector<string>s,long double p){
	int sy,sx,gy,gx;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(s[i][j]=='s')sy=i,sx=j;
			if(s[i][j]=='g')gy=i,gx=j;
		}
	}
	priority_queue<pair<long double,pair<int,int>>,vector<pair<long double,pair<int,int>>>,greater<pair<long double,pair<int,int>>>>PQ;
	vector<vector<long double>>dp(H,vector<long double>(W,1e18));
	dp[gy][gx]=0;
	PQ.push({0,{gy,gx}});
	int dir[]={1,0,-1,0,1};
	for(int h=0;h<H;h++){
		for(int w=0;w<W;w++){
			if(s[h][w]=='*'){
				for(int d=0;d<4;d++){
					int ny=h+dir[d];
					int nx=w+dir[d+1];
					if(ny<0||nx<0||ny>=H||nx>=W)continue;
					if(s[ny][nx]=='g'||s[ny][nx]=='#'||s[ny][nx]=='*')continue;
					if(dp[ny][nx]>p+1){
						dp[ny][nx]=p+1;
						PQ.push({p+1,{ny,nx}});
					}
				}
			}
		}
	}
	while(!PQ.empty()){
		int cy,cx;
		long double cp;
		cp=PQ.top().first;
		tie(cy,cx)=PQ.top().second;
		PQ.pop();
		if(dp[cy][cx]<cp)continue;
		for(int d=0;d<4;d++){
			int ny=cy+dir[d];
			int nx=cx+dir[d+1];
			if(ny<0||nx<0||ny>=H||nx>=W)continue;
			if(s[ny][nx]=='g'||s[ny][nx]=='#'||s[ny][nx]=='*')continue;
			if(dp[ny][nx]>cp+1){
				dp[ny][nx]=cp+1;
				PQ.push({cp+1,{ny,nx}});
			}
		}
	}
	long double sum=0;
	int num=0;
	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			if(s[i][j]=='.'||s[i][j]=='s'){
				sum+=dp[i][j];
				num++;
			}
		}
	}
	ans=dp[sy][sx];
	return sum/num>p;
}

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

    cin>>W>>H;
	vector<string>s(H);
	for(auto &i:s)cin>>i;
	long double l=1,r=1e18;
	for(int loop=0;loop<100;loop++){
		long double mid=(l+r)/2;
		if(Check(s,mid))l=mid;
		else r=mid;
	}
	cout<<fixed<<setprecision(20)<<ans<<endl;
}
accuracy: 1
time_ms: 3200
memory_kb: 9068
final_rank: 6