Submission #250619

#TimeUsernameProblemLanguageResultExecution timeMemory
250619opukittpceno_hhrPortals (BOI14_portals)C++17
100 / 100
876 ms127608 KiB
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <string> #include <cmath> #include <cstdio> #include <iomanip> #include <fstream> #include <cassert> #include <cstring> #include <unordered_set> #include <unordered_map> #include <numeric> #include <ctime> #include <bitset> #include <complex> #include <chrono> #include <random> #include <functional> using namespace std; const int N = 1010; const int INF = 1e9 + 239; const vector<int> dx = {1, -1, 0, 0}; const vector<int> dy = {0, 0, 1, -1}; int n, m; int a[N][N]; int d[N * N]; vector<pair<int, int>> g[N * N]; int ok(int i, int j) { return 0 <= i && i < n && 0 <= j && j < m && !a[i][j]; } int get(int i, int j) { return i * m + j; } void dij(int st) { fill(d, d + n * m, INF); d[st] = 0; set<pair<int, int>> s; s.insert({d[st], st}); while (!s.empty()) { int v = s.begin()->second; s.erase(s.begin()); for (auto [u, w] : g[v]) { if (d[u] > d[v] + w) { s.erase({d[u], u}); d[u] = d[v] + w; s.insert({d[u], u}); } } } } int cu[N][N]; int cd[N][N]; int cl[N][N]; int cr[N][N]; void init() { for (int i = 0; i < n; i++) { cl[i][0] = 0; for (int j = 1; j < m; j++) { if (!a[i][j]) { cl[i][j] = 1 + cl[i][j - 1]; } else { cl[i][j] = 0; } } cr[i][m - 1] = 0; for (int j = m - 2; j >= 0; j--) { if (!a[i][j]) { cr[i][j] = 1 + cr[i][j + 1]; } else { cr[i][j] = 0; } } } for (int j = 0; j < m; j++) { cu[0][j] = 0; for (int i = 1; i < n; i++) { if (!a[i][j]) { cu[i][j] = 1 + cu[i - 1][j]; } else { cu[i][j] = 0; } } cd[n - 1][j] = 0; for (int i = n - 2; i >= 0; i--) { if (!a[i][j]) { cd[i][j] = 1 + cd[i + 1][j]; } else { cd[i][j] = 0; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!a[i][j]) { cl[i][j]--; cr[i][j]--; cu[i][j]--; cd[i][j]--; } } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { a[i][j] = 1; } } int si = -1; int sj = -1; int ci = -1; int cj = -1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char c; cin >> c; a[i][j] = (c == '#'); if (c == 'S') { si = i; sj = j; } if (c == 'C') { ci = i; cj = j; } } } n += 2; m += 2; init(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j]) continue; int mn = min({cu[i][j], cd[i][j], cl[i][j], cr[i][j]}); g[get(i, j)].push_back({get(i - cu[i][j], j), 1 + mn}); g[get(i, j)].push_back({get(i + cd[i][j], j), 1 + mn}); g[get(i, j)].push_back({get(i, j - cl[i][j]), 1 + mn}); g[get(i, j)].push_back({get(i, j + cr[i][j]), 1 + mn}); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < 4; k++) { if (ok(i, j) && ok(i + dx[k], j + dy[k])) { g[get(i, j)].push_back({get(i + dx[k], j + dy[k]), 1}); } } } } dij(get(si, sj)); cout << d[get(ci, cj)] << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...