답안 #538243

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
538243 2022-03-16T11:54:37 Z erto Mecho (IOI09_mecho) C++17
100 / 100
630 ms 6096 KB
#include <bits/stdc++.h>
typedef long long int ll;
#define INF (1e9 + 7)
#define INF2 (998244353)
#define N (ll)5e4+5
using namespace std;

int n, s, mx, my, dx, dy;
bool bb=0;
char a[802][802];
int d[802][802], d2[802][802];
vector<pair<int, int>> v;
 
void bfs(pair<int, int> p){
    queue<pair<int ,int>> q;
    pair<int, int> p2;
    int t;
    q.push(p);
    while(!q.empty()){
        p2 = q.front();
        q.pop();
        if(p2.first == dx && p2.second == dy)continue;
        t = d[p2.first][p2.second];
        if(p2.first > 1 && d[p2.first - 1][p2.second] > t + 1 && a[p2.first - 1][p2.second]!='T'){
            q.push({p2.first-1, p2.second});
            d[p2.first - 1][p2.second] = t + 1;
        }
        if(p2.second > 1 && d[p2.first][p2.second - 1] > t + 1 && a[p2.first][p2.second - 1]!='T'){
            q.push({p2.first, p2.second - 1});
            d[p2.first][p2.second - 1] = t + 1;
        }
        if(p2.first < n && d[p2.first + 1][p2.second] > t + 1 && a[p2.first + 1][p2.second]!='T'){
            q.push({p2.first+1, p2.second});
            d[p2.first + 1][p2.second] = t + 1;
        }
        if(p2.second < n && d[p2.first][p2.second + 1] > t + 1 && a[p2.first][p2.second + 1]!='T'){
            q.push({p2.first, p2.second + 1});
            d[p2.first][p2.second + 1] = t + 1;
        }
    }
}

void bfs2(int mid){
    queue<pair<int ,int>> q;
    pair<int, int> p2;
    int t;
    q.push({mx, my});
    while(!q.empty()){
        p2 = q.front();
        q.pop();
        t = d2[p2.first][p2.second];
        if(p2.first == dx + 1 && p2.second == dy){
            bb = 1;
            return;
        }
        if(p2.first == dx - 1 && p2.second == dy){
            bb = 1;
            return;
        }
        if(p2.first == dx && p2.second == dy - 1){
            bb = 1;
            return;
        }
        if(p2.first == dx && p2.second == dy + 1){
            bb = 1;
            return;
        }
        if(p2.first > 1 && d2[p2.first - 1][p2.second] > t + 1 && a[p2.first - 1][p2.second]!='T' && (t + 1)/s + mid < d[p2.first - 1][p2.second]){
            q.push({p2.first-1, p2.second});
            d2[p2.first - 1][p2.second] = t + 1;
        }
        if(p2.second > 1 && d2[p2.first][p2.second - 1] > t + 1 && a[p2.first][p2.second - 1]!='T' && (t + 1)/s + mid < d[p2.first][p2.second - 1]){
            q.push({p2.first, p2.second - 1});
            d2[p2.first][p2.second - 1] = t + 1;
        }
        if(p2.first < n && d2[p2.first + 1][p2.second] > t + 1 && a[p2.first + 1][p2.second]!='T' && (t + 1)/s + mid < d[p2.first + 1][p2.second]){
            q.push({p2.first+1, p2.second});
            d2[p2.first + 1][p2.second] = t + 1;
        }
        if(p2.second < n && d2[p2.first][p2.second + 1] > t + 1 && a[p2.first][p2.second + 1]!='T' && (t + 1)/s + mid < d[p2.first][p2.second + 1]){
            q.push({p2.first, p2.second + 1});
            d2[p2.first][p2.second + 1] = t + 1;
        }

    }
}

void solve(){
    cin >> n >> s;
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            d[i][j] =  INF;
        }
    }
    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++){
            cin >> a[i][j];
            if(a[i][j] == 'H'){
                v.push_back({i, j});
                d[i][j] = 0;
            }
            if(a[i][j] == 'M'){
                mx = i;
                my = j;
            }
            if(a[i][j] == 'D'){
                dx = i;
                dy = j;
            }
        }
    }
    for(auto u : v){
        bfs(u);
    }
    d2[mx][my] = 0;

    int l=0, r=d[mx][my]-1, mid, ans=-1;
    while(r >= l){
        mid = (r+l)/2;
        bb = 0;
        memset(d2, INF, sizeof(d2));
        d2[mx][my] = 0;
        bfs2(mid);
        if(bb){
            ans = mid;
            l = mid + 1;
        }
        else{
            r = mid - 1;
        }
    }
    cout<<ans;
}   

