Submission #489416

#TimeUsernameProblemLanguageResultExecution timeMemory
489416inksamuraiPatkice II (COCI21_patkice2)C++17
110 / 110
851 ms43308 KiB
#include <bits/stdc++.h> #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(),a.end() #define rep(i,n) for(int i=0;i<n;i++) #define crep(i,x,n) for(int i=x;i<n;i++) #define drep(i,n) for(int i=n-1;i>=0;i--) #define vec(...) vector<__VA_ARGS__> #define _3oIUrY0 ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0) using namespace std; typedef long long ll; typedef long double ld; using pii=pair<int,int>; using vi=vector<int>; const int di[]={1,-1,0,0}; const int dj[]={0,0,1,-1}; const int inf=1e9; int main(){ _3oIUrY0; int h,w; cin>>h>>w; vec(vec(char)) a(h,vec(char)(w)); rep(i,h)rep(j,w){ cin>>a[i][j]; } int home=0,to=0; rep(i,h)rep(j,w){ if(a[i][j]=='o') home=i*w+j; if(a[i][j]=='x') to=i*w+j; } int n=h*w; priority_queue<pii,vec(pii),greater<pii>> pq; vec(pii) dp(n,{inf,-1}); dp[home]={0,-1}; pq.push({0,home}); auto ok=[&](int x,int y)->bool{ return (min(x,y)>=0 and x<h and y<w); }; auto vco=[&](char ch,int dir)->int{ if(ch=='o') return 0; int _dir=-1; if(ch=='^') _dir=1; if(ch=='v') _dir=0; if(ch=='>') _dir=2; if(ch=='<') _dir=3; return !(_dir==dir); }; while(sz(pq)){ pii top=pq.top(); pq.pop(); int v=top.se,cost=top.fi; int x=v/w,y=v%w; if(dp[v].fi!=cost or v==to) continue; rep(dir,4){ int nex=x+di[dir],ney=y+dj[dir]; if(ok(nex,ney)){ int u=nex*w+ney; int necost=cost+vco(a[x][y],dir); if(dp[u].fi>necost){ pq.push({necost,u}); dp[u]={necost,v}; } } } } cout<<dp[to].fi<<"\n"; while(true){ int x=to/w,y=to%w; int neto=dp[to].se; if(neto==-1) break; int nex=neto/w,ney=neto%w; char nech=a[nex][ney]; if(nech!='o'){ if(nex==x-1){ a[nex][ney]='v'; }else if(nex==x+1){ a[nex][ney]='^'; }else if(ney==y-1){ a[nex][ney]='>'; }else{ a[nex][ney]='<'; } } to=neto; } rep(i,h){ rep(j,w) cout<<a[i][j]; cout<<"\n"; } // return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...