# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
621421 | abcisosm5 | Mecho (IOI09_mecho) | C++17 | 331 ms | 2132 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 pi pair<int, int>
#define f first
#define s second
int N, S;
const int MN = 800;
char grid[MN][MN] = {{}};
vector<pi> hives;
pi M; pi D;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void print(char (&v)[MN][MN]){
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(v[i][j] < 1) cout << ".";
else cout << v[i][j];
} cout << endl;
} cout << endl;
}
void movebees(queue<pair<pi,int>> (&b), char (&v)[MN][MN], int t){
if(b.empty()) return;
int start = b.front().s;
for(int i = 0; i < t; i++){
while(!b.empty() && b.front().s <= start + i){
pair<pi, int> x = b.front(); b.pop();
pi p = x.f; int index = x.s;
for(int d = 0; d < 4; d++){
int nx = p.f + dx[d]; int ny = p.s + dy[d];
if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
if(v[nx][ny] != 'B' && grid[nx][ny] == 'G'){
v[nx][ny] = 'B';
b.push({{nx, ny}, index+1});
}
}
}
}
}
bool movemecho(queue<pair<pi, int>> (&m), char (&v)[MN][MN]){
if(m.empty()) return false;
int start = m.front().s;
for(int i = 0; i < S; i++){
while(!m.empty() && m.front().s <= start + i){
pair<pi, int> x = m.front(); m.pop();
pi p = x.f; int index = x.s;
if(v[p.f][p.s] == 'B') continue; //invalidate positions that are now occupied by bees
for(int d = 0; d < 4; d++){
int nx = p.f + dx[d]; int ny = p.s + dy[d];
if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
if(!v[nx][ny] && grid[nx][ny] != 'T'){
v[nx][ny] = 'M';
if(grid[nx][ny] == 'D') return true;
m.push({{nx, ny}, index+1});
}
}
}
}
return false;
}
bool works(int t){ //O(N^2)
char visited[MN][MN] = {{}};
queue<pair<pi, int>> bees;
for(pi p : hives){
bees.push({p,0});
visited[p.first][p.second] = 'B';
}
movebees(bees, visited, t);
queue<pair<pi, int>> mecho; mecho.push({M,0}); visited[M.f][M.s] = 'M';
for(int i = 0; i < N*N; i++){
//first, move mecho S steps
if(mecho.empty()) break;
bool success = movemecho(mecho, visited);
if(success) return true;
//then, move bees 1 step
movebees(bees, visited, 1);
}
return false;
}
int main(){
//freopen("../input.in", "r", stdin);
//freopen("../output.out", "w", stdout);
cin.tie(0)->sync_with_stdio(0);
cin >> N >> S;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
cin >> grid[i][j];
if(grid[i][j] == 'M') M = {i, j};
if(grid[i][j] == 'D') D = {i, j};
if(grid[i][j] == 'H') hives.push_back({i,j});
}
}
int lo = -1; int hi = N*N; //amount of head start to bees
while(lo < hi){ // in total, O(N^2 log N)
int mid = lo + (hi - lo + 1) / 2;
if(works(mid)){
lo = mid;
} else {
hi = mid - 1;
}
}
cout << lo;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |