# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
135481 | Dilshod_Imomov | Mecho (IOI09_mecho) | C++17 | 1092 ms | 17596 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 ll long long
# define fi first
# define se second
# define pb push_back
# define pf push_front
# define For(i, a, b) for( int i = a; i < b; i++ )
# define in insert
# define all(a) a.begin(),a.end()
# define pi pair < int, int >
# define DEBUG
# define readfile(file) freopen ( (file + ".in").c_str(), "r", stdin)
# define writefile(file) freopen ( (file + ".out").c_str(), "w", stdout)
# define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
# define LARGE (1e7)
using namespace std;
void Set_File( string file ){readfile(file);writefile(file);}
int n, s;
string m;
vector < string > forest;
vector < string > forest1;
int step[1000][1000];
int visited[1000][1000];
int x, ans = -1;
pi pos = { -1, -1 };
vector < pi > add = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
int valid ( int i, int j ){
if ( i >= 0 && i < n && j >= 0 && j < n && forest[i][j] != 'T' ) {
return 1;
}
return 0;
}
void spread_bees(){
vector < pi > pos;
For ( i, 0, n ){
For ( j, 0, n ){
if ( forest[i][j] == 'H' ) {
pos.pb({i, j});
}
}
}
For ( i, 0, pos.size() ){
For ( j, 0, 4 ){
pi neighbour = { pos[i].fi + add[j].fi, pos[i].se + add[j].se };
if ( valid( neighbour.fi, neighbour.se ) && forest[neighbour.fi][neighbour.se] == 'G' && forest[neighbour.fi][neighbour.se] != 'D' ){
forest[neighbour.fi][neighbour.se] = 'H';
}
}
}
}
int main(){
/// Author: _Dilshod_
speed;
cin >> n >> s;
For ( i, 0, n ){
cin >> m;
forest.pb(m);
forest1.pb(m);
if ( pos.fi == -1 ){
For ( j, 0, m.size() ){
if ( m[j] == 'M' ){
pos = {i, j};
}
}
}
}
while ( 1 ){
int ans1 = -1;
queue < pi > BFS;
BFS.push(pos);
forest = forest1;
For ( i, 0, 1000 ){
For ( j, 0, 1000 ){
step[i][j] = 0;
visited[i][j] = 0;
}
}
For ( i, 0, x ){
spread_bees();
}
int y = x;
int z = 0;
pi loc = pos;
visited[loc.fi][loc.se] = 1;
while ( !BFS.empty() ){
loc = BFS.front();
if ( step[loc.fi][loc.se] == 0 ){
spread_bees();
y++;
}
if ( step[loc.fi][loc.se] == 1 && z ){
z = 0;
spread_bees();
y++;
}
if ( forest[loc.fi][loc.se] == 'D' ){
ans1 = max( ans1, x );
break;
}
BFS.pop();
if ( step[loc.fi][loc.se] == s ){
if ( forest[loc.fi][loc.se] == 'H' ) {
continue;
}
step[loc.fi][loc.se] = 0;
}
For ( i, 0, 4 ){
pi to = { loc.fi + add[i].fi, loc.se + add[i].se };
if ( valid( to.fi, to.se ) && !visited[to.fi][to.se] ) {
visited[to.fi][to.se] = 1;
if ( step[loc.fi][loc.se] == 2 ) {
z++;
}
step[to.fi][to.se] = step[loc.fi][loc.se] + 1;
BFS.push(to);
}
}
if ( BFS.empty() && forest[loc.fi][loc.se] != 'D' ) {
ans1 = -1;
break;
}
}
x++;
ans = max( ans1, ans );
if ( ans1 == -1 ){
break;
}
}
cout << ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |