Submission #53574

# Submission time Handle Problem Language Result Execution time Memory
53574 2018-06-30T08:37:11 Z 노영훈(#1418) Mecho (IOI09_mecho) C++11
84 / 100
285 ms 6616 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MX=810, inf=2e9;

int n, s;
char B[MX][MX];
int xs, ys, xl, yl;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int Y[MX][MX];

bool valid(int x, int y){
    return 1<=x && x<=n && 1<=y && y<=n && (B[x][y]=='G' || B[x][y]=='H');
}

void bees(){
    queue<pii> Q;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++){
            if(B[i][j]=='H') Y[i][j]=0, Q.push({i,j});
            else Y[i][j]=-1;
        }
    while(!Q.empty()){
        pii p=Q.front(); Q.pop();
        int x,y; tie(x,y)=p;
        for(int k=0; k<4; k++){
            int u=x+dx[k], v=y+dy[k];
            if(!valid(u,v) || Y[u][v]>=0) continue;
            Y[u][v]=Y[x][y]+1;
            Q.push({u,v});
        }
    }
}

int D[MX][MX];

bool solve(int t){
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            D[i][j]=-1;
    queue<pii> Q;
    D[xs][ys]=0;
    Q.push({xs,ys});
    while(!Q.empty()){
        int x,y; tie(x,y)=Q.front(); Q.pop();
        for(int k=0; k<4; k++){
            int u=x+dx[k], v=y+dy[k];
            if(B[u][v]=='D') return true;
            if(!valid(u,v) || D[u][v]>=0) continue;
            int now=t+(D[x][y]+1)/s;
            if(Y[u][v]>=0 && now>=Y[u][v]) continue;
            D[u][v]=D[x][y]+1;
            Q.push({u,v});
        }
    }
    return false;
    /*
    cout<<"\nSOLVED "<<t<<'\n';
    for(int i=1; i<=n; i++, cout<<'\n')
        for(int j=1; j<=n; j++)
            cout<<D[i][j]<<' ';
            */
}


int main(){
    ios::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>>B[i][j];
            if(B[i][j]=='M') xs=i, ys=j, B[i][j]='G';
            if(B[i][j]=='D') xl=i, yl=j;
        }
    bees();
    int s=0, e=n*n;
    while(s<e){
        int m=(s+e+1)/2;
        if(solve(m)) s=m;
        else e=m-1;
    }
    cout<<(solve(s) ? s : -1);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 548 KB Output is correct
3 Correct 2 ms 548 KB Output is correct
4 Correct 2 ms 548 KB Output is correct
5 Correct 2 ms 548 KB Output is correct
6 Correct 2 ms 548 KB Output is correct
7 Correct 123 ms 6216 KB Output is correct
8 Correct 2 ms 6216 KB Output is correct
9 Correct 2 ms 6216 KB Output is correct
10 Correct 2 ms 6216 KB Output is correct
11 Correct 2 ms 6216 KB Output is correct
12 Incorrect 2 ms 6216 KB Output isn't correct
13 Incorrect 2 ms 6216 KB Output isn't correct
14 Correct 3 ms 6216 KB Output is correct
15 Correct 2 ms 6216 KB Output is correct
16 Correct 2 ms 6216 KB Output is correct
17 Correct 2 ms 6216 KB Output is correct
18 Correct 2 ms 6216 KB Output is correct
19 Correct 3 ms 6216 KB Output is correct
20 Correct 2 ms 6216 KB Output is correct
21 Correct 3 ms 6216 KB Output is correct
22 Correct 3 ms 6216 KB Output is correct
23 Correct 3 ms 6216 KB Output is correct
24 Correct 3 ms 6216 KB Output is correct
25 Correct 3 ms 6216 KB Output is correct
26 Correct 2 ms 6216 KB Output is correct
27 Correct 3 ms 6216 KB Output is correct
28 Correct 3 ms 6216 KB Output is correct
29 Correct 3 ms 6216 KB Output is correct
30 Correct 3 ms 6216 KB Output is correct
31 Correct 2 ms 6216 KB Output is correct
32 Correct 2 ms 6216 KB Output is correct
33 Correct 8 ms 6216 KB Output is correct
34 Correct 8 ms 6216 KB Output is correct
35 Correct 28 ms 6216 KB Output is correct
36 Correct 9 ms 6216 KB Output is correct
37 Correct 10 ms 6216 KB Output is correct
38 Correct 40 ms 6216 KB Output is correct
39 Correct 12 ms 6216 KB Output is correct
40 Correct 13 ms 6216 KB Output is correct
41 Correct 54 ms 6216 KB Output is correct
42 Correct 13 ms 6216 KB Output is correct
43 Correct 13 ms 6216 KB Output is correct
44 Correct 58 ms 6216 KB Output is correct
45 Correct 15 ms 6216 KB Output is correct
46 Correct 16 ms 6216 KB Output is correct
47 Correct 72 ms 6216 KB Output is correct
48 Correct 24 ms 6216 KB Output is correct
49 Correct 22 ms 6216 KB Output is correct
50 Correct 104 ms 6216 KB Output is correct
51 Correct 22 ms 6216 KB Output is correct
52 Correct 21 ms 6216 KB Output is correct
53 Correct 118 ms 6216 KB Output is correct
54 Correct 34 ms 6216 KB Output is correct
55 Correct 34 ms 6216 KB Output is correct
56 Correct 137 ms 6216 KB Output is correct
57 Correct 26 ms 6216 KB Output is correct
58 Correct 26 ms 6216 KB Output is correct
59 Correct 178 ms 6216 KB Output is correct
60 Correct 28 ms 6268 KB Output is correct
61 Correct 29 ms 6268 KB Output is correct
62 Correct 215 ms 6268 KB Output is correct
63 Correct 140 ms 6396 KB Output is correct
64 Correct 272 ms 6396 KB Output is correct
65 Correct 285 ms 6396 KB Output is correct
66 Correct 178 ms 6396 KB Output is correct
67 Correct 160 ms 6396 KB Output is correct
68 Correct 72 ms 6396 KB Output is correct
69 Correct 66 ms 6396 KB Output is correct
70 Correct 70 ms 6408 KB Output is correct
71 Correct 59 ms 6408 KB Output is correct
72 Incorrect 45 ms 6408 KB Output isn't correct
73 Incorrect 68 ms 6500 KB Output isn't correct
74 Correct 106 ms 6500 KB Output is correct
75 Correct 117 ms 6500 KB Output is correct
76 Correct 128 ms 6500 KB Output is correct
77 Correct 133 ms 6500 KB Output is correct
78 Correct 115 ms 6500 KB Output is correct
79 Correct 68 ms 6500 KB Output is correct
80 Correct 84 ms 6500 KB Output is correct
81 Correct 94 ms 6548 KB Output is correct
82 Correct 82 ms 6548 KB Output is correct
83 Correct 115 ms 6548 KB Output is correct
84 Correct 105 ms 6548 KB Output is correct
85 Correct 113 ms 6564 KB Output is correct
86 Correct 118 ms 6564 KB Output is correct
87 Correct 147 ms 6568 KB Output is correct
88 Correct 114 ms 6568 KB Output is correct
89 Correct 141 ms 6568 KB Output is correct
90 Correct 156 ms 6616 KB Output is correct
91 Correct 135 ms 6616 KB Output is correct
92 Correct 122 ms 6616 KB Output is correct