Submission #492826

# Submission time Handle Problem Language Result Execution time Memory
492826 2021-12-09T06:32:04 Z niloyroot Mecho (IOI09_mecho) C++14
20 / 100
1000 ms 6620 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});
            }
        }
    }
    dis[end.ff][end.ss]=2000;
    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 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Execution timed out 1098 ms 6620 KB Time limit exceeded
8 Correct 0 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 8 ms 332 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 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 0 ms 204 KB Output isn't correct
23 Incorrect 1 ms 336 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 356 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 11 ms 1484 KB Output isn't correct
34 Incorrect 14 ms 1516 KB Output isn't correct
35 Correct 22 ms 1484 KB Output is correct
36 Incorrect 14 ms 1868 KB Output isn't correct
37 Incorrect 20 ms 1996 KB Output isn't correct
38 Correct 24 ms 1868 KB Output is correct
39 Incorrect 17 ms 2252 KB Output isn't correct
40 Incorrect 26 ms 2300 KB Output isn't correct
41 Correct 30 ms 2228 KB Output is correct
42 Incorrect 20 ms 2764 KB Output isn't correct
43 Incorrect 32 ms 2764 KB Output isn't correct
44 Correct 37 ms 2772 KB Output is correct
45 Incorrect 27 ms 3276 KB Output isn't correct
46 Incorrect 41 ms 3276 KB Output isn't correct
47 Correct 46 ms 3288 KB Output is correct
48 Incorrect 32 ms 3748 KB Output isn't correct
49 Incorrect 48 ms 3832 KB Output isn't correct
50 Correct 56 ms 3860 KB Output is correct
51 Incorrect 40 ms 4444 KB Output isn't correct
52 Incorrect 57 ms 4444 KB Output isn't correct
53 Correct 78 ms 4472 KB Output is correct
54 Incorrect 44 ms 5068 KB Output isn't correct
55 Incorrect 65 ms 5116 KB Output isn't correct
56 Correct 85 ms 5124 KB Output is correct
57 Incorrect 49 ms 5708 KB Output isn't correct
58 Incorrect 77 ms 5812 KB Output isn't correct
59 Correct 99 ms 5836 KB Output is correct
60 Incorrect 58 ms 6572 KB Output isn't correct
61 Incorrect 88 ms 6468 KB Output isn't correct
62 Correct 119 ms 6588 KB Output is correct
63 Execution timed out 1092 ms 6592 KB Time limit exceeded
64 Execution timed out 1085 ms 6476 KB Time limit exceeded
65 Execution timed out 1098 ms 6484 KB Time limit exceeded
66 Execution timed out 1091 ms 6604 KB Time limit exceeded
67 Execution timed out 1087 ms 6604 KB Time limit exceeded
68 Execution timed out 1093 ms 6592 KB Time limit exceeded
69 Execution timed out 1088 ms 6488 KB Time limit exceeded
70 Execution timed out 1094 ms 6584 KB Time limit exceeded
71 Execution timed out 1082 ms 6584 KB Time limit exceeded
72 Execution timed out 1045 ms 6584 KB Time limit exceeded
73 Execution timed out 1094 ms 6576 KB Time limit exceeded
74 Execution timed out 1089 ms 6604 KB Time limit exceeded
75 Execution timed out 1094 ms 6604 KB Time limit exceeded
76 Execution timed out 1096 ms 6604 KB Time limit exceeded
77 Execution timed out 1080 ms 6604 KB Time limit exceeded
78 Execution timed out 1094 ms 6604 KB Time limit exceeded
79 Execution timed out 1084 ms 6604 KB Time limit exceeded
80 Execution timed out 1092 ms 6604 KB Time limit exceeded
81 Execution timed out 1095 ms 6528 KB Time limit exceeded
82 Execution timed out 1094 ms 6604 KB Time limit exceeded
83 Execution timed out 1090 ms 6604 KB Time limit exceeded
84 Execution timed out 1085 ms 6604 KB Time limit exceeded
85 Execution timed out 1087 ms 6604 KB Time limit exceeded
86 Execution timed out 1100 ms 6604 KB Time limit exceeded
87 Execution timed out 1088 ms 6604 KB Time limit exceeded
88 Execution timed out 1096 ms 6604 KB Time limit exceeded
89 Execution timed out 1091 ms 6616 KB Time limit exceeded
90 Execution timed out 1090 ms 6604 KB Time limit exceeded
91 Execution timed out 1094 ms 6604 KB Time limit exceeded
92 Execution timed out 1092 ms 6616 KB Time limit exceeded