Submission #1105277

#TimeUsernameProblemLanguageResultExecution timeMemory
1105277vjudge1Maze (JOI23_ho_t3)C++17
100 / 100
1136 ms775468 KiB
#include <bits/stdc++.h> //#define int long long //#define ll long long #define fi first #define se second #define pb push_back #define all(lmao) lmao.begin(), lmao.end() using namespace std; typedef pair<int,int> pii; typedef tuple<int,int,int> tp; const int N = 6e6 + 5; const int oo = 1e9 + 1; const int mod = 1e9 + 7; //const ll oo = 5e18; const int ha[4] = {1, -1, 0, 0}; const int co[4] = {0, 0, 1, -1}; int m, n, k, xt, yt, xs, ys; int ans; vector<int> f[N]; vector<int> a[N], ph[N], pc[N]; deque<tp> q; bool kt(int x,int y){ return x >= 1 && x <= m && y >= 1 && y <= n; } int row(int u,int v){ return (ph[u][v] == v ? v : ph[u][v] = row(u, ph[u][v])); } int col(int u,int v){ return (pc[u][v] == u ? u : pc[u][v] = col(pc[u][v], v)); } void conduct(int val,int x,int y){ int l = max(1, x - k + 1), r = min(m, x + k - 1), pl = max(1, y - k + 1), pr = min(n, y + k - 1); if(xt >= l && xt <= r && yt >= pl && yt <= pr) ans = min(ans, val + 1); if(l - 1 >= 1){ int lx = l - 1, g = pr; while(ph[lx][g] >= pl){ g = ph[lx][g]; if(f[lx][g] > val + 1) f[lx][g] = val + 1, q.push_back({f[lx][g], lx, g}); if(!ph[lx][g]) break; ph[lx][g] = ph[lx][g - 1]; ph[lx][g] = row(lx, ph[lx][g]); } } if(r + 1 <= m){ int lx = r + 1, g = pr; while(ph[lx][g] >= pl){ g = ph[lx][g]; if(f[lx][g] > val + 1) f[lx][g] = val + 1, q.push_back({f[lx][g], lx, g}); if(!ph[lx][g]) break; ph[lx][g] = ph[lx][g - 1]; ph[lx][g] = row(lx, ph[lx][g]); } } if(pl - 1 >= 1){ int g = r, ly = pl - 1; while(pc[g][ly] >= l){ g = pc[g][ly]; if(f[g][ly] > val + 1) f[g][ly] = val + 1, q.push_back({f[g][ly], g, ly}); if(!pc[g][ly]) break; pc[g][ly] = pc[g - 1][ly]; pc[g][ly] = col(pc[g][ly], ly); } } if(pr + 1 <= n){ int g = r, ly = pr + 1; while(pc[g][ly] >= l){ g = pc[g][ly]; if(f[g][ly] > val + 1) f[g][ly] = val + 1, q.push_back({f[g][ly], g, ly}); if(!pc[g][ly]) break; pc[g][ly] = pc[g - 1][ly]; pc[g][ly] = col(pc[g][ly], ly); } } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define task "v" if(fopen(task ".inp","r")){ freopen(task ".inp","r",stdin); freopen(task ".out","w",stdout); } cin >> m >> n >> k; cin >> xs >> ys >> xt >> yt; for(int i = 1; i <= m; i ++){ string s; cin >> s; f[i].pb(oo); a[i].pb(0); ph[i].pb(0); pc[i].pb(0); for(int j = 0; j < n; j ++){ f[i].pb(oo); pc[i].pb(0); ph[i].pb(j + 1); if(s[j] == '.') a[i].pb(0); else a[i].pb(1); } f[i].pb(oo); a[i].pb(0); ph[i].pb(0); pc[i].pb(0); } for(int i = 1; i <= n + 1; i ++) pc[0].pb(0); for(int i = 1; i <= n; i ++){ for(int j = 1; j <= m; j ++){ pc[j][i] = j; } } f[xs][ys] = 0; q.push_back({0, xs, ys}); ans = oo; while(!q.empty()){ int val, px, py; tie(val, px, py) = q.front(); q.pop_front(); if(val != f[px][py]) continue; if(px == xt && py == yt) ans = min(ans, val); conduct(val, px, py); for(int d = 0; d < 4; d ++){ int x = px + ha[d]; int y = py + co[d]; if(!kt(x, y) || f[x][y] <= val || a[x][y]) continue; q.push_front({f[x][y] = val, x, y}); if(ph[x][y]){ ph[x][y] = ph[x][y - 1]; ph[x][y] = row(x, ph[x][y]); } if(pc[x][y]){ pc[x][y] = pc[x - 1][y]; pc[x][y] = col(pc[x][y], y); } } } cout << min(ans, f[xt][yt]); }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(task ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(task ".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...