| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1083670 | hohemhein | Mecho (IOI09_mecho) | C++17 | 133 ms | 13392 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 ii pair<int,int>
#define iii pair<ii,int>
#define iiii pair<ii,ii>
#define f first
#define s second
#define pb push_back
#define pu push
#define ll long long
#define int long long
#define endl "\n"
#define all(x) (x).begin() , (x).end()
using namespace std;
const int nmax = 8e2+4;
const int oo = 1e12+3;
const ll mod = 1e9+7;
char m[nmax][nmax];
int bee[nmax][nmax];
int bear[nmax][nmax];
int mov[] = { 1,-1 };
void llenar(){
   for( int i = 0; i < nmax; i++ ){
    fill( bear[i], bear[i]+nmax, -1 );
    fill( bee[i], bee[i]+nmax, oo );
   }
}
void bfsBee( vector<ii>&aux, int n ){
  queue<ii>c;
  for( auto [i,j] : aux ){
        c.pu({i,j});
        bee[i][j] = 0;
  }
  while( !c.empty() ){
     auto [x,y] = c.front();
     c.pop();
     for( int i = 0; i < 2; i++ )
        if( x+mov[i] > 0 && x+mov[i] <= n && m[ x+mov[i] ][y] == 'G' && bee[ x+mov[i] ][y] == oo ){
           c.pu( {x+mov[i],y} );
           bee[ x+mov[i] ][y] = bee[x][y]+1;
     }
     for( int i = 0; i < 2; i++ )
        if( y+mov[i] > 0 && y+mov[i] <= n && m[x][y+mov[i]] == 'G' && bee[x][y+mov[i]] == oo ){
           c.pu( {x,y+mov[i]} );
           bee[x][y+mov[i]] = bee[x][y]+1;
     }
  }
}
void bfsBear( ii X, int n, int k ){
  priority_queue<iiii>c;
  bear[X.f][X.s] = bee[X.f][X.s] - 1;
  c.pu({ {bear[X.f][X.s],0} , X });
  while( !c.empty() ){
     auto [x,y] = c.top().s;
     auto [val,pe] = c.top().f;
     pe*=-1;
     c.pop();
     for( int i = 0; i < 2; i++ ){
        int newValue = min( val , bee[x+mov[i]][y] - pe/k - 1 );
        if( x+mov[i] > 0 && x+mov[i] <= n && m[ x+mov[i] ][y] != 'T' && bear[ x+mov[i] ][y] < newValue && bee[ x+mov[i] ][y] > pe/k ){
           c.pu( {  { newValue, -(pe+1) }, {x+mov[i],y} } );
           bear[ x+mov[i] ][y] = newValue;
     }
    }
     for( int i = 0; i < 2; i++ ){
        int newValue = min( bear[x][y] , bee[x][y+mov[i]] - pe/k -1 );
        if( y+mov[i] > 0 && y+mov[i] <= n && m[x][y+mov[i]] != 'T'&& bear[x][y+mov[i]] < newValue && bee[x][y+mov[i]] > pe/k ){
           c.pu( { {newValue,-(pe+1)},  {x,y+mov[i]} } );
           bear[x][y+mov[i]] = newValue;
     }
    }
  }
}
int32_t main()
{
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie();
    //cout << fixed << setprecision(4);
    /*ifstream cin ("atlarge.in");
    ofstream cout ("atlarge.out");*/
    llenar();
    int n, k; cin >> n >> k;
    ii init, meta;
    vector<ii>aux;
    for( int i = 1; i <= n; i++ )
        for( int j = 1; j <= n; j++ ){
            cin >> m[i][j];
            if( m[i][j] == 'H' )aux.pb({i,j});
            else if( m[i][j] == 'M' )init = {i,j};
            else if( m[i][j] == 'D' )meta = {i,j};
        }
     m[init.f][init.s] = 'G';
     bfsBee( aux, n );
     bfsBear( init, n, k);
    /* for( int i = 1; i <= n; i++ ){
        for( int j = 1; j <= n; j++ )
        cout << bear[i][j] <<" ";
        cout <<"\n";
     }*/
     cout << bear[meta.f][meta.s] <<"\n";
    return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|---|---|---|---|
| Fetching results... | ||||
