Submission #1083698

# Submission time Handle Problem Language Result Execution time Memory
1083698 2024-09-03T20:35:05 Z hohemhein Mecho (IOI09_mecho) C++17
100 / 100
128 ms 7840 KB
#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];
bool bear[nmax][nmax];

int mov[] = { 1,-1 };


void llenar(){

   for( int i = 0; i < nmax; i++ ){
    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, int op ){
  for( int i = 0; i <= n+1; i++ ){
    fill( bear[i], bear[i]+n+2, false );
   }

  queue<iiii>c;
  bear[X.f][X.s] = true;
  c.pu({ X, {op,1} });

  while( !c.empty() ){
     auto [x,y] = c.front().f;
     auto add = c.front().s.s;
     auto pe = c.front().s.f + ( ( (add%k) == 0)?1:0 );
     c.pop();

     for( int i = 0; i < 2; i++ ){
        if( x+mov[i] > 0 && x+mov[i] <= n && m[ x+mov[i] ][y] != 'T' && !bear[ x+mov[i] ][y]  && bee[ x+mov[i] ][y] > pe ){
           c.pu( { {x+mov[i],y} , {pe,add+1} } );
           bear[ x+mov[i] ][y] = true;
     }
    }

     for( int i = 0; i < 2; i++ ){
        if( y+mov[i] > 0 && y+mov[i] <= n && m[x][y+mov[i]] != 'T'&& !bear[x][y+mov[i]] && bee[x][y+mov[i]] > pe ){
           c.pu( { {x,y+mov[i]}, {pe,add+1} } );
           bear[x][y+mov[i]] = true;
     }
    }

  }

}



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 );

     int l = 0,r = bee[init.f][init.s];
     while( r-l > 1 ){
       int m = (r+l)>>1;
       bfsBear( init, n, k,m);
       if( bear[meta.f][meta.s] )l = m;
       else r = m;


    /* cout << m <<"\n";
     for( int i = 1; i <= n; i++ ){
        for( int j = 1; j <= n; j++ )
        cout << bear[i][j] <<" ";
        cout <<"\n";
     }
       cout << "\n\n";*/
     }
     bfsBear( init, n, k, l);

     cout << ( ( bear[meta.f][meta.s] )? l : -1 ) <<"\n";


    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 5468 KB Output is correct
2 Correct 2 ms 5468 KB Output is correct
3 Correct 2 ms 5468 KB Output is correct
4 Correct 3 ms 5472 KB Output is correct
5 Correct 2 ms 5468 KB Output is correct
6 Correct 2 ms 5468 KB Output is correct
7 Correct 53 ms 7536 KB Output is correct
8 Correct 3 ms 5468 KB Output is correct
9 Correct 2 ms 5464 KB Output is correct
10 Correct 2 ms 5468 KB Output is correct
11 Correct 2 ms 5468 KB Output is correct
12 Correct 2 ms 5468 KB Output is correct
13 Correct 2 ms 5468 KB Output is correct
14 Correct 2 ms 5468 KB Output is correct
15 Correct 2 ms 5468 KB Output is correct
16 Correct 2 ms 5468 KB Output is correct
17 Correct 3 ms 5464 KB Output is correct
18 Correct 2 ms 5468 KB Output is correct
19 Correct 3 ms 5468 KB Output is correct
20 Correct 2 ms 5468 KB Output is correct
21 Correct 2 ms 5468 KB Output is correct
22 Correct 2 ms 5468 KB Output is correct
23 Correct 2 ms 5468 KB Output is correct
24 Correct 3 ms 5512 KB Output is correct
25 Correct 2 ms 5452 KB Output is correct
26 Correct 3 ms 5464 KB Output is correct
27 Correct 2 ms 5468 KB Output is correct
28 Correct 3 ms 5468 KB Output is correct
29 Correct 4 ms 5468 KB Output is correct
30 Correct 2 ms 5468 KB Output is correct
31 Correct 3 ms 5468 KB Output is correct
32 Correct 3 ms 5468 KB Output is correct
33 Correct 5 ms 5976 KB Output is correct
34 Correct 5 ms 5980 KB Output is correct
35 Correct 17 ms 6192 KB Output is correct
36 Correct 6 ms 6232 KB Output is correct
37 Correct 6 ms 6236 KB Output is correct
38 Correct 23 ms 6328 KB Output is correct
39 Correct 7 ms 6236 KB Output is correct
40 Correct 7 ms 6436 KB Output is correct
41 Correct 31 ms 6236 KB Output is correct
42 Correct 8 ms 6488 KB Output is correct
43 Correct 8 ms 6492 KB Output is correct
44 Correct 28 ms 6476 KB Output is correct
45 Correct 9 ms 6492 KB Output is correct
46 Correct 10 ms 6688 KB Output is correct
47 Correct 41 ms 6700 KB Output is correct
48 Correct 10 ms 6744 KB Output is correct
49 Correct 14 ms 7000 KB Output is correct
50 Correct 42 ms 6748 KB Output is correct
51 Correct 12 ms 6748 KB Output is correct
52 Correct 12 ms 6968 KB Output is correct
53 Correct 63 ms 6748 KB Output is correct
54 Correct 15 ms 7000 KB Output is correct
55 Correct 15 ms 7004 KB Output is correct
56 Correct 60 ms 7108 KB Output is correct
57 Correct 15 ms 7256 KB Output is correct
58 Correct 18 ms 7256 KB Output is correct
59 Correct 68 ms 7260 KB Output is correct
60 Correct 16 ms 7260 KB Output is correct
61 Correct 17 ms 7260 KB Output is correct
62 Correct 79 ms 7260 KB Output is correct
63 Correct 67 ms 7424 KB Output is correct
64 Correct 128 ms 7412 KB Output is correct
65 Correct 110 ms 7252 KB Output is correct
66 Correct 93 ms 7404 KB Output is correct
67 Correct 79 ms 7256 KB Output is correct
68 Correct 35 ms 7252 KB Output is correct
69 Correct 35 ms 7256 KB Output is correct
70 Correct 30 ms 7252 KB Output is correct
71 Correct 29 ms 7260 KB Output is correct
72 Correct 24 ms 7256 KB Output is correct
73 Correct 26 ms 7768 KB Output is correct
74 Correct 40 ms 7732 KB Output is correct
75 Correct 45 ms 7772 KB Output is correct
76 Correct 41 ms 7768 KB Output is correct
77 Correct 41 ms 7772 KB Output is correct
78 Correct 51 ms 7768 KB Output is correct
79 Correct 38 ms 7772 KB Output is correct
80 Correct 38 ms 7840 KB Output is correct
81 Correct 46 ms 7772 KB Output is correct
82 Correct 45 ms 7768 KB Output is correct
83 Correct 57 ms 7840 KB Output is correct
84 Correct 47 ms 7760 KB Output is correct
85 Correct 61 ms 7772 KB Output is correct
86 Correct 52 ms 7764 KB Output is correct
87 Correct 50 ms 7772 KB Output is correct
88 Correct 48 ms 7516 KB Output is correct
89 Correct 51 ms 7512 KB Output is correct
90 Correct 54 ms 7516 KB Output is correct
91 Correct 53 ms 7516 KB Output is correct
92 Correct 69 ms 7516 KB Output is correct