Submission #492822

# Submission time Handle Problem Language Result Execution time Memory
492822 2021-12-09T06:06:48 Z niloyroot Mecho (IOI09_mecho) C++14
13 / 100
1000 ms 7356 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
#define ff first
#define ss second
const ll mod = 1000000007;

void solve(){
    ll n,s; cin>>n>>s;
    char c[n+1][n+1];
    vector<pl> hives;
    ll dis[n+1][n+1]; forp(i,1,n){forp(j,1,n){dis[i][j]=2000;}}
    pl start,end;
    forp(i,1,n){
        forp(j,1,n){
            cin>>c[i][j];
            if(c[i][j]=='H'){
                hives.pb({i,j});
                dis[i][j]=0;
            }
            if(c[i][j]=='T'){
                dis[i][j]=0;
            }
            if(c[i][j]=='M'){
                start.ff=i; start.ss=j;
            }
            if(c[i][j]=='D'){
                end.ff=i; end.ss=j;
            }
        }
    }
    bool vis[n+2][n+2];
    queue<pl> q;
    ll i,j;
    for(auto p:hives){
        memset(vis,0,sizeof(vis));
        forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
        q.push(p);
        vis[p.ff][p.ss]=1;
        while(!q.empty()){
            i=q.front().ff; j=q.front().ss;
            q.pop();
            if(!vis[i][j+1] && (c[i][j+1]!='T' && c[i][j+1]!='H')){
                vis[i][j+1]=1;
                dis[i][j+1]=min(dis[i][j+1],dis[i][j]+1);
                q.push({i,j+1});
            }
            if(!vis[i+1][j] && (c[i+1][j]!='T' && c[i+1][j]!='H')){
                vis[i+1][j]=1;
                dis[i+1][j]=min(dis[i+1][j],dis[i][j]+1);
                q.push({i+1,j});
            }
            if(!vis[i][j-1] && (c[i][j-1]!='T' && c[i][j-1]!='H')){
                vis[i][j-1]=1;
                dis[i][j-1]=min(dis[i][j-1],dis[i][j]+1);
                q.push({i,j-1});
            }
            if(!vis[i-1][j] && (c[i-1][j]!='T' && c[i-1][j]!='H')){
                vis[i-1][j]=1;
                dis[i-1][j]=min(dis[i-1][j],dis[i][j]+1);
                q.push({i-1,j});
            }
        }
    }

    ll l=0,r=n,mid,len;
    queue<pair<pl,ll>> qq;
    while(l<r){
        mid=(l+r+1)/2;
        memset(vis,0,sizeof(vis));
        forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
        qq.push({start,0});
        vis[start.ff][start.ss]=1;
        while(!qq.empty()){
            i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
            qq.pop();
            if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
                vis[i][j+1]=1;
                qq.push({{i,j+1},len+1});
            }
            if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
                vis[i+1][j]=1;
                qq.push({{i+1,j},len+1});
            }
            if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
                vis[i][j-1]=1;
                qq.push({{i,j-1},len+1});
            }
            if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
                vis[i-1][j]=1;
                qq.push({{i-1,j},len+1});
            }
        }
        if(vis[end.ff][end.ss]){
            l=mid;
        } else {
            r=mid-1;
        }
    }
    mid=0;
    memset(vis,0,sizeof(vis));
    forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
    qq.push({start,0});
    vis[start.ff][start.ss]=1;
    while(!qq.empty()){
        i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
        qq.pop();
        if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
            vis[i][j+1]=1;
            qq.push({{i,j+1},len+1});
        }
        if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
            vis[i+1][j]=1;
            qq.push({{i+1,j},len+1});
        }
        if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
            vis[i][j-1]=1;
            qq.push({{i,j-1},len+1});
        }
        if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
            vis[i-1][j]=1;
            qq.push({{i-1,j},len+1});
        }
    }
    if(vis[end.ff][end.ss]){
        cout<<l<<newl;
    } else {
        cout<<-1<<newl;
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1; //cin>>t;
    while(t--)solve();
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Correct 1 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Output isn't correct
5 Correct 0 ms 204 KB Output is correct
6 Incorrect 0 ms 204 KB Output isn't correct
7 Execution timed out 1084 ms 7244 KB Time limit exceeded
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Incorrect 1 ms 332 KB Output isn't correct
13 Incorrect 8 ms 348 KB Output isn't correct
14 Correct 9 ms 332 KB Output is correct
15 Incorrect 0 ms 204 KB Output isn't correct
16 Incorrect 0 ms 204 KB Output isn't correct
17 Incorrect 0 ms 204 KB Output isn't correct
18 Incorrect 0 ms 204 KB Output isn't correct
19 Incorrect 1 ms 204 KB Output isn't correct
20 Incorrect 0 ms 204 KB Output isn't correct
21 Incorrect 1 ms 388 KB Output isn't correct
22 Incorrect 1 ms 204 KB Output isn't correct
23 Incorrect 1 ms 332 KB Output isn't correct
24 Incorrect 1 ms 332 KB Output isn't correct
25 Incorrect 1 ms 332 KB Output isn't correct
26 Incorrect 1 ms 332 KB Output isn't correct
27 Incorrect 1 ms 332 KB Output isn't correct
28 Incorrect 1 ms 332 KB Output isn't correct
29 Incorrect 1 ms 316 KB Output isn't correct
30 Incorrect 1 ms 332 KB Output isn't correct
31 Incorrect 1 ms 332 KB Output isn't correct
32 Incorrect 1 ms 332 KB Output isn't correct
33 Incorrect 4 ms 1612 KB Output isn't correct
34 Incorrect 16 ms 1640 KB Output isn't correct
35 Correct 23 ms 1612 KB Output is correct
36 Incorrect 5 ms 1996 KB Output isn't correct
37 Incorrect 21 ms 2044 KB Output isn't correct
38 Correct 27 ms 2052 KB Output is correct
39 Incorrect 7 ms 2500 KB Output isn't correct
40 Incorrect 35 ms 2480 KB Output isn't correct
41 Correct 33 ms 2508 KB Output is correct
42 Incorrect 8 ms 3008 KB Output isn't correct
43 Incorrect 33 ms 2892 KB Output isn't correct
44 Correct 39 ms 3020 KB Output is correct
45 Incorrect 9 ms 3576 KB Output isn't correct
46 Incorrect 45 ms 3572 KB Output isn't correct
47 Correct 56 ms 3532 KB Output is correct
48 Incorrect 11 ms 4108 KB Output isn't correct
49 Incorrect 53 ms 4172 KB Output isn't correct
50 Correct 59 ms 4208 KB Output is correct
51 Incorrect 13 ms 4812 KB Output isn't correct
52 Incorrect 59 ms 4856 KB Output isn't correct
53 Correct 87 ms 4880 KB Output is correct
54 Incorrect 13 ms 5600 KB Output isn't correct
55 Incorrect 66 ms 5500 KB Output isn't correct
56 Correct 90 ms 5612 KB Output is correct
57 Incorrect 16 ms 6348 KB Output isn't correct
58 Incorrect 94 ms 6364 KB Output isn't correct
59 Correct 111 ms 6392 KB Output is correct
60 Incorrect 18 ms 7116 KB Output isn't correct
61 Incorrect 88 ms 7200 KB Output isn't correct
62 Correct 120 ms 7208 KB Output is correct
63 Execution timed out 1090 ms 7108 KB Time limit exceeded
64 Execution timed out 1087 ms 7108 KB Time limit exceeded
65 Execution timed out 1086 ms 7116 KB Time limit exceeded
66 Execution timed out 1088 ms 7116 KB Time limit exceeded
67 Execution timed out 1045 ms 7104 KB Time limit exceeded
68 Execution timed out 1085 ms 7220 KB Time limit exceeded
69 Execution timed out 1073 ms 7212 KB Time limit exceeded
70 Execution timed out 1077 ms 7108 KB Time limit exceeded
71 Execution timed out 1090 ms 7216 KB Time limit exceeded
72 Execution timed out 1084 ms 7220 KB Time limit exceeded
73 Execution timed out 1098 ms 7244 KB Time limit exceeded
74 Execution timed out 1085 ms 7244 KB Time limit exceeded
75 Execution timed out 1081 ms 7244 KB Time limit exceeded
76 Execution timed out 1087 ms 7244 KB Time limit exceeded
77 Execution timed out 1079 ms 7244 KB Time limit exceeded
78 Execution timed out 1094 ms 7240 KB Time limit exceeded
79 Execution timed out 1080 ms 7244 KB Time limit exceeded
80 Execution timed out 1092 ms 7252 KB Time limit exceeded
81 Execution timed out 1089 ms 7244 KB Time limit exceeded
82 Execution timed out 1089 ms 7244 KB Time limit exceeded
83 Execution timed out 1099 ms 7244 KB Time limit exceeded
84 Execution timed out 1090 ms 7244 KB Time limit exceeded
85 Execution timed out 1087 ms 7244 KB Time limit exceeded
86 Execution timed out 1089 ms 7244 KB Time limit exceeded
87 Execution timed out 1094 ms 7252 KB Time limit exceeded
88 Execution timed out 1092 ms 7128 KB Time limit exceeded
89 Execution timed out 1093 ms 7132 KB Time limit exceeded
90 Execution timed out 1092 ms 7240 KB Time limit exceeded
91 Execution timed out 1085 ms 7356 KB Time limit exceeded
92 Execution timed out 1091 ms 7244 KB Time limit exceeded