Submission #581464

#TimeUsernameProblemLanguageResultExecution timeMemory
581464Soul234Portal (COCI17_portal)C++14
120 / 120
49 ms6860 KiB
#include<bits/stdc++.h> using namespace std; void DBG() { cerr << "]\n"; } template<class H, class... T> void DBG(H h, T... t) { cerr << h; if(sizeof...(t)) cerr << ", "; DBG(t...); } #ifdef LOCAL #define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #else #define dbg(...) 0 #endif // LOCAL #define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--) #define R0F(i,a) ROF(i,0,a) #define each(e,a) for(auto &e : (a)) #define sz(v) (int)(v).size() #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define pb push_back #define tcT template<class T #define nl "\n" using ll = long long; using vi = vector<int>; using pi = pair<int,int>; using str = string; tcT> using V = vector<T>; tcT> using pqg = priority_queue<T,vector<T>,greater<T>>; void setIO(string NAME = "") { cin.tie(0)->sync_with_stdio(0); if(sz(NAME)) { freopen((NAME + ".inp").c_str(),"r",stdin); freopen((NAME + ".out").c_str(),"w",stdout); } } tcT> bool ckmin(T&a, const T&b) { return b < a ? a=b,1 : 0; } tcT> bool ckmax(T&a, const T&b) { return b > a ? a=b,1 : 0; } const int MOD = 1e9 + 7; const int dx[] = {-1, 0 ,1, 0}; const int dy[] = {0, -1, 0, 1}; const int MX = 505; int N, M; char A[MX][MX]; int dtnw[MX][MX], lef[MX][MX], rig[MX][MX], up[MX][MX], down[MX][MX], dis[MX][MX]; pi st, en; bool valid(int x, int y) { return min(x,y) >= 1 && x <= N && y <= M && A[x][y] != '#'; } #define d(x) dis[x.first][x.second] void solve() { cin>>N>>M; FOR(i,1,N+1) FOR(j,1,M+1) { cin>>A[i][j]; if(A[i][j] == 'C') st = {i,j}; else if(A[i][j] == 'F') en = {i,j}; } FOR(i,1,N+1) { lef[i][0] = 0; FOR(j,1,M+1) { if(A[i][j] == '#') lef[i][j] = j; else lef[i][j] = lef[i][j-1]; } rig[i][M+1] = M+1; ROF(j,1,M+1) { if(A[i][j] == '#') rig[i][j] = j; else rig[i][j] = rig[i][j+1]; } } FOR(j,1,M+1) { up[0][j] = 0; FOR(i,1,N+1) { if(A[i][j] == '#') up[i][j] = i; else up[i][j] = up[i-1][j]; } down[N+1][j] = N+1; ROF(i,1,N+1) { if(A[i][j] == '#') down[i][j] = i; else down[i][j] = down[i+1][j]; } } memset(dtnw, 0x3f, sizeof dtnw); memset(dis, 0x3f, sizeof dis); dis[st.first][st.second] = 0; FOR(i,1,N+1) FOR(j,1,M+1) if(A[i][j] != '#') { int L = j - lef[i][j], R = rig[i][j] - j, U = i - up[i][j], D = down[i][j] - i; ckmin(dtnw[i][j], min({L, R, U, D})-1); } pqg<pair<int,pi>> pq; pq.push({0, st}); while(sz(pq)) { int du = pq.top().first; pi cur = pq.top().second; pq.pop(); if(du > d(cur)) continue; F0R(i,4) { pi ncur = {cur.first + dx[i], cur.second + dy[i]}; if(valid(ncur.first, ncur.second) && ckmin(d(ncur), du + 1)) pq.push({d(ncur), ncur}); } int dv = du + dtnw[cur.first][cur.second] + 1; { pi ncur = {cur.first, lef[cur.first][cur.second] + 1}; if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur}); } { pi ncur = {cur.first, rig[cur.first][cur.second] - 1}; if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur}); } { pi ncur = {up[cur.first][cur.second]+1, cur.second}; if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur}); } { pi ncur = {down[cur.first][cur.second]-1, cur.second}; if(ckmin(d(ncur), dv)) pq.push({d(ncur), ncur}); } } if(d(en) >= MOD) cout << "nemoguce\n"; else cout << d(en) << nl; } int main() { setIO(); int t=1; //cin>>t; while(t-->0) { solve(); } return 0; }

Compilation message (stderr)

portal.cpp: In function 'void setIO(std::string)':
portal.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   37 |         freopen((NAME + ".inp").c_str(),"r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
portal.cpp:38:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         freopen((NAME + ".out").c_str(),"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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...