Submission #759173

# Submission time Handle Problem Language Result Execution time Memory
759173 2023-06-15T20:13:13 Z Blagoj Mecho (IOI09_mecho) C++17
51 / 100
1000 ms 7372 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
 
ll n, s, times[810][810];
vector<string> v(810);
pair<ll, ll> st, en;
 
const ll dx[4] = {0, 0, -1, 1};
const ll dy[4] = {-1, 1, 0, 0};
 
inline bool valid(ll x, ll y) {
    return (x >= 0 && x < n && y >= 0 && y < n && v[x][y] != 'T' && (v[x][y] == 'G' || v[x][y] == 'M' || v[x][y] == 'H' || v[x][y] == 'D'));
}
 
bool check(ll m) {
    if (!(m < times[st.first][st.second]) || !(m < times[st.first][st.second])) return 0;
    queue<pair<ll, ll>> q;
    q.push(st);
    bool vis[n + 2][n + 2];
    memset(vis, 0, sizeof(vis));
    vis[st.first][st.second] = 1;
    ll moves = 0;
    do {
        moves++;
        if (moves >= s) {
            moves = 0;
            m++;
        }
        ll sz = q.size();
        while (sz--) {
            ll x = q.front().first, y = q.front().second;
            q.pop();
            for (ll d = 0; d < 4; d++) {
                ll nx = x + dx[d], ny = y + dy[d];
                if (valid(nx, ny) && m < times[nx][ny] && !vis[nx][ny]) {
                    vis[nx][ny] = 1;
                    q.push({nx, ny});
                    if (make_pair(nx, ny) == en) return 1;
                }
            }
        }
    }
    while (q.size() > 0);
    return 0;
}
 
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n >> s;
    queue<pair<ll, ll>> h;
 
    for (ll i = 0; i < n; i++) {
        cin >> v[i];
        for (ll j = 0; j < n; j++) {
            times[i][j] = n * n + 1000;
            if (v[i][j] == 'M') st = {i, j};
            if (v[i][j] == 'H') {
                h.push({i, j});
                times[i][j] = 0;
            }
            if (v[i][j] == 'D') en = {i, j};
        }
    }
 
    ll mx = 0;
    while (h.size() > 0) {
        ll x = h.front().first, y = h.front().second;
        h.pop();
        for (ll d = 0; d < 4; d++) {
            ll nx = x + dx[d], ny = y + dy[d];
            if (valid(nx, ny) && times[x][y] + 1 < times[nx][ny]) {
                times[nx][ny] = times[x][y] + 1;
                mx = max(mx, times[nx][ny]);
                h.push({nx, ny});
            }
        }
    }
    ll ans = -1;
    for (int i = 0; i <= mx; i++) {
        if (check(i)) ans = i;
        else break;
    }
 
    cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Incorrect 0 ms 340 KB Output isn't correct
3 Correct 1 ms 340 KB Output is correct
4 Incorrect 1 ms 416 KB Output isn't correct
5 Correct 1 ms 340 KB Output is correct
6 Incorrect 1 ms 340 KB Output isn't correct
7 Incorrect 225 ms 6932 KB Output isn't correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Incorrect 1 ms 596 KB Output isn't correct
13 Correct 1 ms 468 KB Output is correct
14 Correct 1 ms 596 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 1 ms 596 KB Output is correct
26 Correct 3 ms 596 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 4 ms 596 KB Output is correct
29 Correct 1 ms 596 KB Output is correct
30 Correct 5 ms 596 KB Output is correct
31 Correct 1 ms 596 KB Output is correct
32 Correct 6 ms 596 KB Output is correct
33 Correct 99 ms 2808 KB Output is correct
34 Correct 680 ms 2800 KB Output is correct
35 Correct 12 ms 2772 KB Output is correct
36 Correct 175 ms 3192 KB Output is correct
37 Correct 920 ms 3216 KB Output is correct
38 Correct 15 ms 3156 KB Output is correct
39 Correct 279 ms 3596 KB Output is correct
40 Execution timed out 1100 ms 3540 KB Time limit exceeded
41 Correct 19 ms 3632 KB Output is correct
42 Correct 422 ms 4004 KB Output is correct
43 Execution timed out 1079 ms 3924 KB Time limit exceeded
44 Correct 23 ms 4044 KB Output is correct
45 Correct 627 ms 4436 KB Output is correct
46 Execution timed out 1086 ms 4436 KB Time limit exceeded
47 Correct 28 ms 4436 KB Output is correct
48 Correct 938 ms 4896 KB Output is correct
49 Execution timed out 1074 ms 4820 KB Time limit exceeded
50 Correct 33 ms 4816 KB Output is correct
51 Execution timed out 1082 ms 5308 KB Time limit exceeded
52 Execution timed out 1085 ms 5332 KB Time limit exceeded
53 Correct 45 ms 5332 KB Output is correct
54 Execution timed out 1083 ms 5692 KB Time limit exceeded
55 Execution timed out 1073 ms 5716 KB Time limit exceeded
56 Correct 45 ms 5716 KB Output is correct
57 Execution timed out 1083 ms 6220 KB Time limit exceeded
58 Execution timed out 1092 ms 6228 KB Time limit exceeded
59 Correct 64 ms 6324 KB Output is correct
60 Execution timed out 1084 ms 6612 KB Time limit exceeded
61 Execution timed out 1076 ms 6612 KB Time limit exceeded
62 Correct 59 ms 6720 KB Output is correct
63 Correct 37 ms 6620 KB Output is correct
64 Execution timed out 1075 ms 6704 KB Time limit exceeded
65 Execution timed out 1075 ms 6620 KB Time limit exceeded
66 Execution timed out 1088 ms 6604 KB Time limit exceeded
67 Correct 24 ms 6612 KB Output is correct
68 Correct 27 ms 6760 KB Output is correct
69 Correct 135 ms 6768 KB Output is correct
70 Correct 55 ms 6676 KB Output is correct
71 Correct 39 ms 6668 KB Output is correct
72 Correct 214 ms 6732 KB Output is correct
73 Correct 32 ms 7268 KB Output is correct
74 Execution timed out 1098 ms 7180 KB Time limit exceeded
75 Correct 614 ms 7372 KB Output is correct
76 Correct 326 ms 7260 KB Output is correct
77 Correct 901 ms 7112 KB Output is correct
78 Correct 25 ms 7124 KB Output is correct
79 Execution timed out 1002 ms 7204 KB Time limit exceeded
80 Correct 487 ms 7204 KB Output is correct
81 Correct 286 ms 7208 KB Output is correct
82 Correct 727 ms 7288 KB Output is correct
83 Correct 32 ms 7124 KB Output is correct
84 Correct 689 ms 7132 KB Output is correct
85 Correct 353 ms 7132 KB Output is correct
86 Correct 190 ms 7128 KB Output is correct
87 Correct 493 ms 7128 KB Output is correct
88 Correct 40 ms 6980 KB Output is correct
89 Correct 445 ms 7028 KB Output is correct
90 Correct 206 ms 6988 KB Output is correct
91 Correct 328 ms 7104 KB Output is correct
92 Correct 114 ms 6932 KB Output is correct