int main(){ 
    //freopen("gravity.in", "r", stdin);
    //freopen("gravity.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    //cin>>T;
    while (T--){
        solve();
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2772 KB Output is correct
2 Correct 3 ms 2772 KB Output is correct
3 Correct 2 ms 2760 KB Output is correct
4 Correct 2 ms 2772 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
6 Correct 2 ms 2888 KB Output is correct
7 Correct 123 ms 5972 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 3 ms 2900 KB Output is correct
10 Correct 2 ms 2900 KB Output is correct
11 Correct 2 ms 2888 KB Output is correct
12 Correct 3 ms 3028 KB Output is correct
13 Correct 3 ms 3028 KB Output is correct
14 Correct 4 ms 3020 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
16 Correct 3 ms 2900 KB Output is correct
17 Correct 3 ms 2896 KB Output is correct
18 Correct 2 ms 2900 KB Output is correct
19 Correct 2 ms 2900 KB Output is correct
20 Correct 2 ms 2900 KB Output is correct
21 Correct 3 ms 2892 KB Output is correct
22 Correct 2 ms 2900 KB Output is correct
23 Correct 2 ms 3028 KB Output is correct
24 Correct 3 ms 3028 KB Output is correct
25 Correct 2 ms 3028 KB Output is correct
26 Correct 3 ms 3156 KB Output is correct
27 Correct 2 ms 3028 KB Output is correct
28 Correct 3 ms 3028 KB Output is correct
29 Correct 3 ms 3028 KB Output is correct
30 Correct 2 ms 3028 KB Output is correct
31 Correct 3 ms 3028 KB Output is correct
32 Correct 2 ms 3156 KB Output is correct
33 Correct 6 ms 4188 KB Output is correct
34 Correct 6 ms 4188 KB Output is correct
35 Correct 14 ms 4180 KB Output is correct
36 Correct 7 ms 4436 KB Output is correct
37 Correct 7 ms 4496 KB Output is correct
38 Correct 22 ms 4496 KB Output is correct
39 Correct 7 ms 4660 KB Output is correct
40 Correct 10 ms 4648 KB Output is correct
41 Correct 30 ms 4564 KB Output is correct
42 Correct 9 ms 4820 KB Output is correct
43 Correct 12 ms 4820 KB Output is correct
44 Correct 31 ms 4872 KB Output is correct
45 Correct 9 ms 4996 KB Output is correct
46 Correct 10 ms 5008 KB Output is correct
47 Correct 35 ms 5004 KB Output is correct
48 Correct 10 ms 5204 KB Output is correct
49 Correct 12 ms 5204 KB Output is correct
50 Correct 62 ms 5268 KB Output is correct
51 Correct 17 ms 5460 KB Output is correct
52 Correct 16 ms 5396 KB Output is correct
53 Correct 65 ms 5460 KB Output is correct
54 Correct 14 ms 5592 KB Output is correct
55 Correct 17 ms 5556 KB Output is correct
56 Correct 70 ms 5656 KB Output is correct
57 Correct 16 ms 5844 KB Output is correct
58 Correct 19 ms 5804 KB Output is correct
59 Correct 86 ms 5780 KB Output is correct
60 Correct 19 ms 5976 KB Output is correct
61 Correct 21 ms 6012 KB Output is correct
62 Correct 83 ms 6060 KB Output is correct
63 Correct 116 ms 5972 KB Output is correct
64 Correct 199 ms 6036 KB Output is correct
65 Correct 181 ms 6052 KB Output is correct
66 Correct 156 ms 5964 KB Output is correct
67 Correct 130 ms 5972 KB Output is correct
68 Correct 200 ms 6028 KB Output is correct
69 Correct 175 ms 6052 KB Output is correct
70 Correct 182 ms 5972 KB Output is correct
71 Correct 186 ms 6048 KB Output is correct
72 Correct 158 ms 5964 KB Output is correct
73 Correct 523 ms 6096 KB Output is correct
74 Correct 546 ms 6016 KB Output is correct
75 Correct 630 ms 6008 KB Output is correct
76 Correct 560 ms 6012 KB Output is correct
77 Correct 602 ms 6012 KB Output is correct
78 Correct 484 ms 5988 KB Output is correct
79 Correct 481 ms 5984 KB Output is correct
80 Correct 588 ms 6068 KB Output is correct
81 Correct 555 ms 5964 KB Output is correct
82 Correct 545 ms 5984 KB Output is correct
83 Correct 233 ms 5964 KB Output is correct
84 Correct 203 ms 5992 KB Output is correct
85 Correct 211 ms 6072 KB Output is correct
86 Correct 207 ms 6072 KB Output is correct
87 Correct 219 ms 5988 KB Output is correct
88 Correct 183 ms 6060 KB Output is correct
89 Correct 170 ms 6056 KB Output is correct
90 Correct 212 ms 5976 KB Output is correct
91 Correct 209 ms 5984 KB Output is correct
92 Correct 193 ms 5976 KB Output is correct