Submission #233948

#TimeUsernameProblemLanguageResultExecution timeMemory
233948parsa_mobedPortals (BOI14_portals)C++14
31 / 100
17 ms10624 KiB
#include <bits/stdc++.h> using namespace std; #define pii pair <int , int> #define F first #define S second #define num(i, j) i * (m + 1) + j #define IsAdjToWall(i, j) !(A[i - 1][j] & A[i + 1][j] & A[i][j - 1] & A[i][j + 1]) const int N = 1e3 + 5; int A[N][N], dis[N][N], sid[N][N], nxt[N][N][4]; int n, m, S, E; pii dir[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; void BFS() { memset(sid, 31, sizeof sid); queue <int> q; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (A[i][j] == 1 && IsAdjToWall(i, j)) sid[i][j] = 0, q.push(num(i, j)); while (!q.empty()) { int x = q.front() / (m + 1), y = q.front() % (m + 1); q.pop(); for (int i = 0; i < 4; i++) if (A[x + dir[i].F][y + dir[i].S] && sid[x][y] + 1 < sid[x + dir[i].F][y + dir[i].S]) sid[x + dir[i].F][y + dir[i].S] = sid[x][y] + 1, q.push(num(x + dir[i].F, y + dir[i].S)); } } void Dijkstra() { memset(dis, 31, sizeof dis); priority_queue <pii, vector<pii>, greater<pii>> pq; dis[S / (m + 1)][S % (m + 1)] = 0; pq.push({0, S}); while (!pq.empty()) { int x = pq.top().S / (m + 1), y = pq.top().S % (m + 1); pq.pop(); for (int i = 0; i < 4; i++) { int _x = x + dir[i].F, _y = y + dir[i].S; if (A[_x][_y] && dis[x][y] + 1 < dis[_x][_y]) dis[_x][_y] = dis[x][y] + 1, pq.push({dis[_x][_y], num(_x, _y)}); _x = nxt[x][y][i] / (m + 1), _y = nxt[x][y][i] % (m + 1); if (A[_x][_y] && dis[x][y] + sid[x][y] + 1 < dis[_x][_y]) dis[_x][_y] = dis[x][y] + sid[x][y] + 1, pq.push({dis[_x][_y], num(_x, _y)}); } } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { char c; cin >> c; if (c != '#') A[i][j] = 1; if (c == 'S') S = num(i, j); if (c == 'C') E = num(i, j); } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (A[i][j]) { nxt[i][j][0] = (A[i - 1][j] ? nxt[i - 1][j][0] : num(i, j)); nxt[i][j][2] = (A[i][j - 1] ? nxt[i][j - 1][2] : num(i, j)); } for (int i = n; i >= 1; i--) for (int j = m; j >= 1; j--) if (A[i][j]) { nxt[i][j][1] = (A[i + 1][j] ? nxt[i + 1][j][1] : num(i, j)); nxt[i][j][3] = (A[i][j + 1] ? nxt[i][j + 1][3] : num(i, j)); } BFS(), Dijkstra(); cout << dis[E / (m + 1)][E % (m + 1)] << "\n"; }
#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...