Submission #956816

#TimeUsernameProblemLanguageResultExecution timeMemory
956816DeltaStructMecho (IOI09_mecho)C++17
4 / 100
300 ms12176 KiB
#include <bits/stdc++.h> using namespace std; int main(){ #define int long long int n,m,inf=numeric_limits<int>::max(); cin >> n >> m; int sx,sy,gx,gy; vector<string> A(n); for (auto& a:A) cin >> a; queue<pair<int,int>> que; vector hd(n,vector<int>(n,inf)); for (int i(0);i < n;++i) for (int k(0);k < n;++k){ if (A[i][k]=='M') sx=i,sy=k; if (A[i][k]=='D') gx=i,gy=k; if (A[i][k]=='H') que.emplace(i,k),hd[i][k] = 0; } bool psf = true; auto can = [&psf,&A,&n](int x,int y){ if (x==-1||x==n||y==-1||y==n) return false; if (A[x][y]=='T') return false; if (psf&&A[x][y]=='D') return false; return true; }; while(!que.empty()){ auto [i,k] = que.front(); que.pop(); if (can(i+1,k)&&hd[i+1][k]==inf) hd[i+1][k]=hd[i][k]+m,que.emplace(i+1,k); if (can(i-1,k)&&hd[i-1][k]==inf) hd[i-1][k]=hd[i][k]+m,que.emplace(i-1,k); if (can(i,k+1)&&hd[i][k+1]==inf) hd[i][k+1]=hd[i][k]+m,que.emplace(i,k+1); if (can(i,k-1)&&hd[i][k-1]==inf) hd[i][k-1]=hd[i][k]+m,que.emplace(i,k-1); } psf = false; int l = -1,r = 1e9,mid; while(abs(l-r)>1){ mid = (l+r)/2; vector dist(n,vector<int>(n,inf)); dist[sx][sy] = l,que.emplace(sx,sy); while(!que.empty()){ auto [i,k] = que.front(); que.pop(); if (can(i+1,k)&&dist[i][k]+1<hd[i+1][k]&&dist[i+1][k]==inf) dist[i+1][k] = dist[i][k]+1,que.emplace(i+1,k); if (can(i-1,k)&&dist[i][k]+1<hd[i-1][k]&&dist[i-1][k]==inf) dist[i-1][k] = dist[i][k]+1,que.emplace(i-1,k); if (can(i,k+1)&&dist[i][k]+1<hd[i][k+1]&&dist[i][k+1]==inf) dist[i][k+1] = dist[i][k]+1,que.emplace(i,k+1); if (can(i,k-1)&&dist[i][k]+1<hd[i][k-1]&&dist[i][k-1]==inf) dist[i][k-1] = dist[i][k]+1,que.emplace(i,k-1); } if (dist[gx][gy]==inf) r = mid; else l = mid; } cout << l << endl; }

Compilation message (stderr)

mecho.cpp: In function 'int main()':
mecho.cpp:38:20: warning: 'gy' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |     if (dist[gx][gy]==inf) r = mid;
      |                    ^
mecho.cpp:38:16: warning: 'gx' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |     if (dist[gx][gy]==inf) r = mid;
      |                ^
#Verdict Execution timeMemoryGrader output
Fetching results...