Submission #748149

#TimeUsernameProblemLanguageResultExecution timeMemory
748149onebit1024Portals (BOI14_portals)C++17
70 / 100
1095 ms89192 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define all(c) c.begin(), c.end() #define endl "\n" const double PI=3.141592653589; void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " <<"[" << #x << "] = ["; _print(x) #else #define dbg(x...) #endif int n,m; vector<pair<int,int>>neigh(int i, int j){ vector<pair<int,int>>res; if(i < n)res.pb({i+1,j}); if(i > 1)res.pb({i-1,j}); if(j < m)res.pb({i,j+1}); if(j > 1)res.pb({i,j-1}); return res; } void solve() { cin >> n >> m; vector<vector<int>>a(n+2, vector<int>(m+2,1)),dist(n+2, vector<int>(m+2,1e18)),left(n+1, vector<int>(m+1)),right(n+1, vector<int>(m+1)),up(n+1, vector<int>(m+1)),down(n+1, vector<int>(m+1)); priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq; int ex = 0, ey = 0; for(int i = 1;i<=n;++i){ for(int j = 1;j<=m;++j){ char x; cin >> x; if(x=='.')a[i][j] = 0; else if(x=='C')ex = i,ey = j,a[i][j]=0; else if(x=='S')pq.push({0,i,j}),dist[i][j] = 0,a[i][j]=0; } } // dp[i][j] closest wall for(int i = 1;i<=n;++i){ int prev = m+1; for(int j = m;j>=1;--j){ if(a[i][j])prev = j; right[i][j] = prev; } prev = 0; for(int j = 1;j<=m;++j){ if(a[i][j])prev = j; left[i][j] = prev; } } for(int j = 1;j<=m;++j){ int prev = 0; for(int i = 1;i<=n;++i){ if(a[i][j])prev = i; up[i][j] = prev; } prev = n+1; for(int i = n;i>=1;--i){ if(a[i][j])prev = i; down[i][j] = prev; } } vector<vector<int>>wall(n+2, vector<int>(m+2, 1e18)),vis(n+2, vector<int>(m+2)); // closest wall cell queue<pair<int,int>>q; for(int i = 0;i<=n+1;++i){ for(int j = 0;j<=m+1;++j){ if(a[i][j]){ wall[i][j] = 0; q.push({i,j}); } } } while(!q.empty()){ int i = q.front().first, j = q.front().second; q.pop(); if(vis[i][j])continue; vis[i][j] = 1; for(auto &[x,y] : neigh(i,j)){ if(a[x][y])continue; wall[x][y] = min(wall[x][y],wall[i][j]+1); q.push({x,y}); } } while(!pq.empty()){ int d = pq.top()[0], i = pq.top()[1], j = pq.top()[2]; pq.pop(); dbg(i,j,dist[i][j]); if(dist[i][j]!=d)continue; for(auto &[x,y] : neigh(i,j)){ if(a[x][y])continue; int curr_dist = dist[x][y], new_dist = dist[i][j]+1; if(new_dist < curr_dist){ dist[x][y] = new_dist; pq.push({new_dist, x, y}); } } int val = wall[i][j]; for(auto w : {left[i][j],right[i][j]}){ int y = w+1; if(w > j)y = w-1; int x = i; int curr_dist = dist[x][y], new_dist = dist[i][j]+val; if(new_dist < curr_dist){ dist[x][y] = new_dist; pq.push({new_dist, x, y}); } } for(auto w : {up[i][j],down[i][j]}){ int x = w+1; if(w > i)x = w-1; int y = j; int curr_dist = dist[x][y], new_dist = dist[i][j]+val; if(new_dist < curr_dist){ dist[x][y] = new_dist; pq.push({new_dist, x, y}); } } } cout << dist[ex][ey]; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int T=1; for(int i = 1;i<=T;++i) { // cout << "Case #" << i << ": "; solve(); } }
#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...