Submission #492824

# Submission time Handle Problem Language Result Execution time Memory
492824 2021-12-09T06:10:59 Z niloyroot Mecho (IOI09_mecho) C++14
13 / 100
1000 ms 6732 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]=INT_MAX;}}
    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 0 ms 204 KB Output is correct
4 Incorrect 0 ms 204 KB Output isn't correct
5 Correct 1 ms 204 KB Output is correct
6 Incorrect 1 ms 204 KB Output isn't correct
7 Execution timed out 1091 ms 6600 KB Time limit exceeded
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Incorrect 0 ms 332 KB Output isn't correct
13 Incorrect 9 ms 332 KB Output isn't correct
14 Correct 10 ms 352 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 0 ms 204 KB Output isn't correct
20 Incorrect 0 ms 204 KB Output isn't correct
21 Incorrect 1 ms 204 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 332 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 12 ms 1484 KB Output isn't correct
34 Incorrect 17 ms 1484 KB Output isn't correct
35 Correct 20 ms 1536 KB Output is correct
36 Incorrect 15 ms 1888 KB Output isn't correct
37 Incorrect 21 ms 1892 KB Output isn't correct
38 Correct 24 ms 1904 KB Output is correct
39 Incorrect 18 ms 2228 KB Output isn't correct
40 Incorrect 26 ms 2252 KB Output isn't correct
41 Correct 30 ms 2296 KB Output is correct
42 Incorrect 24 ms 2764 KB Output isn't correct
43 Incorrect 33 ms 2764 KB Output isn't correct
44 Correct 38 ms 2780 KB Output is correct
45 Incorrect 29 ms 3260 KB Output isn't correct
46 Incorrect 41 ms 3240 KB Output isn't correct
47 Correct 47 ms 3288 KB Output is correct
48 Incorrect 35 ms 3788 KB Output isn't correct
49 Incorrect 52 ms 3844 KB Output isn't correct
50 Correct 54 ms 3856 KB Output is correct
51 Incorrect 38 ms 4428 KB Output isn't correct
52 Incorrect 59 ms 4456 KB Output isn't correct
53 Correct 79 ms 4568 KB Output is correct
54 Incorrect 45 ms 5096 KB Output isn't correct
55 Incorrect 67 ms 5120 KB Output isn't correct
56 Correct 97 ms 5240 KB Output is correct
57 Incorrect 55 ms 5828 KB Output isn't correct
58 Incorrect 79 ms 5832 KB Output isn't correct
59 Correct 106 ms 5840 KB Output is correct
60 Incorrect 61 ms 6572 KB Output isn't correct
61 Incorrect 89 ms 6476 KB Output isn't correct
62 Correct 112 ms 6476 KB Output is correct
63 Execution timed out 1092 ms 6476 KB Time limit exceeded
64 Execution timed out 1087 ms 6476 KB Time limit exceeded
65 Execution timed out 1099 ms 6476 KB Time limit exceeded
66 Execution timed out 1079 ms 6476 KB Time limit exceeded
67 Execution timed out 1085 ms 6484 KB Time limit exceeded
68 Execution timed out 1087 ms 6604 KB Time limit exceeded
69 Execution timed out 1090 ms 6584 KB Time limit exceeded
70 Execution timed out 1095 ms 6476 KB Time limit exceeded
71 Execution timed out 1089 ms 6584 KB Time limit exceeded
72 Execution timed out 1086 ms 6584 KB Time limit exceeded
73 Execution timed out 1089 ms 6640 KB Time limit exceeded
74 Execution timed out 1092 ms 6604 KB Time limit exceeded
75 Execution timed out 1088 ms 6604 KB Time limit exceeded
76 Execution timed out 1090 ms 6604 KB Time limit exceeded
77 Execution timed out 1093 ms 6604 KB Time limit exceeded
78 Execution timed out 1092 ms 6604 KB Time limit exceeded
79 Execution timed out 1083 ms 6604 KB Time limit exceeded
80 Execution timed out 1083 ms 6528 KB Time limit exceeded
81 Execution timed out 1082 ms 6604 KB Time limit exceeded
82 Execution timed out 1087 ms 6604 KB Time limit exceeded
83 Execution timed out 1088 ms 6604 KB Time limit exceeded
84 Execution timed out 1083 ms 6604 KB Time limit exceeded
85 Execution timed out 1089 ms 6604 KB Time limit exceeded
86 Execution timed out 1081 ms 6600 KB Time limit exceeded
87 Execution timed out 1091 ms 6732 KB Time limit exceeded
88 Execution timed out 1089 ms 6496 KB Time limit exceeded
89 Execution timed out 1087 ms 6500 KB Time limit exceeded
90 Execution timed out 1080 ms 6616 KB Time limit exceeded
91 Execution timed out 1097 ms 6604 KB Time limit exceeded
92 Execution timed out 1094 ms 6604 KB Time limit exceeded