Submission #784383

#TimeUsernameProblemLanguageResultExecution timeMemory
784383raysh07Portals (BOI14_portals)C++17
20 / 100
21 ms28120 KiB
#include <bits/stdc++.h> using namespace std; #define INF (int)1e9 mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count()); int n, m; const int N = 1005; bool a[N][N]; int d[N][N]; int x, y; vector <pair<int, int>> ok[N][N]; void Solve() { cin >> n >> m; queue <pair<int, int>> q; for (int i = 1; i <= n; i++){ string s; cin >> s; for (int j = 1; j <= m; j++){ ok[i][j].push_back({i - 1, j}); ok[i][j].push_back({i, j + 1}); ok[i][j].push_back({i + 1, j}); ok[i][j].push_back({i, j - 1}); d[i][j] = INF; if (s[j - 1] == 'S'){ a[i][j] = true; d[i][j] = 0; q.push({i, j}); } else if (s[j - 1] == 'C'){ a[i][j] = true; x = i; y = j; } else if (s[j - 1] == '.'){ a[i][j] = true; } } } for (int i = 1; i <= n; i++){ int last = 0; for (int j = 1; j <= m; j++){ ok[i][j].push_back({i, last}); if (!a[i][j]) last = 0; else if (last == 0) last = j; } last = 0; for (int j = m; j >= 1; j--){ ok[i][j].push_back({i, last}); if (!a[i][j]) last = 0; else if (last == 0) last = j; } } for (int j = 1; j <= m; j++){ int last = 0; for (int i = 1; i <= n; i++){ ok[i][j].push_back({last, j}); if (!a[i][j]) last = 0; else if (last == 0) last = i; } last = 0; for (int i = n; i >= 1; i--){ ok[i][j].push_back({last, j}); if (!a[i][j]) last = 0; else if (last == 0) last = i; } } while (!q.empty()){ auto pi = q.front(); q.pop(); int ux = pi.first, uy = pi.second; // cout << ux << " " << uy << "\n"; for (auto [vx, vy] : ok[ux][uy]){ if (!a[vx][vy]) continue; if (d[vx][vy] == INF){ d[vx][vy] = d[ux][uy] + 1; q.push({vx, vy}); } } } cout << d[x][y] << "\n"; } int32_t main() { auto begin = std::chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // freopen("in", "r", stdin); // freopen("out", "w", stdout); // cin >> t; for(int i = 1; i <= t; i++) { //cout << "Case #" << i << ": "; Solve(); } auto end = std::chrono::high_resolution_clock::now(); auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin); cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; return 0; }
#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...