답안 #53572

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
53572 2018-06-30T08:33:16 Z 노영훈(#1418) Mecho (IOI09_mecho) C++11
73 / 100
267 ms 6612 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(!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});
        }
    }
    /*
    cout<<"\nSOLVED "<<t<<'\n';
    for(int i=1; i<=n; i++, cout<<'\n')
        for(int j=1; j<=n; j++)
            cout<<D[i][j]<<' ';
            */
    return D[xl][yl]>=0;
}


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, B[i][j]='G';
        }
    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Incorrect 2 ms 376 KB Output isn't correct
3 Correct 2 ms 412 KB Output is correct
4 Incorrect 2 ms 444 KB Output isn't correct
5 Correct 2 ms 596 KB Output is correct
6 Incorrect 2 ms 596 KB Output isn't correct
7 Incorrect 149 ms 6460 KB Output isn't correct
8 Correct 2 ms 6460 KB Output is correct
9 Correct 2 ms 6460 KB Output is correct
10 Correct 2 ms 6460 KB Output is correct
11 Correct 2 ms 6460 KB Output is correct
12 Incorrect 3 ms 6460 KB Output isn't correct
13 Incorrect 2 ms 6460 KB Output isn't correct
14 Correct 3 ms 6460 KB Output is correct
15 Correct 2 ms 6460 KB Output is correct
16 Correct 2 ms 6460 KB Output is correct
17 Correct 2 ms 6460 KB Output is correct
18 Correct 3 ms 6460 KB Output is correct
19 Correct 2 ms 6460 KB Output is correct
20 Correct 2 ms 6460 KB Output is correct
21 Correct 2 ms 6460 KB Output is correct
22 Correct 2 ms 6460 KB Output is correct
23 Correct 2 ms 6460 KB Output is correct
24 Correct 3 ms 6460 KB Output is correct
25 Correct 3 ms 6460 KB Output is correct
26 Correct 2 ms 6460 KB Output is correct
27 Correct 3 ms 6460 KB Output is correct
28 Correct 3 ms 6460 KB Output is correct
29 Correct 3 ms 6460 KB Output is correct
30 Correct 3 ms 6460 KB Output is correct
31 Correct 2 ms 6460 KB Output is correct
32 Correct 3 ms 6460 KB Output is correct
33 Correct 10 ms 6460 KB Output is correct
34 Correct 9 ms 6460 KB Output is correct
35 Correct 31 ms 6460 KB Output is correct
36 Correct 11 ms 6460 KB Output is correct
37 Correct 13 ms 6460 KB Output is correct
38 Correct 41 ms 6460 KB Output is correct
39 Correct 18 ms 6460 KB Output is correct
40 Correct 15 ms 6460 KB Output is correct
41 Correct 64 ms 6460 KB Output is correct
42 Correct 17 ms 6460 KB Output is correct
43 Correct 17 ms 6460 KB Output is correct
44 Correct 76 ms 6460 KB Output is correct
45 Correct 20 ms 6460 KB Output is correct
46 Correct 20 ms 6460 KB Output is correct
47 Correct 82 ms 6460 KB Output is correct
48 Correct 29 ms 6460 KB Output is correct
49 Correct 22 ms 6460 KB Output is correct
50 Correct 107 ms 6460 KB Output is correct
51 Correct 27 ms 6460 KB Output is correct
52 Correct 29 ms 6460 KB Output is correct
53 Correct 155 ms 6460 KB Output is correct
54 Correct 30 ms 6460 KB Output is correct
55 Correct 32 ms 6460 KB Output is correct
56 Correct 176 ms 6460 KB Output is correct
57 Correct 31 ms 6460 KB Output is correct
58 Correct 33 ms 6460 KB Output is correct
59 Correct 179 ms 6460 KB Output is correct
60 Correct 34 ms 6460 KB Output is correct
61 Correct 39 ms 6460 KB Output is correct
62 Correct 215 ms 6460 KB Output is correct
63 Correct 147 ms 6460 KB Output is correct
64 Correct 267 ms 6460 KB Output is correct
65 Correct 197 ms 6460 KB Output is correct
66 Correct 171 ms 6460 KB Output is correct
67 Correct 181 ms 6460 KB Output is correct
68 Correct 67 ms 6460 KB Output is correct
69 Correct 65 ms 6460 KB Output is correct
70 Correct 60 ms 6460 KB Output is correct
71 Correct 56 ms 6460 KB Output is correct
72 Incorrect 58 ms 6524 KB Output isn't correct
73 Incorrect 65 ms 6524 KB Output isn't correct
74 Correct 76 ms 6524 KB Output is correct
75 Correct 106 ms 6524 KB Output is correct
76 Correct 98 ms 6524 KB Output is correct
77 Correct 109 ms 6524 KB Output is correct
78 Correct 117 ms 6524 KB Output is correct
79 Correct 80 ms 6524 KB Output is correct
80 Correct 99 ms 6524 KB Output is correct
81 Correct 97 ms 6612 KB Output is correct
82 Correct 106 ms 6612 KB Output is correct
83 Correct 139 ms 6612 KB Output is correct
84 Correct 108 ms 6612 KB Output is correct
85 Correct 110 ms 6612 KB Output is correct
86 Correct 116 ms 6612 KB Output is correct
87 Correct 111 ms 6612 KB Output is correct
88 Correct 126 ms 6612 KB Output is correct
89 Correct 151 ms 6612 KB Output is correct
90 Correct 153 ms 6612 KB Output is correct
91 Correct 146 ms 6612 KB Output is correct
92 Correct 134 ms 6612 KB Output is correct