1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include <bits/stdc++.h> #define PI acos(-1) #define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define endl '\n' #define PII pair<int, int>
using namespace std; const int N = 2e6 + 5; int n, m; int id(int x, int y) { return (x - 1) * (2 * m + 1) + y; } int hashed(int x, int y, int f) { if (f == 1) { return id((x - 1) * 2 + 1, y * 2); } else if (f == 2) { return id(x * 2 + 1, y * 2); } else if (f == 3) { return id(x * 2, (y - 1) * 2 + 1); } return id(x * 2, y * 2 + 1); }
char s[1505][1505]; vector<pair<double, int>> g[N]; void addall(int x, int y) { g[hashed(x, y, 1)].push_back({10.0, hashed(x, y + 1, 1)}); g[hashed(x, y, 1)].push_back({10.0, hashed(x, y + 1, 3)});
g[hashed(x, y, 2)].push_back({10.0, hashed(x + 1, y + 1, 1)}); g[hashed(x, y, 2)].push_back({10.0, hashed(x + 1, y + 1, 3)});
g[hashed(x, y, 3)].push_back({10.0, hashed(x + 1, y, 1)}); g[hashed(x, y, 3)].push_back({10.0, hashed(x + 1, y, 3)});
g[hashed(x, y, 4)].push_back({10.0, hashed(x + 1, y + 1, 1)}); g[hashed(x, y, 4)].push_back({10.0, hashed(x + 1, y + 1, 3)}); } void addo(int x, int y) { double L = PI * 5.0 / 2.0; g[hashed(x, y, 1)].push_back({L, hashed(x, y, 4)}); g[hashed(x, y, 3)].push_back({L, hashed(x, y, 2)}); }
int st[N]; double dist[N]; priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> q; double dijkstra() { for (int i = 0; i < N; ++i) dist[i] = 2e9; dist[hashed(1, 1, 1)] = 5.0; dist[hashed(1, 1, 3)] = 5.0; q.push({5.0, hashed(1, 1, 1)}); q.push({5.0, hashed(1, 1, 3)}); while (q.size()) { pair<double, int> t = q.top(); q.pop(); double distance = t.first; int u = t.second; if (st[u]) continue; st[u] = 1; for (int i = 0; i < g[u].size(); ++i) { int j = g[u][i].second; double d = g[u][i].first; if (dist[j] > dist[u] + d) { dist[j] = dist[u] + d; if (!st[j]) { q.push({dist[j], j}); } } } } return min(dist[hashed(n, m, 2)], dist[hashed(n, m, 4)]) + 5.0; }
void solves() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> s[i][j];
for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { addall(i, j); if (s[i][j] == 'O') addo(i, j); } printf("%.10lf", dijkstra()); } signed main() { IOS; int T = 1; while (T--) solves(); }
|