Submission #335753

# Submission time Handle Problem Language Result Execution time Memory
335753 2020-12-13T20:33:35 Z Christopher_Rdz Mecho (IOI09_mecho) C++14
100 / 100
256 ms 9196 KB
#include <bits/stdc++.h>
using namespace std;
struct node{
    int i;
    int j;
    node(){}
    node(int I,int J){
        i=I;
        j=J;
    }
};
queue<node> Q;
char forest[805][805];
int bees[805][805];
int dist[805][805];
int energy[805][805];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,s;

void bfsBees(){    for (int i = 1; i <= n; i++){        for (int j = 1; j <= n; j++){            bees[i][j]=INT_MAX;
            if(forest[i][j]=='H'){
                Q.push(node(i,j));
                bees[i][j]=0;            }       }    }
    while(!Q.empty()){
        node u=Q.front();
        Q.pop();
        for(int mov=0;mov<4;mov++){
            int i=u.i+dx[mov];
            int j=u.j+dy[mov];
            if(i<1 || i>n) continue;
            if(j<1 || j>n) continue;
            if(forest[i][j]=='T' || forest[i][j]=='D') continue;
            if(bees[i][j]==INT_MAX){
                bees[i][j]=bees[u.i][u.j]+1;
                Q.push(node(i,j));
            }
        }
    }
}

node mecho;
node home;

bool bfsMecho(int init){    for (int i = 1; i <= n; i++){        for (int j = 1; j <= n; j++){            dist[i][j]=INT_MAX;
            energy[i][j]=0;        }}
    if(bees[mecho.i][mecho.j]<=init) return false;
    dist[mecho.i][mecho.j]=init;
    energy[mecho.i][mecho.j]=0;
    Q.push(mecho);
    while(!Q.empty()){
        node u=Q.front();
        Q.pop();
        if(bees[u.i][u.j]==dist[u.i][u.j] && energy[u.i][u.j]==0) continue;
        for(int mov=0;mov<4;mov++){
            int i=u.i+dx[mov];
            int j=u.j+dy[mov];
            if(i<1 || i>n) continue;
            if(j<1 || j>n) continue;
            if(forest[i][j]=='T') continue;
            if(dist[i][j]==INT_MAX){
                int change=(energy[u.i][u.j]>0)? 0 : 1;
                dist[i][j]=dist[u.i][u.j]+change;
                if(bees[i][j]<dist[i][j]) continue;
                energy[i][j]=(energy[u.i][u.j]>0)? energy[u.i][u.j]-1 : s-1;
                Q.push(node(i,j));
            }
        }
    }
    return dist[home.i][home.j]!=INT_MAX;
}

int binarySearch(){
    int k=-1;
    int L=0,R=3*n;
    while(L<R){
        int mid=(L+R)/2;
        if(bfsMecho(mid)){
            k=mid;
            L=mid+1;
        }
        else{
            R=mid;
        }
    }
    return k;
}

int binarySearch2(){
    int k=-1;
    for(int b=5000;b>=1;b/=2){
        while(bfsMecho(b+k)) k+=b;
    }
    return k;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin>>n>>s;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>forest[i][j];
            if(forest[i][j]=='M'){
                mecho=node(i,j);
            }
            else if(forest[i][j]=='D'){
                home=node(i,j);
            }
        }
    }        
    bfsBees();
    int ans=binarySearch2();
    cout<<ans<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 151 ms 8940 KB Output is correct
8 Correct 1 ms 492 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 1004 KB Output is correct
13 Correct 1 ms 876 KB Output is correct
14 Correct 1 ms 1004 KB Output is correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 1 ms 640 KB Output is correct
21 Correct 1 ms 748 KB Output is correct
22 Correct 1 ms 748 KB Output is correct
23 Correct 1 ms 876 KB Output is correct
24 Correct 1 ms 876 KB Output is correct
25 Correct 1 ms 1004 KB Output is correct
26 Correct 1 ms 1004 KB Output is correct
27 Correct 1 ms 1024 KB Output is correct
28 Correct 1 ms 1004 KB Output is correct
29 Correct 1 ms 1132 KB Output is correct
30 Correct 1 ms 1132 KB Output is correct
31 Correct 1 ms 1132 KB Output is correct
32 Correct 1 ms 1152 KB Output is correct
33 Correct 14 ms 4088 KB Output is correct
34 Correct 12 ms 4076 KB Output is correct
35 Correct 32 ms 4076 KB Output is correct
36 Correct 22 ms 4716 KB Output is correct
37 Correct 18 ms 4716 KB Output is correct
38 Correct 42 ms 4588 KB Output is correct
39 Correct 30 ms 5228 KB Output is correct
40 Correct 26 ms 5100 KB Output is correct
41 Correct 56 ms 5228 KB Output is correct
42 Correct 42 ms 5612 KB Output is correct
43 Correct 37 ms 5612 KB Output is correct
44 Correct 69 ms 5740 KB Output is correct
45 Correct 58 ms 6272 KB Output is correct
46 Correct 54 ms 6252 KB Output is correct
47 Correct 85 ms 6252 KB Output is correct
48 Correct 77 ms 6764 KB Output is correct
49 Correct 67 ms 6636 KB Output is correct
50 Correct 104 ms 6892 KB Output is correct
51 Correct 101 ms 7276 KB Output is correct
52 Correct 82 ms 7148 KB Output is correct
53 Correct 129 ms 7280 KB Output is correct
54 Correct 127 ms 7660 KB Output is correct
55 Correct 103 ms 7660 KB Output is correct
56 Correct 146 ms 7788 KB Output is correct
57 Correct 164 ms 8300 KB Output is correct
58 Correct 133 ms 8172 KB Output is correct
59 Correct 170 ms 8172 KB Output is correct
60 Correct 205 ms 8684 KB Output is correct
61 Correct 168 ms 8684 KB Output is correct
62 Correct 194 ms 8812 KB Output is correct
63 Correct 136 ms 8684 KB Output is correct
64 Correct 256 ms 8904 KB Output is correct
65 Correct 219 ms 8684 KB Output is correct
66 Correct 187 ms 8684 KB Output is correct
67 Correct 143 ms 8804 KB Output is correct
68 Correct 70 ms 8812 KB Output is correct
69 Correct 61 ms 8832 KB Output is correct
70 Correct 59 ms 8832 KB Output is correct
71 Correct 55 ms 8812 KB Output is correct
72 Correct 51 ms 8812 KB Output is correct
73 Correct 50 ms 9068 KB Output is correct
74 Correct 93 ms 9068 KB Output is correct
75 Correct 109 ms 9196 KB Output is correct
76 Correct 105 ms 9068 KB Output is correct
77 Correct 87 ms 9196 KB Output is correct
78 Correct 101 ms 9068 KB Output is correct
79 Correct 78 ms 8940 KB Output is correct
80 Correct 95 ms 8940 KB Output is correct
81 Correct 114 ms 9056 KB Output is correct
82 Correct 97 ms 8940 KB Output is correct
83 Correct 112 ms 8940 KB Output is correct
84 Correct 121 ms 8940 KB Output is correct
85 Correct 117 ms 9068 KB Output is correct
86 Correct 127 ms 8940 KB Output is correct
87 Correct 130 ms 8940 KB Output is correct
88 Correct 115 ms 8940 KB Output is correct
89 Correct 124 ms 9196 KB Output is correct
90 Correct 142 ms 9068 KB Output is correct
91 Correct 149 ms 9068 KB Output is correct
92 Correct 134 ms 8940 KB Output is correct