답안 #682839

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
682839 2023-01-17T05:53:11 Z NONTAC Mecho (IOI09_mecho) C++11
100 / 100
238 ms 12444 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;

int dx[4] = {0, -1, 1, 0};
int dy[4] = {1, 0, 0, -1};
long long n, s;
int si, sj, fi, fj;
char grid[802][802];
long long reach[802][802], d[802][802];
bool vis[802][802];

void print()
{
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++) cout<<reach[i][j]<<" ";
        cout<<endl;
    }
}

void INIT()
{
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            vis[i][j] = false;
            d[i][j] = INT_MAX;
        }
    }
}

void flood()
{
    queue<ii> q;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++) if(grid[i][j] == 'H'){
            vis[i][j] = true;
            q.push(ii(i, j));
        }
    }
    while(!q.empty()){
        int ui = q.front().first, uj = q.front().second;
        q.pop();
        for(int i = 0; i < 4; i++){
            int vi = ui + dx[i], vj = uj + dy[i];
            if(vi >= 1 && vi <= n && vj >= 1 && vj <= n){
                if(!vis[vi][vj] && grid[vi][vj] != 'T' && grid[vi][vj] != 'D'){
                    vis[vi][vj] = true;
                    reach[vi][vj] = reach[ui][uj] + s;
                    q.push(ii(vi, vj));
                }
            }
        }
    }
}

bool check(long long k)
{
    INIT();
    d[si][sj] = k * s;
    if(d[si][sj] >= reach[si][sj]) return false;
    reach[fi][fj] = INT_MAX;
    vis[si][sj] = true;
    queue<ii> q;
    q.push(ii(si, sj));
    while(!q.empty()){
        int ui = q.front().first, uj = q.front().second;
        q.pop();
        if((ui >= fi - 1 && ui <= fi + 1 && uj == fj)||(ui == fi && uj <= fj + 1 && uj >= fj - 1)){
            return true;
        }
        for(int i = 0; i < 4; i++){
            int vi = ui + dx[i], vj = uj + dy[i];
            if(vi < 1 || vi > n || vj < 1 || vj > n) continue;
            if(reach[vi][vj] > d[ui][uj] + 1 && !vis[vi][vj] && grid[vi][vj] != 'T'){
                vis[vi][vj] = true;
                d[vi][vj] = d[ui][uj] + 1;
                q.push(ii(vi, vj));
            }
        }
    }

    return false;
}

int main()
{
    //freopen("test.in","r",stdin);
    //freopen("test.out","w",stdout);

    cin>>n>>s;

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            cin>>grid[i][j];
            if(grid[i][j] == 'M'){
                si = i;
                sj = j;
            }
            if(grid[i][j] == 'D'){
                fi = i;
                fj = j;
            }
        }
    }

    flood();

    //print();

    long long lo = 0, hi = 1000000000;
    while(lo < hi){
        long long mid = lo + (hi - lo + 1) / 2;
        if(check(mid)){
            lo = mid;
        }
        else{
            hi = mid - 1;
        }
    }

    if(!check(lo)) cout<<-1;
    else cout<<lo;


    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Correct 0 ms 304 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 156 ms 12224 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 436 KB Output is correct
10 Correct 1 ms 436 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 852 KB Output is correct
13 Correct 1 ms 820 KB Output is correct
14 Correct 1 ms 852 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 596 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 820 KB Output is correct
25 Correct 1 ms 852 KB Output is correct
26 Correct 1 ms 852 KB Output is correct
27 Correct 1 ms 948 KB Output is correct
28 Correct 2 ms 980 KB Output is correct
29 Correct 1 ms 980 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 2 ms 1108 KB Output is correct
32 Correct 2 ms 1084 KB Output is correct
33 Correct 11 ms 4948 KB Output is correct
34 Correct 13 ms 5124 KB Output is correct
35 Correct 27 ms 5332 KB Output is correct
36 Correct 14 ms 5588 KB Output is correct
37 Correct 15 ms 5896 KB Output is correct
38 Correct 43 ms 6116 KB Output is correct
39 Correct 19 ms 6332 KB Output is correct
40 Correct 20 ms 6732 KB Output is correct
41 Correct 45 ms 6860 KB Output is correct
42 Correct 23 ms 6988 KB Output is correct
43 Correct 23 ms 7456 KB Output is correct
44 Correct 66 ms 7612 KB Output is correct
45 Correct 26 ms 7740 KB Output is correct
46 Correct 27 ms 8264 KB Output is correct
47 Correct 98 ms 8368 KB Output is correct
48 Correct 38 ms 8436 KB Output is correct
49 Correct 33 ms 8948 KB Output is correct
50 Correct 109 ms 9124 KB Output is correct
51 Correct 43 ms 9076 KB Output is correct
52 Correct 38 ms 9724 KB Output is correct
53 Correct 142 ms 9900 KB Output is correct
54 Correct 47 ms 9940 KB Output is correct
55 Correct 43 ms 10408 KB Output is correct
56 Correct 142 ms 10676 KB Output is correct
57 Correct 48 ms 10552 KB Output is correct
58 Correct 48 ms 11028 KB Output is correct
59 Correct 205 ms 11396 KB Output is correct
60 Correct 55 ms 11084 KB Output is correct
61 Correct 54 ms 11656 KB Output is correct
62 Correct 208 ms 12112 KB Output is correct
63 Correct 157 ms 12040 KB Output is correct
64 Correct 238 ms 12108 KB Output is correct
65 Correct 236 ms 12108 KB Output is correct
66 Correct 192 ms 12108 KB Output is correct
67 Correct 184 ms 12112 KB Output is correct
68 Correct 94 ms 12140 KB Output is correct
69 Correct 88 ms 12020 KB Output is correct
70 Correct 101 ms 12108 KB Output is correct
71 Correct 76 ms 12224 KB Output is correct
72 Correct 65 ms 12140 KB Output is correct
73 Correct 79 ms 12284 KB Output is correct
74 Correct 98 ms 12392 KB Output is correct
75 Correct 126 ms 12320 KB Output is correct
76 Correct 104 ms 12444 KB Output is correct
77 Correct 105 ms 12340 KB Output is correct
78 Correct 119 ms 12348 KB Output is correct
79 Correct 135 ms 12344 KB Output is correct
80 Correct 101 ms 12328 KB Output is correct
81 Correct 103 ms 12348 KB Output is correct
82 Correct 124 ms 12304 KB Output is correct
83 Correct 138 ms 12256 KB Output is correct
84 Correct 154 ms 12288 KB Output is correct
85 Correct 116 ms 12216 KB Output is correct
86 Correct 153 ms 12400 KB Output is correct
87 Correct 134 ms 12308 KB Output is correct
88 Correct 128 ms 12216 KB Output is correct
89 Correct 144 ms 12364 KB Output is correct
90 Correct 159 ms 12260 KB Output is correct
91 Correct 157 ms 12256 KB Output is correct
92 Correct 151 ms 12268 KB Output is correct