#include <bits/stdc++.h>
#define INF 1000000000
using namespace std;
int r, c;
double ll;
char mp[110][110];
int get(int rr, int cc)
{
return (rr - 1) * c + cc;
}
int dr[4] = {1, -1, 0, 0}, dc[4] = {0, 0, 1, -1};
vector <int> bi[10010];
vector <double> ci[10010];
int s, e;
void build(int a, int b, double c)
{
bi[a].push_back(b); ci[a].push_back(c);
bi[b].push_back(a); ci[b].push_back(c);
}
struct node
{
int t; double l;
};
bool operator < (node a, node b) {return a.l > b.l;}
priority_queue <node> H;
int vis[10010]; double dis[10010];
int main()
{
int T;
cin >> T;
while (T --)
{
cin >> ll >> r >> c;
for (int i = 1; i <= r; i++)
{
char cc;
scanf("%c", &cc);
while (cc != '\n') scanf("%c", &cc);
for (int j = 1; j <= c; j++)
{
scanf("%c", &cc);
while (cc == '\n') scanf("%c",&cc);
mp[i][j] = cc;
if (cc=='S') s = get(i, j);
if (cc=='E') e = get(i, j);
}
}
{
double lb = 0, rb = 10;
while (rb - lb > 0.0000001)
{
double md = (lb + rb) / 2;
for (int i = 1; i <= r * c; ++ i) bi[i].clear(), ci[i].clear();
for (int i = 1; i <= r; ++ i)
for (int j = 1; j <= c; ++ j)
for (int d = 0; d < 4; ++ d)
if (mp[i][j] != '#' && mp[i + dr[d]][j + dc[d]] != '#')
build(get(i, j), get(i + dr[d], j + dc[d]), (d >= 2? 1: md));
for (int i = 1; i <= r * c; ++ i)
dis[i] = INF, vis[i] = 0;
while (!H.empty()) H.pop();
dis[s] = 0,
H.push((node){s, 0});
while (!H.empty())
{
int hd;
do hd = H.top().t, H.pop();
while (vis[hd] && !H.empty());
if (!vis[hd]) vis[hd] = 1;
else break;
for (int i = 0; i < bi[hd].size(); ++ i)
if (dis[hd] + ci[hd][i] < dis[bi[hd][i]])
{
dis[bi[hd][i]] = dis[hd] + ci[hd][i];
H.push((node){bi[hd][i], dis[bi[hd][i]]});
}
}
if (dis[e] > ll) rb = md;
else lb = md;
}
printf("%.5lf\n", lb);
}
}
}
12709:[Violet 1]迷宫花园
最新推荐文章于 2024-09-05 19:38:47 发布