# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
74664 | arman_ferdous | Mecho (IOI09_mecho) | C++17 | 682 ms | 66560 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 xx first
#define yy second
typedef pair<int,int> ii;
const int N = 888;
const int inf = 2e9;
int n, step, d[N][N], dist[N][N];
char s[N][N];
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
queue<ii> q;
ii mecho, home;
bool can(int t) {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
dist[i][j] = inf;
q.push(mecho);
dist[mecho.xx][mecho.yy] = 0;
while(!q.empty()) {
ii u = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
ii v = {u.xx + dx[i], u.yy + dy[i]};
if(min(v.xx,v.yy) < 0 || n <= max(v.xx,v.yy)) continue;
char cell = s[v.xx][v.yy];
if(cell == 'T' || cell == 'M' || cell == 'H') continue;
int arrival = ceil((double)(dist[u.xx][u.yy] + 1.) / step) + t;
if(arrival > d[v.xx][v.yy] || dist[u.xx][u.yy] + 1 > dist[v.xx][v.yy]) continue;
dist[v.xx][v.yy] = dist[u.xx][u.yy] + 1;
q.push(v);
}
}
return dist[home.xx][home.yy] != inf;
}
int main() {
scanf("%d %d", &n, &step);
for(int i = 0; i < n; i++)
scanf(" %s", s[i]);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
d[i][j] = inf;
if(s[i][j] == 'M') mecho = {i,j};
else if(s[i][j] == 'D') home = {i,j};
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(s[i][j] == 'H') {
q.push({i,j});
d[i][j] = 0;
}
while(!q.empty()) {
ii u = q.front(); q.pop();
for(int i = 0; i < 4; i++) {
ii v = {u.xx + dx[i], u.yy + dy[i]};
if(min(v.xx,v.yy) < 0 || n <= max(v.xx,v.yy)) continue;
char cell = s[v.xx][v.yy];
if(cell == 'T' || cell == 'D' || cell == 'H') continue;
if(d[v.xx][v.yy] < d[u.xx][u.yy] + 1) continue;
d[v.xx][v.yy] = d[u.xx][u.yy] + 1;
q.push(v);
}
}
int lo = 0, hi = n+n, ans = -1;
while(lo <= hi) {
int mid = (lo + hi) >> 1;
if(can(mid)) ans = mid, lo = mid+1;
else hi = mid-1;
} printf("%d\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |