Submission #1084607

# Submission time Handle Problem Language Result Execution time Memory
1084607 2024-09-06T13:39:02 Z kiethm07 Mecho (IOI09_mecho) C++11
100 / 100
589 ms 11404 KB
#include <bits/stdc++.h>

#define pii pair<int,int>
#define iii pair<int,pii>
#define fi first
#define se second

#define vi vector<int>
#define vii vector<pii>
#define pb(i) push_back(i)
#define all(x) x.begin(),x.end()

#define TEXT "a"

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int inf = 1e9 + 7;
const ld eps = 1e-8;
const double pi = acos(-1);
const int N = 805;

int n,s;
int f[N][N], g[N][N];
char a[N][N];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};

bool check(int val){
    queue<pii> q;
    memset(f,-1,sizeof(f));
    memset(g,-1,sizeof(g));
    pii M, D;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            if (a[i][j] == 'M') M = pii(i,j);
            if (a[i][j] == 'D') D = pii(i,j);
            if (a[i][j] == 'H'){
                q.push(pii(i,j));
                f[i][j] = 0;
            }
        }
    }
    while(!q.empty()){
        pii u = q.front();
        q.pop();
        if (f[u.fi][u.se] >= val) continue;
        for (int i = 0; i < 4; i++){
            int x = u.fi + dx[i];
            int y = u.se + dy[i];
            if (x == 0 || x > n || y == 0 || y > n) continue;
            if (a[x][y] == 'T' || a[x][y] == 'D') continue;
            if (f[x][y] != -1) continue;
            f[x][y] = f[u.fi][u.se] + 1;
            q.push(pii(x,y));
        }
    }
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++){
            if (f[i][j] == -1) continue;
            f[i][j] = 0;
            q.push(pii(i,j));
        }
    }
    if (f[M.fi][M.se] != -1) return 0;
    while(!q.empty()){
        pii u = q.front();
        q.pop();
        for (int i = 0; i < 4; i++){
            int x = u.fi + dx[i];
            int y = u.se + dy[i];
            if (x == 0 || x > n || y == 0 || y > n) continue;
            if (a[x][y] == 'T' || a[x][y] == 'D') continue;
            if (f[x][y] != -1) continue;
            f[x][y] = f[u.fi][u.se] + s;
            q.push(pii(x,y));
        }
    }
    f[D.fi][D.se] = inf;
    g[M.fi][M.se] = 0;
    q.push(M);
    while(!q.empty()){
        pii u = q.front();
        q.pop();
        for (int i = 0; i < 4; i++){
            int x = u.fi + dx[i];
            int y = u.se + dy[i];
            if (x == 0 || x > n || y == 0 || y > n) continue;
            if (a[x][y] == 'T') continue;
            if (g[x][y] != -1) continue;
            if (f[x][y] <= g[u.fi][u.se] + 1) continue;
            g[x][y] = g[u.fi][u.se] + 1;
            q.push(pii(x,y));
        }
    }
    return g[D.fi][D.se] != -1;
}

int main(){
    cin.tie(0) -> sync_with_stdio(0);
    if (fopen(TEXT".inp","r")){
        freopen(TEXT".inp","r",stdin);
        freopen(TEXT".out","w",stdout);
    }
    cin >> n >> s;
    for (int i = 1; i <= n; i++){
        for (int j = 1; j <= n; j++) cin >> a[i][j];
    }
    int res = -1;
    int l = 0;
    int r = inf;
    while(l <= r){
        int mid = (l + r) / 2;
        if (check(mid)){
            res = mid;
            l = mid + 1;
        }
        else r = mid - 1;
    }
    cout << res << "\n";
    return 0;
}

Compilation message

mecho.cpp: In function 'int main()':
mecho.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  105 |         freopen(TEXT".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |         freopen(TEXT".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5980 KB Output is correct
2 Correct 4 ms 5992 KB Output is correct
3 Correct 4 ms 5980 KB Output is correct
4 Correct 4 ms 5980 KB Output is correct
5 Correct 4 ms 5992 KB Output is correct
6 Correct 4 ms 5980 KB Output is correct
7 Correct 519 ms 11404 KB Output is correct
8 Correct 5 ms 5980 KB Output is correct
9 Correct 4 ms 5980 KB Output is correct
10 Correct 4 ms 6000 KB Output is correct
11 Correct 5 ms 5976 KB Output is correct
12 Correct 6 ms 5980 KB Output is correct
13 Correct 6 ms 5980 KB Output is correct
14 Correct 6 ms 6068 KB Output is correct
15 Correct 6 ms 6004 KB Output is correct
16 Correct 5 ms 6008 KB Output is correct
17 Correct 5 ms 5468 KB Output is correct
18 Correct 5 ms 5980 KB Output is correct
19 Correct 6 ms 6016 KB Output is correct
20 Correct 6 ms 5980 KB Output is correct
21 Correct 5 ms 5980 KB Output is correct
22 Correct 5 ms 5980 KB Output is correct
23 Correct 6 ms 5980 KB Output is correct
24 Correct 6 ms 5980 KB Output is correct
25 Correct 6 ms 5980 KB Output is correct
26 Correct 6 ms 5980 KB Output is correct
27 Correct 7 ms 5980 KB Output is correct
28 Correct 6 ms 5980 KB Output is correct
29 Correct 6 ms 5980 KB Output is correct
30 Correct 6 ms 5980 KB Output is correct
31 Correct 7 ms 6072 KB Output is correct
32 Correct 7 ms 5980 KB Output is correct
33 Correct 65 ms 6672 KB Output is correct
34 Correct 52 ms 6668 KB Output is correct
35 Correct 78 ms 6928 KB Output is correct
36 Correct 69 ms 6748 KB Output is correct
37 Correct 69 ms 6804 KB Output is correct
38 Correct 93 ms 7144 KB Output is correct
39 Correct 91 ms 6972 KB Output is correct
40 Correct 81 ms 6976 KB Output is correct
41 Correct 119 ms 7360 KB Output is correct
42 Correct 103 ms 7152 KB Output is correct
43 Correct 102 ms 7172 KB Output is correct
44 Correct 148 ms 7688 KB Output is correct
45 Correct 135 ms 7412 KB Output is correct
46 Correct 117 ms 7404 KB Output is correct
47 Correct 174 ms 8024 KB Output is correct
48 Correct 165 ms 7616 KB Output is correct
49 Correct 150 ms 7616 KB Output is correct
50 Correct 207 ms 8380 KB Output is correct
51 Correct 180 ms 7908 KB Output is correct
52 Correct 172 ms 7892 KB Output is correct
53 Correct 239 ms 8740 KB Output is correct
54 Correct 213 ms 8140 KB Output is correct
55 Correct 205 ms 8132 KB Output is correct
56 Correct 284 ms 9140 KB Output is correct
57 Correct 249 ms 8460 KB Output is correct
58 Correct 211 ms 8412 KB Output is correct
59 Correct 574 ms 9592 KB Output is correct
60 Correct 274 ms 8792 KB Output is correct
61 Correct 258 ms 8800 KB Output is correct
62 Correct 367 ms 10072 KB Output is correct
63 Correct 501 ms 9276 KB Output is correct
64 Correct 546 ms 9276 KB Output is correct
65 Correct 541 ms 9276 KB Output is correct
66 Correct 517 ms 9272 KB Output is correct
67 Correct 492 ms 9304 KB Output is correct
68 Correct 547 ms 9264 KB Output is correct
69 Correct 549 ms 9268 KB Output is correct
70 Correct 530 ms 9272 KB Output is correct
71 Correct 542 ms 9284 KB Output is correct
72 Correct 575 ms 9244 KB Output is correct
73 Correct 471 ms 11376 KB Output is correct
74 Correct 577 ms 11324 KB Output is correct
75 Correct 566 ms 11344 KB Output is correct
76 Correct 553 ms 11352 KB Output is correct
77 Correct 588 ms 11336 KB Output is correct
78 Correct 540 ms 11344 KB Output is correct
79 Correct 580 ms 11344 KB Output is correct
80 Correct 542 ms 11352 KB Output is correct
81 Correct 536 ms 11352 KB Output is correct
82 Correct 549 ms 11352 KB Output is correct
83 Correct 515 ms 11356 KB Output is correct
84 Correct 580 ms 11344 KB Output is correct
85 Correct 539 ms 11348 KB Output is correct
86 Correct 535 ms 11328 KB Output is correct
87 Correct 549 ms 11356 KB Output is correct
88 Correct 522 ms 11344 KB Output is correct
89 Correct 589 ms 11348 KB Output is correct
90 Correct 534 ms 11356 KB Output is correct
91 Correct 542 ms 11356 KB Output is correct
92 Correct 520 ms 11356 KB Output is correct