Submission #870684

# Submission time Handle Problem Language Result Execution time Memory
870684 2023-11-08T19:58:20 Z Essa2006 Mecho (IOI09_mecho) C++17
100 / 100
192 ms 6484 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
const int inf=1e9;
int n, s, Md, can;
array<int, 2>Str, End;
vector<string>A;
vector<vector<int>>Pri;
queue<array<int, 2>>q, qu;
int dx[]={0, 0, 1, -1};
int dy[]={1, -1, 0, 0}; 
 
void pre(){
    A.clear(), Pri.clear();
    A.resize(n), Pri.resize(n, vector<int>(n, inf));
}
 
bool valid(int i, int j){
    return !(i<0 || i>=n || j<0 || j>=n);
}
 
void bfs(){
    while(!q.empty()){
        array<int, 2>u=q.front();
        q.pop();
        int i=u[0], j=u[1];
        for(int f=0;f<4;f++){
            int ii=i+dx[f], jj=j+dy[f];
            if(valid(ii, jj) && A[ii][jj]!='D' && Pri[i][j]+1<Pri[ii][jj])
                Pri[ii][jj]=Pri[i][j]+1, q.push({ii, jj});
        }
    }
}
 
void check(){
    vector<vector<int>>Dis(n, vector<int>(n, inf));
    Dis[Str[0]][Str[1]]=0;
    while(!qu.empty()){
        array<int, 2>u=qu.front();
        qu.pop();
        int i=u[0], j=u[1];
        if(Md+Dis[i][j]/s>=Pri[i][j]){
            continue;
        }
        if(i==End[0] && j==End[1]){
            can=1;
            continue;
        }
        
        for(int f=0;f<4;f++){
            int ii=i+dx[f], jj=j+dy[f];
            if(valid(ii, jj) && Dis[i][j]+1<Dis[ii][jj]){
                Dis[ii][jj]=Dis[i][j]+1, qu.push({ii, jj});
            }
        }
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>s;
    pre();
    for(int i=0;i<n;i++){
        cin>>A[i];
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(A[i][j]=='H')
                q.push({i, j}), Pri[i][j]=0;
            else if(A[i][j]=='M')
                Str[0]=i, Str[1]=j;
            else if(A[i][j]=='D')
                End[0]=i, End[1]=j;
            else if(A[i][j]=='T')
                Pri[i][j]=0;
        }
    }
    bfs();
    int l=0, r=n*n, ans=-1;
    while(l<=r){
        int md=(l+r)/2;
        Md=md;
        can=0;
        qu.push({Str[0], Str[1]});
        check();
        if(can)
            ans=md, l=md+1;
        else
            r=md-1;
    }
    assert(Pri[End[0]][End[1]]==inf);
    cout<<ans; 
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 67 ms 6268 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 600 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 5 ms 1572 KB Output is correct
34 Correct 6 ms 1572 KB Output is correct
35 Correct 18 ms 1612 KB Output is correct
36 Correct 6 ms 1884 KB Output is correct
37 Correct 7 ms 1884 KB Output is correct
38 Correct 23 ms 1932 KB Output is correct
39 Correct 8 ms 2268 KB Output is correct
40 Correct 9 ms 2356 KB Output is correct
41 Correct 36 ms 2264 KB Output is correct
42 Correct 11 ms 2728 KB Output is correct
43 Correct 11 ms 2732 KB Output is correct
44 Correct 42 ms 2752 KB Output is correct
45 Correct 12 ms 3432 KB Output is correct
46 Correct 14 ms 3192 KB Output is correct
47 Correct 45 ms 3256 KB Output is correct
48 Correct 14 ms 3712 KB Output is correct
49 Correct 16 ms 3780 KB Output is correct
50 Correct 62 ms 3776 KB Output is correct
51 Correct 16 ms 4328 KB Output is correct
52 Correct 19 ms 4184 KB Output is correct
53 Correct 74 ms 4276 KB Output is correct
54 Correct 20 ms 4964 KB Output is correct
55 Correct 22 ms 4904 KB Output is correct
56 Correct 81 ms 4912 KB Output is correct
57 Correct 22 ms 5576 KB Output is correct
58 Correct 25 ms 5824 KB Output is correct
59 Correct 102 ms 5548 KB Output is correct
60 Correct 26 ms 6272 KB Output is correct
61 Correct 28 ms 6264 KB Output is correct
62 Correct 111 ms 6228 KB Output is correct
63 Correct 101 ms 6256 KB Output is correct
64 Correct 192 ms 6220 KB Output is correct
65 Correct 145 ms 6244 KB Output is correct
66 Correct 119 ms 6268 KB Output is correct
67 Correct 111 ms 6240 KB Output is correct
68 Correct 48 ms 6360 KB Output is correct
69 Correct 50 ms 6252 KB Output is correct
70 Correct 38 ms 6288 KB Output is correct
71 Correct 42 ms 6292 KB Output is correct
72 Correct 33 ms 6304 KB Output is correct
73 Correct 42 ms 6316 KB Output is correct
74 Correct 48 ms 6292 KB Output is correct
75 Correct 58 ms 6284 KB Output is correct
76 Correct 53 ms 6320 KB Output is correct
77 Correct 63 ms 6484 KB Output is correct
78 Correct 61 ms 6236 KB Output is correct
79 Correct 43 ms 6292 KB Output is correct
80 Correct 52 ms 6284 KB Output is correct
81 Correct 61 ms 6316 KB Output is correct
82 Correct 51 ms 6272 KB Output is correct
83 Correct 64 ms 6268 KB Output is correct
84 Correct 62 ms 6432 KB Output is correct
85 Correct 62 ms 6276 KB Output is correct
86 Correct 75 ms 6300 KB Output is correct
87 Correct 69 ms 6288 KB Output is correct
88 Correct 63 ms 6280 KB Output is correct
89 Correct 64 ms 6232 KB Output is correct
90 Correct 73 ms 6256 KB Output is correct
91 Correct 65 ms 6260 KB Output is correct
92 Correct 73 ms 6280 KB Output is correct