Submission #287390

#TimeUsernameProblemLanguageResultExecution timeMemory
287390NamnamseoPortals (BOI14_portals)C++17
100 / 100
641 ms92792 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pp; typedef pair<ll,ll> pll; void read(int& x){ scanf("%d",&x); } void read(ll& x){ scanf("%lld",&x); } template<typename T,typename... Args> void read(T& a,Args&... b){ read(a); read(b...); } #define all(x) (x).begin(),(x).end() #define pb push_back #define eb emplace_back #define x first #define y second int n, m; inline int enc(int r, int c){ return (r-1)*m + c; } char z[1010][1010]; int aL[1010][1010], aR[1010][1010], aD[1010][1010], aU[1010][1010]; void bm(){ for(int i=1; i<=n; ++i){ aL[i][0] = 1; for(int j=1; j<=m; ++j){ aL[i][j] = aL[i][j-1]; if(z[i][j] == '#') aL[i][j]=j+1; } aR[i][m+1] = m; for(int j=m; 1<=j; --j){ aR[i][j] = aR[i][j+1]; if(z[i][j] == '#') aR[i][j]=j-1; } } for(int j=1; j<=m; ++j){ aU[0][j] = 1; for(int i=1; i<=n; ++i){ aU[i][j] = aU[i-1][j]; if(z[i][j] == '#') aU[i][j]=i+1; } aD[n+1][j] = n; for(int i=n; 1<=i; --i){ aD[i][j] = aD[i+1][j]; if(z[i][j] == '#') aD[i][j]=i-1; } } } vector<pp> e[1001*1001]; void be(){ for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ if(z[i][j]=='#') continue; int nd = 1e9; nd = min(nd, j - aL[i][j]); nd = min(nd, i - aU[i][j]); nd = min(nd, aR[i][j] - j); nd = min(nd, aD[i][j] - i); nd += 1; int me = enc(i, j); auto f = [&](int a, int b){ //printf("%d,%d -> %d,%d\n", i, j, a, b); e[me].eb(enc(a, b), nd); }; f(i, aL[i][j]); f(i, aR[i][j]); f(aU[i][j], j); f(aD[i][j], j); //putchar(10); } } } int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; pp sp, ep; priority_queue<pp> pq; int dist[1001*1001]; int main() { read(n, m); for(int i=1; i<=n; ++i){ scanf("%s", z[i]+1); for(int j=1; j<=m; ++j){ if(z[i][j] == 'S') sp={i, j}; if(z[i][j] == 'C') ep={i, j}; } } bm(); be(); int V = n*m; for(int i=1; i<=V; ++i) dist[i] = 1e9; int s = enc(sp.x, sp.y); dist[s]=0; pq.emplace(0, s); while(pq.size()){ int d, p; tie(d, p)=pq.top(); pq.pop(); if(-d != dist[p]) continue; int x=1+(p-1)/m, y=p-(x-1)*m; for(int d=0; d<4; ++d){ int nx=x+dx[d], ny=y+dy[d]; if(1<=nx && 1<=ny && nx<=n && ny<=m && z[nx][ny] != '#'){ int np=enc(nx, ny); if(dist[np] > dist[p]+1){ dist[np] = dist[p] + 1; pq.emplace(-dist[np], np); } } } for(auto t:e[p]){ int q, d; tie(q, d)=t; if(dist[q] > dist[p]+d){ dist[q] = dist[p]+d; pq.emplace(-dist[q], q); } } } printf("%d\n", dist[enc(ep.x, ep.y)]); return 0; }

Compilation message (stderr)

portals.cpp: In function 'void read(int&)':
portals.cpp:6:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    6 | void read(int& x){ scanf("%d",&x); }
      |                    ~~~~~^~~~~~~~~
portals.cpp: In function 'void read(ll&)':
portals.cpp:7:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    7 | void read(ll& x){ scanf("%lld",&x); }
      |                   ~~~~~^~~~~~~~~~~
portals.cpp: In function 'int main()':
portals.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   87 |   scanf("%s", z[i]+1);
      |   ~~~~~^~~~~~~~~~~~~~
#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...