# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
733168 | penguin133 | Mecho (IOI09_mecho) | C++17 | 167 ms | 14260 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
queue <pi> q;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
char G[1005][1005];
int dist[1005][1005], dist2[1005][1005];
void solve(){
int n, s; cin >> n >> s;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)cin >> G[i][j];
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j] = 1e15;
if(G[i][j] == 'H'){
dist[i][j] = 0;
q.push({i, j});
}
}
}
while(!q.empty()){
int x = q.front().fi, y = q.front().se;
q.pop();
for(int i=0;i<4;i++){
int x1 = x + dx[i], y1 = y + dy[i];
if(x1 < 1 || x1 > n || y1 < 1 || y1 > n)continue;
if(G[x1][y1] == 'T')continue;
if(dist[x1][y1] > dist[x][y] + 1)dist[x1][y1] = dist[x][y] + 1, q.push({x1, y1});
}
}
int s1, s2, e1, e2;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(G[i][j] == 'M')s1 = i, s2 = j;
if(G[i][j] == 'D')e1 = i, e2 = j;
}
}
int lo = 0, hi = (int)2e6, ans = hi;
while(lo <= hi){
int mid = (lo + hi) >> 1;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)dist2[i][j] = 1e15;
dist2[s1][s2] = mid * s;
q.push({s1, s2});
while(!q.empty()){
int x = q.front().fi, y = q.front().se;
q.pop();
for(int i=0;i<4;i++){
int x1 = x + dx[i], y1 = y + dy[i];
if(x1 < 1 || x1 > n || y1 < 1 || y1 > n)continue;
if(G[x1][y1] == 'T')continue;
if(dist2[x1][y1] > dist2[x][y] + 1 && dist2[x][y] + 1 < s * dist[x1][y1])dist2[x1][y1] = dist2[x][y] + 1, q.push({x1, y1});
}
}
if(dist2[e1][e2] == 1e15)hi = mid - 1;
else lo = mid + 1, ans = mid;
}
cout << (ans == 2e6 ? -1 : ans);
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |