답안 #879152

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
879152 2023-11-26T13:50:03 Z anarch_y Mecho (IOI09_mecho) C++17
10 / 100
151 ms 7004 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define all(x) begin(x), end(x)
#define pb push_back

int N, S;
char A[800][800];
int d[800][800], D[800][800];
pii M, P;
int ans = -1;

int dr[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};

bool invalid(int r, int c){
    if(r<0 or r>=N or c<0 or c>=N) return true;
    if(A[r][c] == 'T') return true;
    return false;
}

bool check(int k){
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++) d[i][j] = 1e9;
    }
    queue<pii> q, qn;
    q.push(M);
    int tt = 0;
    d[M.first][M.second] = 0;
    bool found = false;
    for(int x=0; x<N; x++){
        while(!q.empty()){
            auto p = q.front(); q.pop();
            int r = p.first, c = p.second;
            if(d[r][c] == (tt+1)*S){
                if(D[r][c]>k+tt+1){
                    qn.push({r, c});
                }
                continue;
            }
            for(int i=0; i<4; i++){
                int nr = r+dr[i], nc = c+dc[i];
                if(invalid(nr, nc)) continue;
                if(d[nr][nc] != 1e9) continue;
                d[nr][nc] = d[r][c]+1;
                q.push({nr, nc});
                if(P == pii(nr, nc)) found = true;
            }
        }
        if(found) break;
        q = qn;
        qn = queue<pii>();
        tt++;
    }
    return found;
}

void solve(){
    int L = 1, R = N;
    while(L <= R){
        int mid = (L+R)/2;
        if(check(mid)){
            ans = mid;
            L=mid+1;
        }
        else R=mid-1;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> S;
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            cin >> A[i][j];
            if(A[i][j] == 'M') M = {i, j};
            if(A[i][j] == 'D') P = {i, j};
            d[i][j] = D[i][j] = 1e9;
        }
    }
    queue<pii> Q;
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            if(A[i][j] == 'H'){
                Q.push({i, j});
                D[i][j] = 0;
            }
        }
    }

    while(!Q.empty()){
        auto p = Q.front(); Q.pop();
        int r = p.first, c = p.second;
        for(int i=0; i<4; i++){
            int nr = r+dr[i], nc = c+dc[i];
            if(invalid(nr, nc)) continue;
            if(A[nr][nc] == 'D') continue;
            if(D[nr][nc] != 1e9) continue;
            D[nr][nc] = D[r][c]+1;
            Q.push({nr, nc});
        }
    }
    solve();
    cout << ans;

}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2392 KB Output is correct
2 Correct 0 ms 2648 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Incorrect 0 ms 2396 KB Output isn't correct
6 Incorrect 0 ms 2396 KB Output isn't correct
7 Incorrect 69 ms 6816 KB Output isn't correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Incorrect 1 ms 2396 KB Output isn't correct
12 Incorrect 1 ms 4788 KB Output isn't correct
13 Incorrect 1 ms 2648 KB Output isn't correct
14 Correct 1 ms 4700 KB Output is correct
15 Incorrect 1 ms 2396 KB Output isn't correct
16 Incorrect 1 ms 2396 KB Output isn't correct
17 Incorrect 1 ms 2396 KB Output isn't correct
18 Incorrect 1 ms 2396 KB Output isn't correct
19 Incorrect 1 ms 2396 KB Output isn't correct
20 Incorrect 1 ms 2392 KB Output isn't correct
21 Incorrect 1 ms 2652 KB Output isn't correct
22 Incorrect 1 ms 2652 KB Output isn't correct
23 Incorrect 1 ms 2652 KB Output isn't correct
24 Incorrect 1 ms 2652 KB Output isn't correct
25 Incorrect 1 ms 4700 KB Output isn't correct
26 Incorrect 1 ms 4700 KB Output isn't correct
27 Incorrect 1 ms 4700 KB Output isn't correct
28 Incorrect 1 ms 4700 KB Output isn't correct
29 Incorrect 1 ms 4696 KB Output isn't correct
30 Incorrect 1 ms 4952 KB Output isn't correct
31 Incorrect 1 ms 4700 KB Output isn't correct
32 Incorrect 1 ms 4700 KB Output isn't correct
33 Incorrect 4 ms 4956 KB Output isn't correct
34 Incorrect 4 ms 4956 KB Output isn't correct
35 Incorrect 13 ms 5132 KB Output isn't correct
36 Incorrect 4 ms 5212 KB Output isn't correct
37 Incorrect 5 ms 5212 KB Output isn't correct
38 Incorrect 16 ms 5220 KB Output isn't correct
39 Incorrect 6 ms 5212 KB Output isn't correct
40 Incorrect 5 ms 5212 KB Output isn't correct
41 Incorrect 20 ms 5268 KB Output isn't correct
42 Incorrect 6 ms 5208 KB Output isn't correct
43 Incorrect 7 ms 5212 KB Output isn't correct
44 Incorrect 24 ms 5212 KB Output isn't correct
45 Incorrect 8 ms 5468 KB Output isn't correct
46 Incorrect 8 ms 5268 KB Output isn't correct
47 Incorrect 30 ms 5464 KB Output isn't correct
48 Incorrect 9 ms 5464 KB Output isn't correct
49 Incorrect 9 ms 5676 KB Output isn't correct
50 Incorrect 37 ms 5660 KB Output isn't correct
51 Incorrect 12 ms 5720 KB Output isn't correct
52 Incorrect 10 ms 5928 KB Output isn't correct
53 Incorrect 43 ms 5944 KB Output isn't correct
54 Incorrect 13 ms 5976 KB Output isn't correct
55 Incorrect 11 ms 5980 KB Output isn't correct
56 Incorrect 49 ms 6112 KB Output isn't correct
57 Incorrect 13 ms 6312 KB Output isn't correct
58 Incorrect 13 ms 6236 KB Output isn't correct
59 Incorrect 69 ms 6444 KB Output isn't correct
60 Incorrect 20 ms 6492 KB Output isn't correct
61 Incorrect 14 ms 6608 KB Output isn't correct
62 Incorrect 64 ms 6724 KB Output isn't correct
63 Incorrect 45 ms 6492 KB Output isn't correct
64 Incorrect 151 ms 6488 KB Output isn't correct
65 Incorrect 138 ms 6488 KB Output isn't correct
66 Incorrect 97 ms 6708 KB Output isn't correct
67 Correct 51 ms 6488 KB Output is correct
68 Incorrect 43 ms 6744 KB Output isn't correct
69 Correct 37 ms 6748 KB Output is correct
70 Incorrect 32 ms 6748 KB Output isn't correct
71 Incorrect 29 ms 6748 KB Output isn't correct
72 Incorrect 28 ms 6748 KB Output isn't correct
73 Incorrect 30 ms 7000 KB Output isn't correct
74 Incorrect 58 ms 7004 KB Output isn't correct
75 Incorrect 46 ms 6744 KB Output isn't correct
76 Incorrect 45 ms 6948 KB Output isn't correct
77 Incorrect 47 ms 7000 KB Output isn't correct
78 Incorrect 58 ms 7000 KB Output isn't correct
79 Incorrect 63 ms 6956 KB Output isn't correct
80 Incorrect 42 ms 6744 KB Output isn't correct
81 Incorrect 51 ms 6748 KB Output isn't correct
82 Incorrect 45 ms 6748 KB Output isn't correct
83 Incorrect 57 ms 6744 KB Output isn't correct
84 Incorrect 69 ms 6748 KB Output isn't correct
85 Incorrect 69 ms 6928 KB Output isn't correct
86 Incorrect 59 ms 6912 KB Output isn't correct
87 Incorrect 55 ms 6916 KB Output isn't correct
88 Incorrect 60 ms 6748 KB Output isn't correct
89 Incorrect 60 ms 6744 KB Output isn't correct
90 Incorrect 63 ms 6744 KB Output isn't correct
91 Incorrect 60 ms 6864 KB Output isn't correct
92 Incorrect 60 ms 6744 KB Output isn't correct