Submission #739028

#TimeUsernameProblemLanguageResultExecution timeMemory
739028He_HuangluMaze (JOI23_ho_t3)C++17
94 / 100
2090 ms693192 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") //#pragma GCC optimize("O3,fast-math,unroll-loops") #include <bits/stdc++.h> #define ii pair<int, int> #define fi first #define se second using namespace std; const int N = 6e6 + 1; int n, m, s, rs, cs, re, ce; int dx[4] = {0, -1, 0, 1}; int dy[4] = {-1, 0, 1, 0}; vector <int> f[N], a[N], p[2][N]; queue <ii> q[2]; void up(int u, int v, int val) { if(0 < u && u <= m && 0 < v && v <= n && !f[u][v]) { q[1].push({u, v}); f[u][v] = val; } } int get(int t, int id, int u) { return p[t][id][u] == u ? u : p[t][id][u] = get(t, id, p[t][id][u]); } void upd(int t, int id, int i, int j, int val) { int u = get(t, id, i); while (u <= j) { if(t && !f[id][u]) { q[1].push({id, u}); f[id][u] = val; } if(!t && !f[u][id]) { q[1].push({u, id}); f[u][id] = val; } u = get(t, id, u + 1); p[t][id][i] = u; } } main () { cin.tie(0)->sync_with_stdio(0); if(fopen("task.inp", "r")) { freopen("task.inp", "r", stdin); freopen("wa.out", "w", stdout); } cin >> m >> n >> s >> rs >> cs >> re >> ce; for(int i = 1; i <= m; i++) a[i].resize(n + 1), f[i].resize(n + 1); for(int i = 1; i <= m; i++) { for(int j = 0; j <= n + 1; j++) p[1][i].push_back(j); } for(int i = 1; i <= n; i++) { for(int j = 0; j <= m + 1; j++) p[0][i].push_back(j); } for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) { char ch; cin >> ch; a[i][j] = (ch == '.' ? 1 : 0); } } q[1].push({rs, cs}); f[rs][cs] = 1; while (!q[1].empty() || !q[0].empty()) { while (!q[1].empty()) { int x, y; tie(x, y) = q[1].front(); q[1].pop(); q[0].push({x, y}); if(x == re && y == ce) return cout << f[x][y] - 1 << "\n", 0; for(int i = 0; i < 4; i++) { int u = x + dx[i], v = y + dy[i]; if(0 < u && u <= m && 0 < v && v <= n && !f[u][v] && a[u][v]) { f[u][v] = f[x][y]; q[1].push({u, v}); } } } while (!q[0].empty()) { int x, y; tie(x, y) = q[0].front(); q[0].pop(); if(x == rs && y == cs) { int s1 = max(1, x - s), s2 = max(1, y - s); int s3 = min(m, x + s), s4 = min(n, y + s); for(int i = s1; i <= s3; i++) { for(int j = s2; j <= s4; j++) if(!f[i][j]) { if(i == x - s && (j == y - s || j == y + s)) continue ; if(i == x + s && (j == y - s || j == y + s)) continue ; f[i][j] = f[x][y] + 1; q[1].push({i, j}); } } continue ; } int val = f[x][y] + 1; for(int i = 0; i < 4; i++) { int u = x + dx[i], v = y + dy[i]; up(x - s, y + s - 1, val); up(x + s, y + s - 1, val); if(y + s <= n) upd(0, y + s, max(1, x - s + 1), min(m, x + s - 1), val); up(x + s - 1, y - s, val); up(x + s - 1, y + s, val); if(x + s <= m) upd(1, x + s, max(1, y - s + 1), min(n, y + s - 1), val); up(x - s, y - s + 1, val); up(x + s, y - s + 1, val); if(y > s) upd(0, y - s, max(1, x - s + 1), min(m, x + s - 1), val); up(x - s + 1, y - s, val); up(x - s + 1, y + s, val); if(x > s) upd(1, x - s, max(1, y - s + 1), min(n, y + s - 1), val); } } } }

Compilation message (stderr)

Main.cpp:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   44 | main () {
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:95:21: warning: unused variable 'u' [-Wunused-variable]
   95 |                 int u = x + dx[i], v = y + dy[i];
      |                     ^
Main.cpp:95:36: warning: unused variable 'v' [-Wunused-variable]
   95 |                 int u = x + dx[i], v = y + dy[i];
      |                                    ^
Main.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen("task.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen("wa.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...