# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
977567 | farica | Mecho (IOI09_mecho) | C++14 | 177 ms | 6756 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>
#define int long long
using namespace std;
const int MAX_N = 4e5 + 5;
int n, s, dist[805][805], sx, sy, ex, ey;
string grid[805];
bool test(int m) {
queue<pair<int, pair<int,int>>>q;
q.push({0, {sx, sy}});
vector<vector<bool>>visited(n, vector<bool>(n, 0));
while(!q.empty()) {
int cnt = q.front().first, x = q.front().second.first, y = q.front().second.second;
q.pop();
if(visited[x][y]) continue;
visited[x][y] = 1;
++cnt;
int cost = cnt / s + (cnt % s > 0 ? 1 : 0) + m;
if(x && dist[x-1][y] >= cost && grid[x-1][y] == 'G') q.push({cnt, {x-1, y}});
if(y && dist[x][y-1] >= cost && grid[x][y-1] == 'G') q.push({cnt, {x, y-1}});
if(x+1<n && dist[x+1][y] >= cost && grid[x+1][y] == 'G') q.push({cnt, {x+1, y}});
if(y+1<n && dist[x][y+1] >= cost && grid[x][y+1] == 'G') q.push({cnt, {x, y+1}});
}
if(ex && visited[ex-1][ey]) return true;
if(ey && visited[ex][ey-1]) return true;
if(ex+1<n && visited[ex+1][ey]) return true;
if(ey+1<n && visited[ex][ey+1]) return true;
return false;
}
void solve() {
cin >> n >> s;
queue<pair<int,int>>q;
for(int i=0; i<n; ++i) {
cin >> grid[i];
for(int j=0; j<n; ++j) {
dist[i][j] = -1;
if(grid[i][j] == 'H') {
q.push({i, j});
dist[i][j] = 0;
} else if(grid[i][j] == 'M') sx=i, sy=j;
else if(grid[i][j] == 'D') ex=i, ey=j;
}
}
while(!q.empty()) {
int i = q.front().first, j = q.front().second;
q.pop();
if(i && dist[i-1][j] == -1 && grid[i-1][j] == 'G') {
dist[i-1][j] = dist[i][j] + 1;
q.push({i-1, j});
}
if(j && dist[i][j-1] == -1 && grid[i][j-1] == 'G') {
dist[i][j-1] = dist[i][j] + 1;
q.push({i, j-1});
}
if(i<n-1 && dist[i+1][j] == -1 && grid[i+1][j] == 'G') {
dist[i+1][j] = dist[i][j] + 1;
q.push({i+1, j});
}
if(j<n-1 && dist[i][j+1] == -1 && grid[i][j+1] == 'G') {
dist[i][j+1] = dist[i][j] + 1;
q.push({i, j+1});
}
}
if(!test(0)) {
cout << -1 << endl;
return;
}
int l=0, r=n*n+5, mid;
while(l<=r) {
mid = (l+r)/2;
if(test(mid)) l = mid + 1;
else r = mid - 1;
}
cout << l - 1 << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |