Submission #884944

#TimeUsernameProblemLanguageResultExecution timeMemory
884944AlcabelMaze (JOI23_ho_t3)C++17
51 / 100
2041 ms41324 KiB
#pragma GCC optimize("O3", "Ofast", "unroll-loops") #include <bits/stdc++.h> using namespace std; const int inf = 1e7; const pair<int, int> deltas[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; vector<string> a; vector<vector<int>> vis, dist; int r, c, mini, minj, maxi, maxj, tt; vector<pair<int, int>> q; void dfs(int i, int j) { q.emplace_back(i, j); vis[i][j] = tt; for (const auto& [di, dj] : deltas) { if (i + di >= mini && i + di <= maxi && j + dj >= minj && j + dj <= maxj && vis[i + di][j + dj] < tt && dist[i + di][j + dj] >= dist[i][j]) { dist[i + di][j + dj] = dist[i][j]; dfs(i + di, j + dj); } } } void solve() { int n; cin >> r >> c >> n; int sr, sc, gr, gc; cin >> sr >> sc >> gr >> gc; --sr, --sc, --gr, --gc; a.resize(r); for (int i = 0; i < r; ++i) { cin >> a[i]; } vis.resize(r, vector<int>(c, -1)); dist.resize(r, vector<int>(c, inf)); vector<pair<int, int>> ext; dist[sr][sc] = 0; q.emplace_back(sr, sc); tt = -1; ext.reserve(r * c); q.reserve(r * c); while (dist[gr][gc] == inf) { ++tt; while (!q.empty()) { auto [i, j] = q.back(); q.pop_back(); if (vis[i][j] == tt) { continue; } vis[i][j] = tt; ext.emplace_back(i, j); for (const auto& [di, dj] : deltas) { int newi = i + di, newj = j + dj; if (newi >= 0 && newi < r && newj >= 0 && newj < c && a[newi][newj] == '.' && dist[newi][newj] == inf) { dist[newi][newj] = dist[i][j]; q.emplace_back(newi, newj); } } } for (int iter = 0; iter < 4; ++iter) { ++tt; sort(ext.begin(), ext.end()); if (iter == 0) { // up for (auto [i, j] : ext) { mini = max(0, i - n), minj = max(0, j - n + 1); maxi = min(r - 1, i - 1), maxj = min(c - 1, j + n - 1); if (i - 1 >= 0) { int newD = dist[i][j] + 1; --i; while (j <= maxj && (vis[i][j] == tt || dist[i][j] == newD - 1)) { ++j; } if (j <= maxj && dist[i][j] > newD - 1) { dist[i][j] = newD; dfs(i, j); } } } } else if (iter == 1) { // left for (auto [j, i] : ext) { i = -i; mini = max(0, i - n + 1), minj = max(0, j - n); maxi = min(r - 1, i + n - 1), maxj = min(c - 1, j - 1); if (j - 1 >= 0) { int newD = dist[i][j] + 1; --j; while (i >= mini && (vis[i][j] == tt || dist[i][j] == newD - 1)) { --i; } if (i >= mini && dist[i][j] > newD - 1) { dist[i][j] = newD; dfs(i, j); } } } } else if (iter == 2) { // down for (auto [i, j] : ext) { i = -i, j = -j; mini = max(0, i + 1), minj = max(0, j - n + 1); maxi = min(r - 1, i + n), maxj = min(c - 1, j + n - 1); if (i + 1 < r) { int newD = dist[i][j] + 1; ++i; while (j >= minj && (vis[i][j] == tt || dist[i][j] == newD - 1)) { --j; } if (j >= minj && dist[i][j] > newD - 1) { dist[i][j] = newD; dfs(i, j); } } } } else { // right for (auto [j, i] : ext) { j = -j; mini = max(0, i - n + 1), minj = max(0, j + 1); maxi = min(r - 1, i + n - 1), maxj = min(c - 1, j + n); if (j + 1 < c) { int newD = dist[i][j] + 1; ++j; while (i <= maxi && (vis[i][j] == tt || dist[i][j] == newD - 1)) { ++i; } if (i <= maxi && dist[i][j] > newD - 1) { dist[i][j] = newD; dfs(i, j); } } } } for (int i = 0; i < (int)ext.size(); ++i) { ext[i].first = -ext[i].first; swap(ext[i].first, ext[i].second); } } ext.clear(); } cout << dist[gr][gc] << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); int T = 1; cin >> T; while (T--) { solve(); cerr << "-----------\n"; cout << "-----------\n"; } #else int T = 1; // cin >> T; while (T--) { solve(); } #endif 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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...