# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
803129 | Liudas | Mecho (IOI09_mecho) | C++17 | 239 ms | 5216 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;
int main()
{
int N, S;
cin >> N >> S;
vector<string> _map(N + 2);
string t = string(N+2, 'T');
_map[0] = t;
_map[N+1] = t;
for(int i = 1; i <= N; i ++){
cin >> _map[i];
_map[i] = "T"+_map[i] + "T";
}
int sx, sy, ex, ey;
vector<pair<int, int>> bees;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j ++){
if(_map[i][j] == 'M'){
sx = j;
sy = i;
_map[i][j]='G';
}
if(_map[i][j] == 'D'){
ex = j;
ey = i;
}
if(_map[i][j] == 'H'){
_map[i][j] == 'T';
bees.push_back({i, j});
}
}
}
vector<int> dx = {1,-1,0,0}, dy = {0,0,1,-1};
int l = -1, r = N * N + 10;
bool gr = false;
while(l + 1 < r){
int mid = (l + r) / 2;
//cout << mid << " " << l << " " << r << endl;
vector<string> arr = _map;
vector<vector<int>> been(N + 2, vector<int>(N + 2));
queue<pair<int, int>> bee, bee2, moves;
moves.push({sy, sx});
for(auto[l, r] : bees){
bee.push({l, r});
}
bool reach = false;
for(int j = 0; j < mid; j ++){
while(!bee.empty()){
int a = bee.front().first, b = bee.front().second;
bee.pop();
for(int i = 0; i < 4; i ++){
if(arr[a+dy[i]][b+dx[i]] == 'G'){
arr[a+dy[i]][b+dx[i]] = 'T';
bee2.push({a+dy[i], b+dx[i]});
}
}
}
swap(bee, bee2);
}
int it = 0;
while(!moves.empty() && !reach){
while(!moves.empty()){
int a = moves.front().first, b = moves.front().second;
//cout << a << " " << b << " " << mid << endl;
if(been[a][b] >= (it + 1) * S)break;
if(arr[a][b]=='D'){reach = true;gr = true;break;}
moves.pop();
//cout << arr[a][b] << endl;
if(arr[a][b] == 'T')continue;
for(int i = 0; i < 4; i ++){
// cout << a + dy[i] << " " << endl;
if(!been[a+dy[i]][b+dx[i]] && arr[a+dy[i]][b+dx[i]] != 'T'){
been[a+dy[i]][b+dx[i]] = been[a][b] + 1;
moves.push({a+dy[i], b+dx[i]});
}
}
}
//cout << " ?" << endl;
while(!bee.empty()){
int a = bee.front().first, b = bee.front().second;
bee.pop();
for(int i = 0; i < 4; i ++){
if(arr[a+dy[i]][b+dx[i]] == 'G'){
arr[a+dy[i]][b+dx[i]] = 'T';
bee2.push({a+dy[i], b+dx[i]});
}
}
}
swap(bee, bee2);
it++;
}
if(reach){
l = mid;
}
else{
r = mid;
}
}
cout << l << endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |