Submission #474948

#TimeUsernameProblemLanguageResultExecution timeMemory
474948NTGPatkice II (COCI21_patkice2)C++11
110 / 110
1328 ms78708 KiB
#include<bits/stdc++.h> using namespace std; const int N=2e3+1, oo=0x3f3f3f3f; typedef pair<int,int> ii; typedef tuple<int,int,int> iii; #define fi first #define sc second #define ux(x) get<1>(x) #define uy(x) get<2>(x) #define w(x) get<0>(x) int r,s,a[N][N]; char cc[7]={'o','x','.','>','<','v','^'}; int h[4]={0,0,1,-1}, c[4]={1,-1,0,0}; ii ss,ff; bool check(int x, int n) { return (x>0 && x<=n); } int d[N][N]; ii trace[N][N]; void dij() { for(int i=0; i<=r+1; i++) for(int j=1; j<=s+1; j++) d[i][j]=oo, trace[i][j]={-1,-1}; priority_queue<iii,vector<iii>,greater<iii>> q; q.push({0,ss.fi,ss.sc}); d[ss.fi][ss.sc]=0; while(!q.empty()) { int u1=ux(q.top()); int u2=uy(q.top()); int du=w(q.top()); if(u1 == ff.fi && u2 == ff.sc) break; q.pop(); if(du!=d[u1][u2]) continue; for(int i=0; i<4; i++) if(check(u1+h[i],r) && check(u2+c[i],s)) { int v1=u1+h[i]; int v2=u2+c[i]; int t=1; if(i+3 == a[u1][u2] || (u1 == ss.fi && u2 == ss.sc)) t=0; if(d[v1][v2] > d[u1][u2] + t) { d[v1][v2]=d[u1][u2]+t; trace[v1][v2]={h[i],c[i]}; q.push({d[v1][v2],v1,v2}); } } } cout<<d[ff.fi][ff.sc]<<'\n'; ii t=ff; while(t!=ss) { ii tt=trace[t.fi][t.sc]; t={t.fi-tt.fi,t.sc-tt.sc}; for(int i=0; i<4; i++) if(tt.fi == h[i] && tt.sc == c[i]) {a[t.fi][t.sc]=i+3;break;} } a[ss.fi][ss.sc]=0; for(int i=1; i<=r; i++) { for(int j=1; j<=s; j++) cout<<cc[a[i][j]]; cout<<'\n'; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin>>r>>s; for(int i=1; i<=r; i++) for(int j=1; j<=s; j++) { char ch; cin>>ch; if(ch == 'o') a[i][j]=0, ss={i,j}; if(ch == 'x') a[i][j]=1, ff={i,j}; if(ch == '.') a[i][j]=2; if(ch == '>') a[i][j]=3; if(ch == '<') a[i][j]=4; if(ch == 'v') a[i][j]=5; if(ch == '^') a[i][j]=6; } dij(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...